From: Zach Johnson Date: Wed, 26 Feb 2020 23:49:02 +0000 (-0800) Subject: Add a truth module, to make cert asserts more fluent X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=a35d05156a6ec386d6be5f7b19e22878a0ee3773;p=android-x86%2Fsystem-bt.git Add a truth module, to make cert asserts more fluent More to come, with event streams. Test: cert/run --host Change-Id: Ie4a750c24990e9d848c673cde482ae8af71a39de --- diff --git a/gd/cert/cert_self_test.py b/gd/cert/cert_self_test.py index ad9d8f51e..bbc3ded47 100644 --- a/gd/cert/cert_self_test.py +++ b/gd/cert/cert_self_test.py @@ -22,6 +22,7 @@ from datetime import datetime, timedelta from acts.base_test import BaseTestClass from cert.event_callback_stream import EventCallbackStream from cert.event_asserts import EventAsserts +from cert.truth import assertThat # Test packet nesting from bluetooth_packets_python3 import hci_packets @@ -231,3 +232,55 @@ class CertSelfTest(BaseTestClass): # Size is ACL (4) + L2CAP (4) + Configure (8) + MTU (4) + FCS (3) asserts.assert_true( len(wrapped.Serialize()) == 23, "Packet serialized incorrectly") + + def test_assertThat_boolean_success(self): + assertThat(True).isTrue() + assertThat(False).isFalse() + + def test_assertThat_boolean_falseIsTrue(self): + try: + assertThat(False).isTrue() + except Exception as e: + return True + return False + + def test_assertThat_boolean_trueIsFalse(self): + try: + assertThat(True).isFalse() + except Exception as e: + return True + return False + + def test_assertThat_object_success(self): + assertThat("this").isEqualTo("this") + assertThat("this").isNotEqualTo("that") + assertThat(None).isNone() + assertThat("this").isNotNone() + + def test_assertThat_object_isEqualToFails(self): + try: + assertThat("this").isEqualTo("that") + except Exception as e: + return True + return False + + def test_assertThat_object_isNotEqualToFails(self): + try: + assertThat("this").isNotEqualTo("this") + except Exception as e: + return True + return False + + def test_assertThat_object_isNoneFails(self): + try: + assertThat("this").isNone() + except Exception as e: + return True + return False + + def test_assertThat_object_isNotNoneFails(self): + try: + assertThat(None).isNotNone() + except Exception as e: + return True + return False diff --git a/gd/cert/truth.py b/gd/cert/truth.py new file mode 100644 index 000000000..1093c96d7 --- /dev/null +++ b/gd/cert/truth.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +# +# Copyright 2020 - The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import time + +from mobly.asserts import assert_true +from mobly.asserts import assert_false + +from mobly import signals + +import sys, traceback + + +class ObjectSubject(object): + + def __init__(self, value): + self._value = value + + def isEqualTo(self, other): + if self._value != other: + raise signals.TestFailure( + "Expected \"%s\" to be equal to \"%s\"" % (self._value, other), + extras=None) + + def isNotEqualTo(self, other): + if self._value == other: + raise signals.TestFailure( + "Expected \"%s\" to not be equal to \"%s\"" % (self._value, + other), + extras=None) + + def isNone(self): + if self._value is not None: + raise signals.TestFailure( + "Expected \"%s\" to be None" % self._value, extras=None) + + def isNotNone(self): + if self._value is None: + raise signals.TestFailure( + "Expected \"%s\" to not be None" % self._value, extras=None) + + +class BooleanSubject(ObjectSubject): + + def __init__(self, value): + super().__init__(value) + + def isTrue(self): + assert_true(self._value, "") + + def isFalse(self): + assert_false(self._value, "") + + +def assertThat(subject): + if type(subject) is bool: + return BooleanSubject(subject) + else: + return ObjectSubject(subject) diff --git a/gd/hci/cert/controller_test.py b/gd/hci/cert/controller_test.py index 9148d80bc..7f6ef2e26 100644 --- a/gd/hci/cert/controller_test.py +++ b/gd/hci/cert/controller_test.py @@ -16,8 +16,8 @@ import time -from mobly.asserts import assert_true from cert.gd_base_test_facade_only import GdFacadeOnlyBaseTestClass +from cert.truth import assertThat from google.protobuf import empty_pb2 as empty_proto from facade import rootservice_pb2 as facade_rootservice from hci.facade import controller_facade_pb2 as controller_facade @@ -32,10 +32,8 @@ class ControllerTest(GdFacadeOnlyBaseTestClass): def test_get_addresses(self): cert_address = self.cert.hci_controller.GetMacAddressSimple() dut_address = self.dut.hci_controller.GetMacAddressSimple() - assert_true( - cert_address != dut_address, - msg="Expected cert and dut address to be different %s" % - cert_address) + + assertThat(cert_address).isNotEqualTo(dut_address) time.sleep(1) # This shouldn't be needed b/149120542 def test_get_local_extended_features(self): @@ -47,10 +45,9 @@ class ControllerTest(GdFacadeOnlyBaseTestClass): request0.page_number = 0 dut_feature_response0 = self.dut.hci_controller.GetLocalExtendedFeatures( request0) - assert_true( - dut_feature_response1.page != dut_feature_response0.page, - msg="Expected cert dut feature pages to be different %d" % - dut_feature_response1.page) + + assertThat(dut_feature_response1.page).isNotEqualTo( + dut_feature_response0.page) def test_write_local_name(self): self.dut.hci_controller.WriteLocalName( @@ -59,8 +56,6 @@ class ControllerTest(GdFacadeOnlyBaseTestClass): controller_facade.NameMsg(name=b'ImTheCert')) cert_name = self.cert.hci_controller.GetLocalNameSimple() dut_name = self.dut.hci_controller.GetLocalNameSimple() - assert_true( - dut_name == b'ImTheDUT', msg="unexpected dut name %s" % dut_name) - assert_true( - cert_name == b'ImTheCert', - msg="unexpected cert name %s" % cert_name) + + assertThat(dut_name).isEqualTo(b'ImTheDUT') + assertThat(cert_name).isEqualTo(b'ImTheCert')