Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(383)

Unified Diff: tools/json_schema_compiler/code_test.py

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: a fistful of rework Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/json_schema_compiler/code_test.py
diff --git a/tools/json_schema_compiler/code_test.py b/tools/json_schema_compiler/code_test.py
new file mode 100644
index 0000000000000000000000000000000000000000..b42eb8ee3e3995cb57a8ce2720932989708bc024
--- /dev/null
+++ b/tools/json_schema_compiler/code_test.py
@@ -0,0 +1,139 @@
+# 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.
+
+import unittest
+from code import Code
+
+class CodeTest(unittest.TestCase):
+ def test_append(self):
+ c = Code()
+ c.append('line')
+ self.assertEquals(c.render(), 'line')
+
+ def test_block(self):
+ c = Code()
+ (c.append('line')
+ .sblock('sblock')
+ .append('inner')
+ .append('moreinner')
+ .sblock('moresblock')
+ .append('inner')
+ .eblock('out')
+ .append('inner')
+ .eblock('out')
+ )
+ self.assertEquals(c.render(),
+ 'line\n'
+ 'sblock\n'
+ ' inner\n'
+ ' moreinner\n'
+ ' moresblock\n'
+ ' inner\n'
+ ' out\n'
+ ' inner\n'
+ 'out')
+
+ def test_concat(self):
+ b = Code()
+ (b.sblock('2')
+ .append('2')
+ .eblock('2')
+ )
+ c = Code()
+ (c.sblock('1')
+ .concat(b)
+ .append('1')
+ .eblock('1')
+ )
+ self.assertEquals(c.render(),
+ '1\n'
+ ' 2\n'
+ ' 2\n'
+ ' 2\n'
+ ' 1\n'
+ '1')
+ d = Code()
+ a = Code()
+ a.concat(d)
+ self.assertEquals(a.render(), '')
+ a.concat(c)
+ self.assertEquals(a.render(),
+ '1\n'
+ ' 2\n'
+ ' 2\n'
+ ' 2\n'
+ ' 1\n'
+ '1')
+
+ def test_concat_errors(self):
+ c = Code()
+ d = Code()
+ d.append('%s')
+ self.assertRaises(TypeError, c.concat, d)
+ d = Code()
+ d.append('%(classname)s')
+ self.assertRaises(TypeError, c.concat, d)
+ d = 'line of code'
+ self.assertRaises(TypeError, c.concat, d)
+
+ def test_substitute(self):
+ c = Code()
+ c.append('%(var1)s %(var2)s %(var1)s')
+ c.substitute({'var1': 'one', 'var2': 'two'})
+ self.assertEquals(c.render(), 'one two one')
+ c.append('%(var1)s %(var2)s %(var3)s')
+ c.append('%(var2)s %(var1)s %(var3)s')
+ c.substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'})
+ self.assertEquals(c.render(),
+ 'one two one\n'
+ 'one two three\n'
+ 'two one three')
+
+ def test_substitute_errors(self):
+ # No unnamed placeholders allowed when substitute is run
+ c = Code()
+ c.append('%s %s')
+ self.assertRaises(TypeError, c.substitute, ('var1', 'one'))
+ c = Code()
+ c.append('%s %(var1)s')
+ self.assertRaises(TypeError, c.substitute, {'var1': 'one'})
+ c = Code()
+ c.append('%s %(var1)s')
+ self.assertRaises(TypeError, c.substitute, {'var1': 'one'})
+ c = Code()
+ c.append('%(var1)s')
+ self.assertRaises(KeyError, c.substitute, {'clearlynotvar1': 'one'})
+
+ def test_comment(self):
+ long_comment = ('This comment is eighty nine characters in longness, '
+ 'that is, to use another word, length')
+ c = Code()
+ c.comment(long_comment)
+ self.assertEquals(c.render(),
+ '// This comment is eighty nine characters '
+ 'in longness, that is, to use another\n'
+ '// word, length')
+ c = Code()
+ c.sblock('sblock')
+ c.comment(long_comment)
+ c.eblock('eblock')
+ c.comment(long_comment)
+ self.assertEquals(c.render(),
+ 'sblock\n'
+ ' // This comment is eighty nine characters '
+ 'in longness, that is, to use\n'
+ ' // another word, length\n'
+ 'eblock\n'
+ '// This comment is eighty nine characters in '
+ 'longness, that is, to use another\n'
+ '// word, length')
+ long_word = 'x' * 100
+ c = Code()
+ c.comment(long_word)
+ self.assertEquals(c.render(),
+ '// ' + 'x' * 77 + '\n'
+ '// ' + 'x' * 23)
+
+if __name__ == '__main__':
+ unittest.main()

Powered by Google App Engine
This is Rietveld 408576698