def testSplitTextWithCurlies(self): self.assertBuildTree( '''{COUNT, plural, =0 {Open all in &incognito window} =1 {Open in &incognito window} other {Open all ({COUNT}) in &incognito window}}''', pl.Plural('{COUNT, plural,\n ', [ pl.PluralOption('=0 {', [pl.RawText('Open all in &incognito window')]), pl.PluralOption('=1 {', [pl.RawText('Open in &incognito window')]), pl.PluralOption('other {', [ pl.RawText('Open all ('), pl.BasicVariable('{COUNT}'), pl.RawText(') in &incognito window'), ]), ])) self.assertBuildTree( '''{ATTEMPTS_LEFT, plural, =1 {{0} attempt left} other {{0}<a>attempts</a> left}}''', pl.Plural('{ATTEMPTS_LEFT, plural,\n ', [ pl.PluralOption( '=1 {', [pl.BasicVariable('{0}'), pl.RawText(' attempt left')]), pl.PluralOption('other {', [ pl.BasicVariable('{0}'), pl.HtmlTag('<a>'), pl.RawText('attempts'), pl.HtmlTag('</a>'), pl.RawText(' left') ]) ]))
def testToString(self): self.assertEqual(pl.RawText('Hello world').ToString(), 'Hello world') self.assertEqual(pl.BasicVariable('{0}').ToString(), '{0}') self.assertEqual(pl.HtmlTag('<a>').ToString(), '<a>') self.assertEqual( pl.NodeSequence([pl.RawText('Hello'), pl.RawText('World')]).ToString(), 'HelloWorld') self.assertEqual( pl.Plural('{ATTEMPTS_LEFT, plural,\n ', [ pl.PluralOption('=1 {', [pl.RawText('hello')]), pl.PluralOption('other {', [pl.RawText('world')]) ]).ToString(), '{ATTEMPTS_LEFT, plural,\n =1 {hello}\nother {world}\n}')
def testTreeNumWords(self): self.assertEqual(pl.HtmlTag('<a href="blah">').GetNumWords(), 0) self.assertEqual(pl.BasicVariable('{COUNT}').GetNumWords(), 1) self.assertEqual( pl.NodeSequence([pl.RawText('hi'), pl.RawText('World')]).GetNumWords(), 2) self.assertEqual( pl.PluralOption( 'other {', [pl.RawText('hello'), pl.RawText('World')]).GetNumWords(), 2) self.assertEqual( pl.Plural('{COUNT, plural, {', [ pl.PluralOption('=0 {', [pl.RawText('1 2')]), pl.PluralOption('=0 {', [pl.RawText('1 2 3')]), pl.PluralOption('=0 {', [pl.RawText('1 2 3 4 5 6 7 8 9 10')]), ]).GetNumWords(), 10)
def testTransform(self): self.assertTransformsInto(pl.RawText('HI WORLD'), pl.RawText('Hi World')) self.assertTransformsInto(pl.BasicVariable('{HELLO}'), pl.BasicVariable('{HELLO}')) self.assertTransformsInto(pl.HtmlTag('<a>'), pl.HtmlTag('<a>')) self.assertTransformsInto( pl.NodeSequence( [pl.RawText('HELLO'), pl.HtmlTag('<a>'), pl.RawText('WORLD')]), pl.NodeSequence( [pl.RawText('Hello'), pl.HtmlTag('<a>'), pl.RawText('World')])) self.assertTransformsInto( pl.Plural('{ATTEMPTS_LEFT, plural,\n ', [ pl.PluralOption('=1 {', [pl.RawText('hello')]), pl.PluralOption('other {', [pl.RawText('world')]) ]), pl.Plural('{ATTEMPTS_LEFT, plural,\n ', [ pl.PluralOption('=1 {', [pl.RawText('Hello')]), pl.PluralOption('other {', [pl.RawText('World')]) ]))
def testProdFailures(self): p1 = tclib.Placeholder(u'USERNAME', '%s', 'foo') msg = tclib.Message() msg.AppendText(u'{LINE_COUNT, plural,\n =1 {<1 line not shown>}\n' ' other {<') msg.AppendPlaceholder(p1) msg.AppendText(u' lines not shown>}\n}') tree, _ = pl.BuildTreeFromMessage(msg) self.assertTreesEqual( tree, pl.Plural('{LINE_COUNT, plural,\n ', [ pl.PluralOption('=1 {', [pl.RawText('<1 line not shown>')]), pl.PluralOption('other {', [ pl.RawText('<'), PLACEHOLDER_NODE, pl.RawText(' lines not shown>') ]) ])) msg = tclib.Message() msg.AppendText(u'{1, plural,\n \n =1 {Rated ') msg.AppendPlaceholder(p1) msg.AppendText(u' by one user.}\n other{Rated ') msg.AppendPlaceholder(p1) msg.AppendText(u' by # users.}}') tree, _ = pl.BuildTreeFromMessage(msg) self.assertTreesEqual( tree, pl.Plural('{1, plural,\n \n ', [ pl.PluralOption('=1 {', [ pl.RawText('Rated '), PLACEHOLDER_NODE, pl.RawText(' by one user.') ]), pl.PluralOption('other{', [ pl.RawText('Rated '), PLACEHOLDER_NODE, pl.RawText(' by # users.') ]), ])) self.assertBuildTree( '{count, plural, offset:2\n' ' =1 {{VAR}}\n' ' =2 {{VAR}, {VAR}}\n' ' other {{VAR}, {VAR}, and # more}\n' ' }', pl.Plural('{count, plural, offset:2\n ', [ pl.PluralOption('=1 {', [VAR_NODE]), pl.PluralOption( '=2 {', [VAR_NODE, pl.RawText(', '), VAR_NODE]), pl.PluralOption('other {', [ VAR_NODE, pl.RawText(', '), VAR_NODE, pl.RawText(', and # more') ]), ])) self.assertBuildTree( '{NUM_POPUPS,plural,=1{Pop-up blocked} other{# pop-ups blocked}}', pl.Plural('{NUM_POPUPS,plural,', [ pl.PluralOption('=1{', [pl.RawText('Pop-up blocked')]), pl.PluralOption('other{', [pl.RawText('# pop-ups blocked')]) ])) self.assertBuildTree( 'Open ${url}', pl.NodeSequence([pl.RawText('Open '), pl.BasicVariable('${url}')]))