class TestQuotes(unittest.TestCase): def setUp(self): self.target = generator self.output = BaseIO() def test_single_quotes(self): self.target(BaseIO('\'abc "123" d d a\''), self.output) self.assertEqual('abc "123" d d a\n', self.output.getvalue()) def test_nested_single_quotes(self): self.target(BaseIO('\'abc \\\'123\\\' d d a\''), self.output) self.assertEqual('abc \'123\' d d a\n', self.output.getvalue()) def test_double_quotes(self): self.target(BaseIO('"abc \'123\' d d a"'), self.output) self.assertEqual('abc \'123\' d d a\n', self.output.getvalue()) def test_nested_double_quotes(self): self.target(BaseIO('"abc \\"123\\" d d a"'), self.output) self.assertEqual('abc "123" d d a\n', self.output.getvalue()) def test_back_quotes(self): self.target(BaseIO('` 1 + 2 * 3 `'), self.output) self.assertEqual('7\n', self.output.getvalue()) def test_nested_back_quotes(self): self.target(BaseIO('` "`aiueo`" `'), self.output) self.assertEqual('`aiueo`\n', self.output.getvalue()) self.output.truncate(0) self.output.seek(0) self.target(BaseIO('` \'`aiueo`\' `'), self.output) self.assertEqual('`aiueo`\n', self.output.getvalue())
class TestType(unittest.TestCase): def setUp(self): self.target = generator self.output = BaseIO() def test_type_int(self): self.target(BaseIO("1 2 3"), self.output) self.assertEqual("1 2 3\n", self.output.getvalue()) def test_type_complex(self): self.target(BaseIO("1+1j 2+3j 3+1j"), self.output) self.assertEqual("(1+1j) (2+3j) (3+1j)\n", self.output.getvalue()) def test_type_bool(self): self.target(BaseIO("True False 1==1 1!=1"), self.output) self.assertEqual("True False True False\n", self.output.getvalue()) def test_type_str(self): self.target(BaseIO("'a' 'b' 'c'"), self.output) self.assertEqual("a b c\n", self.output.getvalue()) def test_type_float(self): self.target(BaseIO("1.0 2.0 3. .1"), self.output) self.assertEqual("1.0 2.0 3.0 0.1\n", self.output.getvalue()) @unittest.skipUnless(platform.python_version_tuple()[0] == '2', "unicode exists only in Python2") def test_type_unicode(self): self.target(BaseIO("unicode('ab') u'b'"), self.output) self.assertEqual("ab b\n", self.output.getvalue()) @unittest.skipUnless(platform.python_version_tuple()[0] == '2', "This test only in Python2") def test_type_bytes_2(self): self.target(BaseIO("bytes('a') b'b'"), self.output) self.assertEqual("a b\n", self.output.getvalue()) @unittest.skipUnless(platform.python_version_tuple()[0] == '3', "This test only in Python3") def test_type_bytes_3(self): self.target(BaseIO("'ab'.encode('utf-8') b'b'"), self.output) self.assertEqual("ab b\n", self.output.getvalue()) def test_type_None(self): self.target(BaseIO("None None"), self.output) self.assertEqual(" \n", self.output.getvalue())
class TestSyntax(unittest.TestCase): def setUp(self): self.target = generator self.output = BaseIO() def test_tab_split(self): self.target(BaseIO("1\t2\t3"), self.output) self.assertEqual("1\t2\t3\n", self.output.getvalue()) def test_skip_comment(self): self.target(BaseIO("1 2 3 [2] # comment"), self.output) self.assertEqual("1 2 3 2\n", self.output.getvalue()) self.output.truncate(0) self.output.seek(0) self.target(BaseIO(" # comment"), self.output) self.assertEqual("\n", self.output.getvalue()) self.output.truncate(0) self.output.seek(0) self.target(BaseIO("# comment ## comment"), self.output) self.assertEqual("\n", self.output.getvalue()) def test_python_statement(self): self.target(BaseIO("% A = 1"), self.output) self.assertEqual("", self.output.getvalue()) self.output.truncate(0) self.output.seek(0) self.target(BaseIO("% x = 1\nx x"), self.output) self.assertEqual("1 1\n", self.output.getvalue()) self.output.truncate(0) self.output.seek(0) self.target(BaseIO("% print(1)"), self.output) self.assertEqual("1\n", self.output.getvalue()) self.output.truncate(0) self.output.seek(0) self.target(BaseIO("%"), self.output) self.assertEqual("", self.output.getvalue())
class TestBracket(unittest.TestCase): def setUp(self): self.target = generator self.output = BaseIO() def test_brackets_list(self): self.target(BaseIO("[1, 2, 3]"), self.output) self.assertEqual("1 2 3\n", self.output.getvalue()) def test_brackets_nested_list(self): self.target(BaseIO("[1, [4, [5, 6]], 3]"), self.output) self.assertEqual("1 4 5 6 3\n", self.output.getvalue()) def test_brackets_generator_list(self): self.target(BaseIO("[i**2 for i in range(4)]"), self.output) self.assertEqual("0 1 4 9\n", self.output.getvalue()) def test_brackets_caluculate_list(self): self.target(BaseIO("[7]+[1, 2]*2+[6]"), self.output) self.assertEqual("7 1 2 1 2 6\n", self.output.getvalue()) def test_brackets_array_ref(self): self.target(BaseIO("[1, 2, 3][ 1 ]"), self.output) self.assertEqual("2\n", self.output.getvalue()) def test_brackets_in_string(self): self.target(BaseIO("[1, '] [', 3]"), self.output) self.assertEqual("1 ] [ 3\n", self.output.getvalue()) def test_brackets_empty(self): self.target(BaseIO("[]"), self.output) self.assertEqual("\n", self.output.getvalue()) def test_parenthesis_tuple(self): self.target(BaseIO("(1, 2, 3)"), self.output) self.assertEqual("1 2 3\n", self.output.getvalue()) def test_parenthesis_nested_tuple(self): self.target(BaseIO("(1, (4, (5, 6)), 3)"), self.output) self.assertEqual("1 4 5 6 3\n", self.output.getvalue()) def test_parenthesis_function_call(self): self.target(BaseIO("int( 1.8 )"), self.output) self.assertEqual("1\n", self.output.getvalue()) def test_parenthesis_generator(self): self.target(BaseIO("(i**2 for i in range(4))"), self.output) self.assertEqual("0 1 4 9\n", self.output.getvalue()) def test_parenthesis_in_string(self): self.target(BaseIO("(1, ') (', 3)"), self.output) self.assertEqual("1 ) ( 3\n", self.output.getvalue()) def test_parenthesis_empty(self): self.target(BaseIO("()"), self.output) self.assertEqual("\n", self.output.getvalue()) @unittest.skipIf(sys.version_info < (2, 7), 'Set literals is not supported in version < 2.7.') def test_brace_set(self): self.target(BaseIO("{1, 1}"), self.output) self.assertEqual("1\n", self.output.getvalue()) def test_brace_dict(self): self.target(BaseIO("{1: 2, 3: 4}"), self.output) self.assertEqual("1 3\n", self.output.getvalue()) @unittest.skipIf(sys.version_info < (2, 7), 'Set literals is not supported in version < 2.7.') def test_brace_generator_set(self): self.target(BaseIO("{i**2 for i in range(4)}"), self.output) self.assertEqual(set([0,1,4,9]), set(map(int, self.output.getvalue().split()))) @unittest.skipIf(sys.version_info < (2, 7), 'Set literals is not supported in version < 2.7.') def test_brace_set_in_string(self): self.target(BaseIO("{ '} {' }"), self.output) self.assertEqual("} {\n", self.output.getvalue()) def test_brace_empty(self): self.target(BaseIO("{} set()"), self.output) self.assertEqual(" \n", self.output.getvalue()) @unittest.skipIf(sys.version_info < (2, 7), 'Set literals is not supported in version < 2.7.') def test_mixed_nested_bracket(self): self.target(BaseIO("(1, [4, {6}, (5, 8)], 3)"), self.output) self.assertEqual("1 4 6 5 8 3\n", self.output.getvalue()) def test_mixed_nested_bracket_2_6(self): self.target(BaseIO("(1, [4, [6], (5, 8)], 3)"), self.output) self.assertEqual("1 4 6 5 8 3\n", self.output.getvalue())