def test_listOfTagPatternGenerator(self): """ Querying a list which contains a tag for patterns gives back the tag if the tag has a matching pattern special. """ patterns = IQ([tags.div(pattern="foo", bar="baz")]).patternGenerator("foo") for i in xrange(3): self.assertEqual(patterns.next().attributes['bar'], "baz")
def _testQuery(self, container, expected): pattern = IQ(container).onePattern('foo') # The pattern node has had its pattern special removed - put it back, # so we can perform a comparison self.assertEqual(pattern.pattern, None) pattern.pattern = 'foo' self.assertEqual(str(pattern), str(expected))
def _testTooManyPatterns(self, obj): """ Test that the L{IQ} adapter for C{obj} provides a L{onePattern} method which raises L{stan.TooManyNodes} if passed a pattern name for which there are multiple pattern nodes. """ self.assertRaises(stan.TooManyNodes, IQ(obj).onePattern, 'foo')
def test_onePatternTag(self): """ A L{Tag} returned from L{IQ.onePattern} is represented in the flattened output. """ self.assertStringEqual( self.flatten(IQ(div(pattern="foo")).onePattern("foo")), "<div></div>")
def startMenu(translator, navigation, tag): """ Drop-down menu-style navigation view. For each primary navigation element available, a copy of the I{tab} pattern will be loaded from the tag. It will have its I{href} slot filled with the URL for that navigation item. It will have its I{name} slot filled with the user-visible name of the navigation element. It will have its I{kids} slot filled with a list of secondary navigation for that element. For each secondary navigation element available beneath each primary navigation element, a copy of the I{subtabs} pattern will be loaded from the tag. It will have its I{kids} slot filled with a self-similar structure. @type translator: L{IWebTranslator} provider @type navigation: L{list} of L{Tab} @rtype: {nevow.stan.Tag} """ setTabURLs(navigation, translator) getp = IQ(tag).onePattern def fillSlots(tabs): for tab in tabs: if tab.children: kids = getp('subtabs').fillSlots('kids', fillSlots(tab.children)) else: kids = '' yield dictFillSlots( getp('tab'), dict(href=tab.linkURL, name=tab.name, kids=kids)) return tag.fillSlots('tabs', fillSlots(navigation))
def testContextMissing(self): self.assertRaises( stan.NodeNotFound, IQ(context.WovenContext(tag=notEnough)).patternGenerator, 'foo')
def testContextGenerators(self): self.verify( IQ(context.WovenContext(tag=multiple)).patternGenerator('foo'))
def testTagGenerators(self): self.verify(IQ(multiple).patternGenerator('foo'))
def testXmlMissing(self): self.assertRaises(stan.NodeNotFound, IQ(stan.xml('<html>hello</html>')).patternGenerator, 'foo')
def testLoaderGenerators(self): self.verify(IQ(loaders.stan(multiple)).patternGenerator('foo'))
def testClonableDefault(self): orig = tags.p["Hello"] gen = IQ(flat.precompile(notEnough)).patternGenerator('foo', orig) new = gen.next() self.assertEquals(new.tagName, 'p') self.assertNotIdentical(orig, new)
def test_listNotEnough(self): P = flat.precompile(notEnough) self.assertRaises(stan.NodeNotFound, IQ(P).onePattern, 'foo')
def test_loaderNotEnough(self): L = loaders.stan(notEnough) self.assertRaises(stan.NodeNotFound, IQ(L).onePattern, 'foo')
def test_contextNotEnough(self): self.assertRaises(stan.NodeNotFound, IQ(context.WovenContext(tag=notEnough)).onePattern, 'foo')
def test_contextTagQuery(self): T = simple.clone(deep=False) T.pattern = "outer" C = context.WovenContext(tag=T) new = IQ(C).onePattern('outer') self.assertEquals(new.tagName, 'html')
def test_tagNotEnough(self): self.assertRaises(stan.NodeNotFound, IQ(notEnough).onePattern, 'foo')
def test_loaderQuery(self): L = loaders.stan(simple) new = IQ(L).onePattern('foo') self.assertEquals(new.tagName, 'div')
def test_loaderTooMany(self): L = loaders.stan(tooMany) self.assertRaises(stan.TooManyNodes, IQ(L).onePattern, 'foo')
def testListGenerators(self): self.verify(IQ(flat.precompile(multiple)).patternGenerator('foo'))
def test_listTooMany(self): P = flat.precompile(tooMany) self.assertRaises(stan.TooManyNodes, IQ(P).onePattern, 'foo')
def testListMissing(self): self.assertRaises(stan.NodeNotFound, IQ(flat.precompile(notEnough)).patternGenerator, 'foo')
def testNonClonableDefault(self): gen = IQ(flat.precompile(notEnough)).patternGenerator('foo', 'bar') new = gen.next() self.assertEquals(new, 'bar')
def testTagMissing(self): self.assertRaises(stan.NodeNotFound, IQ(loaders.stan(notEnough)).patternGenerator, 'foo')
def test_listQuery(self): P = flat.precompile(simple) new = IQ(P).onePattern('foo') self.assertEquals(new.tagName, 'div')
def testLoaderPatterns(self): self.verify(IQ(loaders.stan(multiple)).allPatterns('foo'))
def testTagPatterns(self): self.verify(IQ(multiple).allPatterns('foo'))
def applicationNavigation(ctx, translator, navigation): """ Horizontal, primary-only navigation view. For the navigation element currently being viewed, copies of the I{selected-app-tab} and I{selected-tab-contents} patterns will be loaded from the tag. For all other navigation elements, copies of the I{app-tab} and I{tab-contents} patterns will be loaded. For either case, the former pattern will have its I{name} slot filled with the name of the navigation element and its I{tab-contents} slot filled with the latter pattern. The latter pattern will have its I{href} slot filled with a link to the corresponding navigation element. The I{tabs} slot on the tag will be filled with all the I{selected-app-tab} or I{app-tab} pattern copies. @type ctx: L{nevow.context.WebContext} @type translator: L{IWebTranslator} provider @type navigation: L{list} of L{Tab} @rtype: {nevow.stan.Tag} """ setTabURLs(navigation, translator) selectedTab = getSelectedTab(navigation, url.URL.fromContext(ctx)) getp = IQ(ctx.tag).onePattern tabs = [] for tab in navigation: if tab == selectedTab or selectedTab in tab.children: p = 'selected-app-tab' contentp = 'selected-tab-contents' else: p = 'app-tab' contentp = 'tab-contents' childTabs = [] for subtab in tab.children: try: subtabp = getp("subtab") except NodeNotFound: continue childTabs.append( dictFillSlots( subtabp, { 'name': subtab.name, 'href': subtab.linkURL, 'tab-contents': getp("subtab-contents") })) tabs.append( dictFillSlots( getp(p), { 'name': tab.name, 'tab-contents': getp(contentp).fillSlots( 'href', tab.linkURL), 'subtabs': childTabs })) ctx.tag.fillSlots('tabs', tabs) return ctx.tag
def testContextPatterns(self): self.verify(IQ(context.WovenContext(tag=multiple)).allPatterns('foo'))
def testListPatterns(self): self.verify(IQ(flat.precompile(multiple)).allPatterns('foo'))
def test_contextTagTooMany(self): tooMany = tags.html(pattern="foo")[tags.div(pattern="foo")] self.assertRaises(stan.TooManyNodes, IQ(context.WovenContext(tag=tooMany)).onePattern, 'foo')