summaryrefslogtreecommitdiffstats
path: root/tools/grit/grit/format/c_format_unittest.py
blob: ba1c5c71c6d2d8d1619758fd79e307eebdd73e6a (plain)
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
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unittest for c_format.py.
"""

import os
import sys
if __name__ == '__main__':
  sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))

import unittest
import StringIO

from grit import util
from grit.tool import build


class CFormatUnittest(unittest.TestCase):

  def testMessages(self):
    root = util.ParseGrdForUnittest("""
    <messages>
      <message name="IDS_QUESTIONS">Do you want to play questions?</message>
      <message name="IDS_QUOTES">
      "What's in a name, <ph name="NAME">%s<ex>Brandon</ex></ph>?"
      </message>
      <message name="IDS_LINE_BREAKS">
          Was that rhetoric?
No.
Statement.  Two all.  Game point.
</message>
      <message name="IDS_NON_ASCII">
         \xc3\xb5\\xc2\\xa4\\\xc2\xa4\\\\xc3\\xb5\xe4\xa4\xa4
      </message>
    </messages>
      """)

    buf = StringIO.StringIO()
    build.RcBuilder.ProcessNode(root, DummyOutput('c_format', 'en'), buf)
    output = util.StripBlankLinesAndComments(buf.getvalue())
    self.assertEqual(u"""\
#include "resource.h"
const char* GetString(int id) {
  switch (id) {
    case IDS_QUESTIONS:
      return "Do you want to play questions?";
    case IDS_QUOTES:
      return "\\"What\\'s in a name, %s?\\"";
    case IDS_LINE_BREAKS:
      return "Was that rhetoric?\\nNo.\\nStatement.  Two all.  Game point.";
    case IDS_NON_ASCII:
      return "\\303\\265\\xc2\\xa4\\\\302\\244\\\\xc3\\xb5\\344\\244\\244";
    default:
      return 0;
  }
}""", output)


class DummyOutput(object):

  def __init__(self, type, language):
    self.type = type
    self.language = language

  def GetType(self):
    return self.type

  def GetLanguage(self):
    return self.language

  def GetOutputFilename(self):
    return 'hello.gif'

if __name__ == '__main__':
  unittest.main()