1# Copyright (c) 2004 Divmod.
2# See LICENSE for details.
3
4
5from nevow import stan
6from nevow.testutil import TestCase
7
8class TestProto(TestCase):
9    def test_proto(self):
10        tagName = "hello"
11        proto = stan.Proto(tagName)
12        self.assertEquals(tagName, str(proto))
13
14    def test_callCreatesTag(self):
15        proto = stan.Proto("hello")
16        tag = proto(world="1")
17        self.assertEquals(proto, tag.tagName)
18        self.assertEquals(tag.attributes['world'], '1')
19
20    def test_getItemCreatesTag(self):
21        proto = stan.Proto("hello")
22        tag = proto[proto]
23        self.assertEquals(proto, tag.tagName)
24        self.assertEquals(tag.children, [proto])
25
26
27proto = stan.Proto("hello")
28
29
30class TestTag(TestCase):
31    def test_clone(self):
32        tag = proto(hello="world")["How are you"]
33        tag.fillSlots('foo', 'bar')
34        tag.filename = "foo/bar"
35        tag.lineNumber = 6
36        tag.columnNumber = 12
37        clone = tag.clone()
38        self.assertEquals(clone.attributes['hello'], 'world')
39        self.assertNotIdentical(clone.attributes, tag.attributes)
40        self.assertEquals(clone.children, ["How are you"])
41        self.assertNotIdentical(clone.children, tag.children)
42        self.assertEquals(tag.slotData, clone.slotData)
43        self.assertNotIdentical(tag.slotData, clone.slotData)
44        self.assertEqual(clone.filename, "foo/bar")
45        self.assertEqual(clone.lineNumber, 6)
46        self.assertEqual(clone.columnNumber, 12)
47
48    ## TODO: need better clone test here to test clone(deep=True),
49    ## and behavior of cloning nested lists.
50
51    def test_clear(self):
52        tag = proto["these are", "children", "cool"]
53        tag.clear()
54        self.assertEquals(tag.children, [])
55
56    def test_specials(self):
57        tag = proto(data=1, render=str, remember="stuff", key="myKey", **{'pattern': "item"})
58        self.assertEquals(tag.data, 1)
59        self.assertEquals(getattr(tag, 'render'), str)
60        self.assertEquals(tag.remember, "stuff")
61        self.assertEquals(tag.key, "myKey")
62        self.assertEquals(tag.pattern, "item")
63
64
65    def test_visit(self):
66        """
67        Test that L{nevow.stan.visit} invokes the visitor it is given with all
68        the nodes in the DOM it is given in pre-order.
69        """
70        visited = []
71        def visitor(t):
72            visited.append(t)
73        root = stan.Proto('root')()
74        firstChild = stan.Proto('firstChild')()
75        secondChild = stan.Proto('secondChild')()
76        firstGrandchild = stan.Proto('firstGrandchild')()
77        secondGrandchild = stan.Proto('secondGrandchild')()
78        thirdGrandchild = 'thirdGrandchild'
79        root[firstChild, secondChild]
80        secondChild[firstGrandchild, secondGrandchild, thirdGrandchild]
81        stan.visit(root, visitor)
82        self.assertEquals(
83            visited,
84            [root, firstChild, secondChild,
85             firstGrandchild, secondGrandchild, thirdGrandchild])
86
87
88
89class TestComment(TestCase):
90
91    def test_notCallable(self):
92        comment = stan.CommentProto()
93        self.assertRaises(NotImplementedError, comment, id='oops')
94
95class TestUnderscore(TestCase):
96    def test_prefix(self):
97        proto = stan.Proto('div')
98        tag = proto()
99        tag(_class='a')
100        self.assertEquals(tag.attributes, {'class': 'a'})
101
102    def test_suffix(self):
103        proto = stan.Proto('div')
104        tag = proto()
105        tag(class_='a')
106        self.assertEquals(tag.attributes, {'class': 'a'})
107