1 #!/usr/bin/env python 2 # 3 # Copyright 2010 Google Inc. 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 18 """Tests for protorpc.generate.""" 19 20 __author__ = 'rafek (at] google.com (Rafe Kaplan)' 21 22 import operator 23 24 import cStringIO 25 import sys 26 import unittest 27 28 from protorpc import generate 29 from protorpc import test_util 30 31 32 class ModuleInterfaceTest(test_util.ModuleInterfaceTest, 33 test_util.TestCase): 34 35 MODULE = generate 36 37 38 class IndentWriterTest(test_util.TestCase): 39 40 def setUp(self): 41 self.out = cStringIO.StringIO() 42 self.indent_writer = generate.IndentWriter(self.out) 43 44 def testWriteLine(self): 45 self.indent_writer.write_line('This is a line') 46 self.indent_writer.write_line('This is another line') 47 48 self.assertEquals('This is a line\n' 49 'This is another line\n', 50 self.out.getvalue()) 51 52 def testLeftShift(self): 53 self.run_count = 0 54 def mock_write_line(line): 55 self.run_count += 1 56 self.assertEquals('same as calling write_line', line) 57 58 self.indent_writer.write_line = mock_write_line 59 self.indent_writer << 'same as calling write_line' 60 self.assertEquals(1, self.run_count) 61 62 def testIndentation(self): 63 self.indent_writer << 'indent 0' 64 self.indent_writer.begin_indent() 65 self.indent_writer << 'indent 1' 66 self.indent_writer.begin_indent() 67 self.indent_writer << 'indent 2' 68 self.indent_writer.end_indent() 69 self.indent_writer << 'end 2' 70 self.indent_writer.end_indent() 71 self.indent_writer << 'end 1' 72 self.assertRaises(generate.IndentationError, 73 self.indent_writer.end_indent) 74 75 self.assertEquals('indent 0\n' 76 ' indent 1\n' 77 ' indent 2\n' 78 ' end 2\n' 79 'end 1\n', 80 self.out.getvalue()) 81 82 def testBlankLine(self): 83 self.indent_writer << '' 84 self.indent_writer.begin_indent() 85 self.indent_writer << '' 86 self.assertEquals('\n\n', self.out.getvalue()) 87 88 def testNoneInvalid(self): 89 self.assertRaises( 90 TypeError, operator.lshift, self.indent_writer, None) 91 92 def testAltIndentation(self): 93 self.indent_writer = generate.IndentWriter(self.out, indent_space=3) 94 self.indent_writer << 'indent 0' 95 self.assertEquals(0, self.indent_writer.indent_level) 96 self.indent_writer.begin_indent() 97 self.indent_writer << 'indent 1' 98 self.assertEquals(1, self.indent_writer.indent_level) 99 self.indent_writer.begin_indent() 100 self.indent_writer << 'indent 2' 101 self.assertEquals(2, self.indent_writer.indent_level) 102 self.indent_writer.end_indent() 103 self.indent_writer << 'end 2' 104 self.assertEquals(1, self.indent_writer.indent_level) 105 self.indent_writer.end_indent() 106 self.indent_writer << 'end 1' 107 self.assertEquals(0, self.indent_writer.indent_level) 108 self.assertRaises(generate.IndentationError, 109 self.indent_writer.end_indent) 110 self.assertEquals(0, self.indent_writer.indent_level) 111 112 self.assertEquals('indent 0\n' 113 ' indent 1\n' 114 ' indent 2\n' 115 ' end 2\n' 116 'end 1\n', 117 self.out.getvalue()) 118 119 def testIndent(self): 120 self.indent_writer << 'indent 0' 121 self.assertEquals(0, self.indent_writer.indent_level) 122 123 def indent1(): 124 self.indent_writer << 'indent 1' 125 self.assertEquals(1, self.indent_writer.indent_level) 126 127 def indent2(): 128 self.indent_writer << 'indent 2' 129 self.assertEquals(2, self.indent_writer.indent_level) 130 test_util.do_with(self.indent_writer.indent(), indent2) 131 132 self.assertEquals(1, self.indent_writer.indent_level) 133 self.indent_writer << 'end 2' 134 test_util.do_with(self.indent_writer.indent(), indent1) 135 136 self.assertEquals(0, self.indent_writer.indent_level) 137 self.indent_writer << 'end 1' 138 139 self.assertEquals('indent 0\n' 140 ' indent 1\n' 141 ' indent 2\n' 142 ' end 2\n' 143 'end 1\n', 144 self.out.getvalue()) 145 146 147 def main(): 148 unittest.main() 149 150 151 if __name__ == '__main__': 152 main() 153