Home | History | Annotate | Download | only in json_schema_compiler
      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 from code import Code
      7 import unittest
      8 
      9 class CodeTest(unittest.TestCase):
     10   def testAppend(self):
     11     c = Code()
     12     c.Append('line')
     13     self.assertEquals('line', c.Render())
     14 
     15   def testBlock(self):
     16     c = Code()
     17     (c.Append('line')
     18       .Sblock('sblock')
     19         .Append('inner')
     20         .Append('moreinner')
     21         .Sblock('moresblock')
     22           .Append('inner')
     23         .Eblock('out')
     24         .Append('inner')
     25       .Eblock('out')
     26     )
     27     self.assertEquals(
     28       'line\n'
     29       'sblock\n'
     30       '  inner\n'
     31       '  moreinner\n'
     32       '  moresblock\n'
     33       '    inner\n'
     34       '  out\n'
     35       '  inner\n'
     36       'out',
     37       c.Render())
     38 
     39   def testConcat(self):
     40     b = Code()
     41     (b.Sblock('2')
     42         .Append('2')
     43       .Eblock('2')
     44     )
     45     c = Code()
     46     (c.Sblock('1')
     47         .Concat(b)
     48         .Append('1')
     49       .Eblock('1')
     50     )
     51     self.assertEquals(
     52       '1\n'
     53       '  2\n'
     54       '    2\n'
     55       '  2\n'
     56       '  1\n'
     57       '1',
     58       c.Render())
     59     d = Code()
     60     a = Code()
     61     a.Concat(d)
     62     self.assertEquals('', a.Render())
     63     a.Concat(c)
     64     self.assertEquals(
     65       '1\n'
     66       '  2\n'
     67       '    2\n'
     68       '  2\n'
     69       '  1\n'
     70       '1',
     71       a.Render())
     72 
     73   def testConcatErrors(self):
     74     c = Code()
     75     d = Code()
     76     d.Append('%s')
     77     self.assertRaises(TypeError, c.Concat, d)
     78     d = Code()
     79     d.Append('%(classname)s')
     80     self.assertRaises(TypeError, c.Concat, d)
     81     d = 'line of code'
     82     self.assertRaises(TypeError, c.Concat, d)
     83 
     84   def testSubstitute(self):
     85     c = Code()
     86     c.Append('%(var1)s %(var2)s %(var1)s')
     87     c.Substitute({'var1': 'one', 'var2': 'two'})
     88     self.assertEquals('one two one', c.Render())
     89     c.Append('%(var1)s %(var2)s %(var3)s')
     90     c.Append('%(var2)s %(var1)s %(var3)s')
     91     c.Substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'})
     92     self.assertEquals(
     93         'one two one\n'
     94         'one two three\n'
     95         'two one three',
     96         c.Render())
     97 
     98   def testSubstituteErrors(self):
     99     # No unnamed placeholders allowed when substitute is run
    100     c = Code()
    101     c.Append('%s %s')
    102     self.assertRaises(TypeError, c.Substitute, ('var1', 'one'))
    103     c = Code()
    104     c.Append('%s %(var1)s')
    105     self.assertRaises(TypeError, c.Substitute, {'var1': 'one'})
    106     c = Code()
    107     c.Append('%s %(var1)s')
    108     self.assertRaises(TypeError, c.Substitute, {'var1': 'one'})
    109     c = Code()
    110     c.Append('%(var1)s')
    111     self.assertRaises(KeyError, c.Substitute, {'clearlynotvar1': 'one'})
    112 
    113   def testIsEmpty(self):
    114     c = Code()
    115     self.assertTrue(c.IsEmpty())
    116     c.Append('asdf')
    117     self.assertFalse(c.IsEmpty())
    118 
    119   def testComment(self):
    120     long_comment = ('This comment is eighty nine characters in longness, '
    121         'that is, to use another word, length')
    122     c = Code()
    123     c.Comment(long_comment)
    124     self.assertEquals(
    125         '// This comment is eighty nine characters '
    126         'in longness, that is, to use another\n'
    127         '// word, length',
    128         c.Render())
    129     c = Code()
    130     c.Sblock('sblock')
    131     c.Comment(long_comment)
    132     c.Eblock('eblock')
    133     c.Comment(long_comment)
    134     self.assertEquals(
    135         'sblock\n'
    136         '  // This comment is eighty nine characters '
    137         'in longness, that is, to use\n'
    138         '  // another word, length\n'
    139         'eblock\n'
    140         '// This comment is eighty nine characters in '
    141         'longness, that is, to use another\n'
    142         '// word, length',
    143         c.Render())
    144     long_word = 'x' * 100
    145     c = Code()
    146     c.Comment(long_word)
    147     self.assertEquals(
    148         '// ' + 'x' * 77 + '\n'
    149         '// ' + 'x' * 23,
    150         c.Render())
    151 
    152   def testCommentWithSpecialCharacters(self):
    153     c = Code()
    154     c.Comment('20% of 80%s')
    155     c.Substitute({})
    156     self.assertEquals('// 20% of 80%s', c.Render())
    157     d = Code()
    158     d.Append('90')
    159     d.Concat(c)
    160     self.assertEquals('90\n'
    161         '// 20% of 80%s',
    162         d.Render())
    163 
    164 if __name__ == '__main__':
    165   unittest.main()
    166