Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Copyright (c) 2004 Divmod.
# See LICENSE for details.
from nevow.accessors import convertToData, NoAccessor
from nevow import context
from nevow import inevow
from nevow.stan import directive
from nevow.tags import invisible, slot
from nevow.flat import precompile
from nevow import rend
from nevow.testutil import TestCase
class Base(TestCase):
def makeContext(self, *args):
ctx = context.WovenContext()
ctx.remember(convertToData(self.remember, ctx), inevow.IData)
for a in args:
ctx = context.WovenContext(ctx, invisible())
ctx.remember(convertToData(a, ctx), inevow.IData)
return ctx
class TestBasics(Base):
remember = None
def test_dict_directive(self):
d = directive('one')
ctx = self.makeContext({'one': 1, 'two': 2})
self.assertEqual(1, convertToData(d, ctx))
self.assertRaises(KeyError, convertToData, directive('asdfasdf'), ctx)
def test_list_directive(self):
d= directive('2')
ctx = self.makeContext([0, 1, 42, 3, 4])
self.assertEqual(42, convertToData(d, ctx))
self.assertRaises(IndexError, convertToData, directive('9999'), ctx)
self.assertRaises(ValueError, convertToData, directive('HAHAHAHA'), ctx)
def test_function_accessor(self):
def foo(context, data):
return 42
ctx = self.makeContext()
self.assertEqual(42, convertToData(foo, ctx))
d = directive('this wont work')
ctx2 = self.makeContext(foo)
self.assertRaises(NoAccessor, convertToData, d, ctx2)
thefoo = "foo"
class Factory(rend.DataFactory):
original = None
def data_foo(self, context, data):
return thefoo
def data_dict(self, context, data):
return {"one": 1, "two": 2}
def data_list(self, context, data):
return [1, 99, 43]
def data_factory(self, context, data):
return [self]
f = Factory()
class TestThroughDirective(Base):
remember = f
def test_simple(self):
d = directive('foo')
ctx = self.makeContext()
self.assertEqual(thefoo, convertToData(d, ctx))
def test_dict_through_directive(self):
d1, d2 = directive('dict'), directive('one')
ctx = self.makeContext(d1)
self.assertEqual(1, convertToData(d2, ctx))
def test_list_through_directive(self):
d1, d2 = directive('list'), directive('1')
ctx = self.makeContext(d1)
self.assertEqual(99, convertToData(d2, ctx))
def test_roundAndRound(self):
ctx = self.makeContext(
directive('factory'), directive('0'), directive('factory')
)
self.assertEqual(f, convertToData(directive('0'), ctx))
def test_missing(self):
self.assertRaises(rend.DataNotFoundError, self.makeContext, directive('missing'))
class APage(rend.Page):
def data_foo(self, ctx, data):
return "foo"
class TestPageData(Base):
def test_1_noneOriginal(self):
data = None
ctx = context.WovenContext()
ctx.remember(APage(data), inevow.IData)
self.assertEqual(data, convertToData(ctx.locate(inevow.IData), ctx))
self.assertEqual('foo', convertToData(directive('foo'), ctx))
def test_2_dictOriginal(self):
data = {'hello': 'world'}
ctx = context.WovenContext()
ctx.remember(APage(data), inevow.IData)
# IGettable should return our dictionary
self.assertEqual(data, convertToData(ctx.locate(inevow.IData), ctx))
# IContainer on the *Page*, not the dictionary, should work
self.assertEqual('foo', convertToData(directive('foo'), ctx))
# IContainer on the Page should delegate to IContainer(self.original) if no data_ method
self.assertEqual('world', convertToData(directive('hello'), ctx))
class SlotAccessorTestCase(TestCase):
def _accessorTest(self, obj):
ctx = context.WovenContext()
ctx.fillSlots('slotname', 'foo')
data = inevow.IGettable(obj).get(ctx)
self.assertEqual(data, 'foo')
def test_slotAccessor(self):
return self._accessorTest(slot('slotname'))
def test_precompiledSlotAccessor(self):
return self._accessorTest(precompile(slot('slotname'))[0])