1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6'''Unit tests for grit.xtb_reader'''
7
8
9import os
10import sys
11if __name__ == '__main__':
12  sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
13
14import StringIO
15import unittest
16
17from grit import util
18from grit import xtb_reader
19from grit.node import empty
20
21
22class XtbReaderUnittest(unittest.TestCase):
23  def testParsing(self):
24    xtb_file = StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?>
25      <!DOCTYPE translationbundle>
26      <translationbundle lang="fr">
27        <translation id="5282608565720904145">Bingo.</translation>
28        <translation id="2955977306445326147">Bongo longo.</translation>
29        <translation id="238824332917605038">Hullo</translation>
30        <translation id="6629135689895381486"><ph name="PROBLEM_REPORT"/> peut <ph name="START_LINK"/>utilisation excessive de majuscules<ph name="END_LINK"/>.</translation>
31        <translation id="7729135689895381486">Hello
32this is another line
33and another
34
35and another after a blank line.</translation>
36      </translationbundle>''')
37
38    messages = []
39    def Callback(id, structure):
40      messages.append((id, structure))
41    xtb_reader.Parse(xtb_file, Callback)
42    self.failUnless(len(messages[0][1]) == 1)
43    self.failUnless(messages[3][1][0])  # PROBLEM_REPORT placeholder
44    self.failUnless(messages[4][0] == '7729135689895381486')
45    self.failUnless(messages[4][1][7][1] == 'and another after a blank line.')
46
47  def testParsingIntoMessages(self):
48    root = util.ParseGrdForUnittest('''
49      <messages>
50        <message name="ID_MEGA">Fantastic!</message>
51        <message name="ID_HELLO_USER">Hello <ph name="USERNAME">%s<ex>Joi</ex></ph></message>
52      </messages>''')
53
54    msgs, = root.GetChildrenOfType(empty.MessagesNode)
55    clique_mega = msgs.children[0].GetCliques()[0]
56    msg_mega = clique_mega.GetMessage()
57    clique_hello_user = msgs.children[1].GetCliques()[0]
58    msg_hello_user = clique_hello_user.GetMessage()
59
60    xtb_file = StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?>
61      <!DOCTYPE translationbundle>
62      <translationbundle lang="is">
63        <translation id="%s">Meirihattar!</translation>
64        <translation id="%s">Saelir <ph name="USERNAME"/></translation>
65      </translationbundle>''' % (msg_mega.GetId(), msg_hello_user.GetId()))
66
67    xtb_reader.Parse(xtb_file,
68                     msgs.UberClique().GenerateXtbParserCallback('is'))
69    self.assertEqual('Meirihattar!',
70                     clique_mega.MessageForLanguage('is').GetRealContent())
71    self.failUnless('Saelir %s',
72                    clique_hello_user.MessageForLanguage('is').GetRealContent())
73
74  def testIfNodesWithUseNameForId(self):
75    root = util.ParseGrdForUnittest('''
76      <messages>
77        <message name="ID_BINGO" use_name_for_id="true">Bingo!</message>
78      </messages>''')
79    msgs, = root.GetChildrenOfType(empty.MessagesNode)
80    clique = msgs.children[0].GetCliques()[0]
81    msg = clique.GetMessage()
82
83    xtb_file = StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?>
84      <!DOCTYPE translationbundle>
85      <translationbundle lang="is">
86        <if expr="is_linux">
87          <translation id="ID_BINGO">Bongo!</translation>
88        </if>
89        <if expr="not is_linux">
90          <translation id="ID_BINGO">Congo!</translation>
91        </if>
92      </translationbundle>''')
93    xtb_reader.Parse(xtb_file,
94                     msgs.UberClique().GenerateXtbParserCallback('is'),
95                     target_platform='darwin')
96    self.assertEqual('Congo!', clique.MessageForLanguage('is').GetRealContent())
97
98  def testParseLargeFile(self):
99    def Callback(id, structure):
100      pass
101    with open(util.PathFromRoot('grit/testdata/generated_resources_fr.xtb')) as xtb:
102      xtb_reader.Parse(xtb, Callback)
103
104
105if __name__ == '__main__':
106  unittest.main()
107