github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-tools-src/grumpy_tools/compiler/util_test.py (about)

     1  # coding=utf-8
     2  
     3  # Copyright 2016 Google Inc. All Rights Reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  """Tests Writer and other utils."""
    18  
    19  from __future__ import unicode_literals
    20  
    21  import unittest
    22  
    23  from grumpy_tools.compiler import block
    24  from grumpy_tools.compiler import imputil
    25  from grumpy_tools.compiler import util
    26  
    27  
    28  class WriterTest(unittest.TestCase):
    29  
    30    def testIndentBlock(self):
    31      writer = util.Writer()
    32      writer.write('foo')
    33      with writer.indent_block(n=2):
    34        writer.write('bar')
    35      writer.write('baz')
    36      self.assertEqual(writer.getvalue(), 'foo\n\t\tbar\nbaz\n')
    37  
    38    def testWriteBlock(self):
    39      writer = util.Writer()
    40      mod_block = block.ModuleBlock(None, '__main__', '<test>', '',
    41                                    imputil.FutureFeatures())
    42      writer.write_block(mod_block, 'BODY')
    43      output = writer.getvalue()
    44      dispatch = 'switch πF.State() {\n\tcase 0:\n\tdefault: panic'
    45      self.assertIn(dispatch, output)
    46  
    47    def testWriteMultiline(self):
    48      writer = util.Writer()
    49      writer.indent(2)
    50      writer.write('foo\nbar\nbaz\n')
    51      self.assertEqual(writer.getvalue(), '\t\tfoo\n\t\tbar\n\t\tbaz\n')
    52  
    53    def testWritePyContext(self):
    54      writer = util.Writer()
    55      writer.write_py_context(12, 'print "foo"')
    56      self.assertEqual(writer.getvalue(), '// line 12: print "foo"\n')
    57  
    58    def testWriteSkipBlankLine(self):
    59      writer = util.Writer()
    60      writer.write('foo\n\nbar')
    61      self.assertEqual(writer.getvalue(), 'foo\nbar\n')
    62  
    63    def testWriteTmpl(self):
    64      writer = util.Writer()
    65      writer.write_tmpl('$foo, $bar\n$baz', foo=1, bar=2, baz=3)
    66      self.assertEqual(writer.getvalue(), '1, 2\n3\n')
    67  
    68    def testIndent(self):
    69      writer = util.Writer()
    70      writer.indent(2)
    71      writer.write('foo')
    72      self.assertEqual(writer.getvalue(), '\t\tfoo\n')
    73  
    74    def testDedent(self):
    75      writer = util.Writer()
    76      writer.indent(4)
    77      writer.dedent(3)
    78      writer.write('foo')
    79      self.assertEqual(writer.getvalue(), '\tfoo\n')
    80  
    81  
    82  if __name__ == '__main__':
    83    unittest.main()