This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathutil_test.py
83 lines (66 loc) · 2.43 KB
/
util_test.py
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
79
80
81
82
83
# coding=utf-8
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests Writer and other utils."""
from __future__ import unicode_literals
import unittest
from grumpy.compiler import block
from grumpy.compiler import imputil
from grumpy.compiler import util
class WriterTest(unittest.TestCase):
def testIndentBlock(self):
writer = util.Writer()
writer.write('foo')
with writer.indent_block(n=2):
writer.write('bar')
writer.write('baz')
self.assertEqual(writer.getvalue(), 'foo\n\t\tbar\nbaz\n')
def testWriteBlock(self):
writer = util.Writer()
mod_block = block.ModuleBlock(None, '__main__', '<test>', '',
imputil.FutureFeatures())
writer.write_block(mod_block, 'BODY')
output = writer.getvalue()
dispatch = 'switch πF.State() {\n\tcase 0:\n\tdefault: panic'
self.assertIn(dispatch, output)
def testWriteMultiline(self):
writer = util.Writer()
writer.indent(2)
writer.write('foo\nbar\nbaz\n')
self.assertEqual(writer.getvalue(), '\t\tfoo\n\t\tbar\n\t\tbaz\n')
def testWritePyContext(self):
writer = util.Writer()
writer.write_py_context(12, 'print "foo"')
self.assertEqual(writer.getvalue(), '// line 12: print "foo"\n')
def testWriteSkipBlankLine(self):
writer = util.Writer()
writer.write('foo\n\nbar')
self.assertEqual(writer.getvalue(), 'foo\nbar\n')
def testWriteTmpl(self):
writer = util.Writer()
writer.write_tmpl('$foo, $bar\n$baz', foo=1, bar=2, baz=3)
self.assertEqual(writer.getvalue(), '1, 2\n3\n')
def testIndent(self):
writer = util.Writer()
writer.indent(2)
writer.write('foo')
self.assertEqual(writer.getvalue(), '\t\tfoo\n')
def testDedent(self):
writer = util.Writer()
writer.indent(4)
writer.dedent(3)
writer.write('foo')
self.assertEqual(writer.getvalue(), '\tfoo\n')
if __name__ == '__main__':
unittest.main()