def test_equivalence(self): """ Two function calls are equivalent not only if they share the same class, but also if their arguments are equivalent. """ class FooFunction(Function): required_arguments = ("abc", ) optional_arguments = {"xyz": LeafNode("123")} class CommutativeFunction(Function): required_arguments = range(2) argument_types = BooleanType is_commutative = True func1 = FooFunction(LeafNode("whatever")) func2 = FooFunction(LeafNode("whatever")) func3 = PermissiveFunction(BranchNode("baz")) func4 = PermissiveFunction(LeafNode("foo")) func5 = FooFunction(LeafNode("something")) func6 = CommutativeFunction(BoolVar(), TrafficLightVar()) func7 = CommutativeFunction(TrafficLightVar(), BoolVar()) assert_node_equivalence( (func1, func2), (func3, ), (func4, ), (func5, ), (func6, func7), )
def test_constructor_with_minimum_arguments(self): func = PermissiveFunction(LeafNode("baz")) args = { 'arg0': LeafNode("baz"), 'oarg0': LeafNode("foo"), 'oarg1': BranchNode("bar"), } eq_(func.arguments, args)
def test_constructor_with_one_optional_argument(self): func = PermissiveFunction(LeafNode("this-is-arg0"), LeafNode("this-is-oarg0")) args = { 'arg0': LeafNode("this-is-arg0"), 'oarg0': LeafNode("this-is-oarg0"), 'oarg1': BranchNode("bar"), } eq_(func.arguments, args)
def test_hash_with_arguments(self): """The hash of a function also uses the hash of the arguments.""" arg1 = LeafNode("foo") arg2 = BranchNode(u"fú") arg3 = LeafNode("bar") func = PermissiveFunction(arg1, arg2, arg3) args_hash = hash(arg1) + hash(arg2) + hash(arg3) expected_hash = hash(PermissiveFunction) + args_hash eq_(hash(func), expected_hash)
def test_constructor_with_all_arguments(self): func = PermissiveFunction( BranchNode("this-is-arg0"), LeafNode("this-is-oarg0"), BranchNode("this-is-oarg1"), ) args = { 'arg0': BranchNode("this-is-arg0"), 'oarg0': LeafNode("this-is-oarg0"), 'oarg1': BranchNode("this-is-oarg1"), } eq_(func.arguments, args)
def test_constructor_with_many_arguments(self): PermissiveFunction( LeafNode(0), LeafNode(1), LeafNode(2), LeafNode(3), LeafNode(4), LeafNode(5), LeafNode(6), LeafNode(7), LeafNode(8), LeafNode(9), )
def test_representation(self): func = PermissiveFunction(LeafNode("foo"), BranchNode(u"fú")) expected = "<Anonymous function call [PermissiveFunction] " \ "arg0=LeafNode('foo'), oarg0=BranchNode(u'f\\xfa'), " \ "oarg1=BranchNode('bar')>" eq_(repr(func), expected)
def test_constructor_accepts_operands(self): """Only operands are valid function arguments.""" PermissiveFunction(LeafNode(), BranchNode()) assert_raises(BadCallError, PermissiveFunction, None) assert_raises(BadCallError, PermissiveFunction, 2) assert_raises(BadCallError, PermissiveFunction, Datatype())
def test_constructor_with_few_arguments(self): PermissiveFunction()
def test_node_type(self): """Functions are branch nodes.""" func = PermissiveFunction(LeafNode()) ok_(func.is_branch) assert_false(func.is_leaf)
def test_existing_function_without_namespace(self): parse_tree = self.parser('~ foo("baz")') eq_(parse_tree.root_node, Not(PermissiveFunction(String("baz"))))