def test_equivalence(self): """ Two functions are equivalent not only if they share the same class, but also if their arguments are equivalent. """ class FooFunction(Function): bypass_operation_check = True required_arguments = ("abc", ) optional_arguments = {"xyz": String("123")} def check_arguments(self): pass func1 = FooFunction(String("whatever")) func2 = FooFunction(String("whatever")) func3 = TrafficViolationFunc(String("pedestrians")) func4 = PermissiveFunction(String("foo")) func5 = FooFunction(String("something")) func1.check_equivalence(func2) func2.check_equivalence(func1) assert_raises(AssertionError, func1.check_equivalence, func3) assert_raises(AssertionError, func1.check_equivalence, func4) assert_raises(AssertionError, func1.check_equivalence, func5) assert_raises(AssertionError, func2.check_equivalence, func3) assert_raises(AssertionError, func2.check_equivalence, func4) assert_raises(AssertionError, func2.check_equivalence, func5) assert_raises(AssertionError, func3.check_equivalence, func1) assert_raises(AssertionError, func3.check_equivalence, func2) assert_raises(AssertionError, func3.check_equivalence, func4) assert_raises(AssertionError, func3.check_equivalence, func5) assert_raises(AssertionError, func4.check_equivalence, func1) assert_raises(AssertionError, func4.check_equivalence, func2) assert_raises(AssertionError, func4.check_equivalence, func3) assert_raises(AssertionError, func4.check_equivalence, func5) assert_raises(AssertionError, func5.check_equivalence, func1) assert_raises(AssertionError, func5.check_equivalence, func2) assert_raises(AssertionError, func5.check_equivalence, func3) assert_raises(AssertionError, func5.check_equivalence, func4) ok_(func1 == func2) ok_(func2 == func1) ok_(func1 != func3) ok_(func1 != func4) ok_(func1 != func5) ok_(func2 != func3) ok_(func2 != func4) ok_(func2 != func5) ok_(func3 != func1) ok_(func3 != func2) ok_(func3 != func4) ok_(func3 != func5) ok_(func4 != func1) ok_(func4 != func2) ok_(func4 != func3) ok_(func4 != func5) ok_(func5 != func1) ok_(func5 != func2) ok_(func5 != func3) ok_(func5 != func4)
def test_subset(self): op = Set(String("carla"), String("andreina"), String("liliana")) ok_(op.is_subset(["carla"], None)) ok_(op.is_subset(["carla", "liliana"], None)) ok_(op.is_subset(["andreina", "carla"], None)) assert_false(op.is_subset(["gustavo", "carlos"], None)) assert_false(op.is_subset(["carla", "gustavo"], None))
def test_representation(self): # With Unicode: string = String(u"caña") eq_(repr(string), '<String "caña">') # With ASCII string = String("cana") eq_(repr(string), '<String "cana">')
def test_greater_than(self): op = Set(String("carla"), String("andreina"), String("liliana")) ok_(op.greater_than(2, None)) ok_(op.greater_than("2", None)) assert_false(op.greater_than(3, None)) assert_false(op.greater_than(4, None)) assert_raises(InvalidOperationError, op.greater_than, "some string", 0) assert_raises(InvalidOperationError, op.greater_than, 2.60, None)
def test_constructor_with_minimum_arguments(self): func = PermissiveFunction(String("this-is-arg0")) args = { 'arg0': String("this-is-arg0"), 'oarg0': Set(), 'oarg1': Number(1), } eq_(func.arguments, args)
def test_parsing_with_no_localized_grammars(self): mgr = EvaluableParseManager(self.symbol_table, Grammar()) parse_tree1 = mgr.parse('message == "2009-07-13"') parse_tree2 = mgr.parse('message == "2009-07-13"', None) expected_tree = EvaluableParseTree( Equal(String("Hello world"), String("2009-07-13"))) eq_(parse_tree1, parse_tree2) eq_(parse_tree1, expected_tree)
def test_less_than(self): op = Set(String("carla"), String("andreina"), String("liliana")) ok_(op.less_than(4, None)) ok_(op.less_than("4", None)) assert_false(op.less_than(2, None)) assert_false(op.less_than(1, None)) assert_raises(InvalidOperationError, op.less_than, "some string", None) assert_raises(InvalidOperationError, op.less_than, 3.10, None)
def test_representation_without_namespace(self): # With Unicode: func = PlaceholderFunction(u"aquí", None, Number(1), String("hi")) eq_('<Placeholder function call "aquí"(<Number 1.0>, <String "hi">)>', repr(func)) # With ASCII: func = PlaceholderFunction("here", None, Number(1), String("hi")) eq_(u'<Placeholder function call "here"(<Number 1.0>, <String "hi">)>', repr(func))
def test_constructor_with_one_optional_argument(self): func = PermissiveFunction(String("this-is-arg0"), String("this-is-oarg0")) args = { 'arg0': String("this-is-arg0"), 'oarg0': String("this-is-oarg0"), 'oarg1': Number(1), } eq_(func.arguments, args)
def test_representation_with_namespace(self): # With Unicode: func = PlaceholderFunction(u"aquí", ["a", "d"], Number(1), String("hi")) eq_('<Placeholder function call "aquí"(<Number 1.0>, <String "hi">) ' \ 'at namespace="a:d">', repr(func)) # With ASCII: func = PlaceholderFunction("here", ["a", "d"], Number(1), String("hi")) eq_(u'<Placeholder function call "here"(<Number 1.0>, <String "hi">) ' \ 'at namespace="a:d">', repr(func))
def test_number_support(self): """Strings given as a number must be converted first into a string""" # When the constant is defined as a number: op = String(10) ok_(op.equals("10", None)) # When the constant is defined as a string: op = String("10") ok_(op.equals(10, None)) # When both are defined as numbers: op = String(10) ok_(op.equals(10, None))
def test_representation(self): set1 = Set(Number(3), Number(5)) try: eq_(repr(set1), "<Set <Number 3.0>, <Number 5.0>>") except AssertionError: eq_(repr(set1), "<Set <Number 5.0>, <Number 3.0>>") # Now with an empty set: set2 = Set() eq_(repr(set2), "<Set>") # Now with Unicode stuff in it: set3 = Set(String(u"España"), String(u"carabobeño")) try: eq_(repr(set3), '<Set <String "España">, <String "carabobeño">>') except AssertionError: eq_(repr(set3), '<Set <String "carabobeño">, <String "España">>')
def test_global_names(self): """When an operand is bound, its global name must be set accordingly.""" operand = String("hey there") bind1 = Bind("da_global_name", operand) bind2 = Bind("Da_Global_Name", operand) eq_(bind1.global_name, "da_global_name") eq_(bind1.global_name, bind2.global_name)
def test_constructor_with_objects(self): objects = [ Bind("greeting", String("hey")), Bind("traffic", TrafficLightVar()) ] st = SymbolTable("global", objects) eq_(st.objects, set(objects))
def test_checking_logical_support(self): """Sets don't have logical support.""" op = Set(Number(9), String("carla")) op.check_logical_support() ok_(op(None)) ok_(Set(Number(0))(None)) ok_(not Set()(None))
def test_retrieving_object_in_non_existing_subtable(self): global_objects = { 'foo': TrafficLightVar(), 'traffic-violation': TrafficViolationFunc(String("pedestrians")), } st = Namespace(global_objects, ) assert_raises(ScopeError, st.get_object, "foo", ["doesn't", "exist"])
def test_retrieving_non_existing_object(self): global_objects = { 'foo': TrafficLightVar(), 'traffic-violation': TrafficViolationFunc(String("pedestrians")), } st = Namespace(global_objects, ) assert_raises(ScopeError, st.get_object, "bool")
def test_duplicate_objects(self): """There must be no duplicate object.""" # In the constructor: objects1 = [ Bind("salutation", String("hey")), Bind("traffic", TrafficLightVar()), Bind("salutation", String("hey")) ] assert_raises(ScopeError, SymbolTable, "global", objects1) # Post-instantiation: objects2 = [ Bind("salutation", String("hey")), Bind("traffic", TrafficLightVar()) ] st = SymbolTable("global", objects2) assert_raises(ScopeError, st.add_object, Bind("salutation", String("hey")))
def test_parsing_with_no_localized_grammars(self): mgr = ConvertibleParseManager(Grammar()) parse_tree1 = mgr.parse('message == "2009-07-13"') parse_tree2 = mgr.parse('message == "2009-07-13"', None) expected_tree = ConvertibleParseTree( Equal(PlaceholderVariable("message"), String("2009-07-13"))) eq_(parse_tree1, parse_tree2) eq_(parse_tree1, expected_tree)
def test_instantiation(self): """All the members of a set must be operands""" # Instantiation with operands: Set(Number(3), String("hola")) # Instantiation with a non-operand value: try: Set(Number(3), "hola") assert False, "InvalidOperationError exception not raised" except InvalidOperationError as exc: assert 'Item "hola" is not an operand' in six.text_type(exc)
def test_equality(self): op = Set(Number(3), String("hola")) set1 = {3, "hola"} set2 = {3} set3 = {3, "hola", "something else"} set4 = {"nothing to do"} # Comparing them... ok_(op.equals(set1, None), "The constant equals %s" % op.constant_value) assert_false(op.equals(set2, None)) assert_false(op.equals(set3, None)) assert_false(op.equals(set4, None))
def test_checking_logical_support(self): class NoBoolFunction(Function): operations = set(["equality"]) def equals(self, node): pass def to_python(self): pass def check_arguments(self): pass func1 = PermissiveFunction(String("foo")) func2 = NoBoolFunction() # Checking logical support: func1.check_logical_support() assert_raises(InvalidOperationError, func2.check_logical_support)
def test_equivalence(self): """ Two constant strings are equivalent if they represent the same string. """ text1 = String("hello world") text2 = String("hello earth") text3 = String("hello world") text1.check_equivalence(text3) text3.check_equivalence(text1) assert_raises(AssertionError, text1.check_equivalence, text2) assert_raises(AssertionError, text2.check_equivalence, text1) assert_raises(AssertionError, text2.check_equivalence, text3) assert_raises(AssertionError, text3.check_equivalence, text2) ok_(text1 == text3) ok_(text3 == text1) ok_(text1 != text2) ok_(text2 != text1) ok_(text2 != text3) ok_(text3 != text2)
def test_names(self): """ When an operand is bound, its multiple names must be set accordingly. """ operand = String("Vive la france !") # No localized names: bind1 = Bind("foo", operand) eq_(bind1.names, {}) # Lower-case names: names0 = {'es_VE': "cartuchera", 'es_ES': "estuche"} bind2 = Bind("bar", operand, **names0) eq_(bind2.names, names0) # Mixed-case names -- must be converted to lower-case: names1 = {'es_VE': "Cartuchera", 'es_ES': "estuche"} bind2 = Bind("bar", operand, **names1) eq_(bind2.names, names0)
def test_constructor_with_all_arguments(self): func = PermissiveFunction( String("this-is-arg0"), String("this-is-oarg0"), String("this-is-oarg1"), ) args = { 'arg0': String("this-is-arg0"), 'oarg0': String("this-is-oarg0"), 'oarg1': String("this-is-oarg1"), } eq_(func.arguments, args)
def test_retrieving_existing_3rd_level_object(self): third_level_objects = { 'bool': BoolVar(), } second_level_objects = { 'traffic': TrafficLightVar(), } global_objects = { 'foo': TrafficLightVar(), 'traffic-violation': TrafficViolationFunc(String("pedestrians")), } third_level_namespaces = {'sub2': Namespace(third_level_objects)} second_level_namespaces = { 'sub1': Namespace(second_level_objects, third_level_namespaces) } st = Namespace(global_objects, second_level_namespaces) requested_object = st.get_object("bool", ["sub1", "sub2"]) eq_(requested_object, BoolVar())
def test_retrieving_non_existing_localized_names(self): """ When a non-existing localized name is requested, a warning must be issued and the global name returned. """ bind = Bind("foo", String("hey"), es="fulano") logging_fixture = LoggingHandlerFixture() eq_(bind.get_localized_name("fr"), "foo") # Checking for the the warning: eq_(len(logging_fixture.handler.messages["warning"]), 1) warning = logging_fixture.handler.messages["warning"][0] eq_( warning, 'Operand "hey" bound as "foo" doesn\'t have a name in fr; ' 'using the global one') # Undoing it: logging_fixture.undo()
def test_equivalence(self): tree1 = ConvertibleParseTree(PlaceholderVariable("my_variable")) tree2 = ConvertibleParseTree(PlaceholderVariable("my_variable")) tree3 = ConvertibleParseTree(String("hello")) tree4 = EvaluableParseTree(BoolVar()) ok_(tree1 == tree2) ok_(tree2 == tree1) ok_(tree1 != None) ok_(tree1 != tree3) ok_(tree1 != tree4) ok_(tree2 != tree3) ok_(tree2 != tree4) ok_(tree3 != tree1) ok_(tree3 != tree2) ok_(tree3 != tree4) ok_(tree4 != tree1) ok_(tree4 != tree2) ok_(tree4 != tree3)
def test_equivalence(self): """ Two constant sets A and B are equivalent if each element in A is equivalent to one element in B. """ set1 = Set(String("hi"), Set(Number(3), String("hello"))) set2 = Set(String("hi"), Number(3), String("hello")) set3 = Set(Set(String("hello"), Number(3)), String("hi")) set4 = Set(String("hi"), String("hello")) set1.check_equivalence(set3) set3.check_equivalence(set1) assert_raises(AssertionError, set1.check_equivalence, set2) assert_raises(AssertionError, set1.check_equivalence, set4) assert_raises(AssertionError, set2.check_equivalence, set1) assert_raises(AssertionError, set2.check_equivalence, set3) assert_raises(AssertionError, set2.check_equivalence, set4) assert_raises(AssertionError, set3.check_equivalence, set2) assert_raises(AssertionError, set3.check_equivalence, set4) assert_raises(AssertionError, set4.check_equivalence, set1) assert_raises(AssertionError, set4.check_equivalence, set2) assert_raises(AssertionError, set4.check_equivalence, set3) ok_(set1 == set3) ok_(set3 == set1) ok_(set1 != set2) ok_(set1 != set4) ok_(set2 != set1) ok_(set2 != set3) ok_(set2 != set4) ok_(set3 != set2) ok_(set3 != set4) ok_(set4 != set1) ok_(set4 != set2) ok_(set4 != set3)
def test_belongs_to(self): op = Set(String("arepa"), Number(4)) ok_(op.belongs_to(4, None)) ok_(op.belongs_to(4.00, None)) ok_(op.belongs_to("arepa", None)) assert_false(op.belongs_to("something else", None))