示例#1
0
 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)
示例#2
0
    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),
        )
示例#3
0
def test_different_nodes():
    """Nodes from different classes are not marked as equivalent."""
    node1 = LeafNode()
    node2 = BranchNode()
    assert_false(node1 == node2)
    assert_false(node2 == node1)
    ok_(node1 != node2)
    ok_(node2 != node1)
示例#4
0
 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)
示例#5
0
 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)
示例#6
0
    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)
示例#7
0
def test_branch():
    """Nodes are marked as branch when appropriate."""
    leaf = LeafNode()
    branch = BranchNode()
    assert_false(leaf.is_branch)
    ok_(branch.is_branch)
示例#8
0
 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)
示例#9
0
 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())