Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Tools / Scripts / webkitpy / formatter / main_unittest.py
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import StringIO
6 import unittest
7
8 from webkitpy.common.system.systemhost_mock import MockSystemHost
9 from webkitpy.formatter.main import main
10
11
12 ACTUAL_INPUT = '''
13 def foo():
14     """triple-quoted docstring"""
15     try:
16         bar = "bar"
17         long_list = ['this is a list of strings that should be wrapped', "and consistently quoted"]
18         longer_list = ['this is a list of strings that should be wrapped', "and consistently quoted", "because it's important to test quoting"]
19     except Exception, e:
20         pass
21 '''
22
23
24 EXPECTED_BLINK_OUTPUT = '''
25 def foo():
26     """triple-quoted docstring"""
27     try:
28         bar = 'bar'
29         long_list = ['this is a list of strings that should be wrapped', 'and consistently quoted']
30         longer_list = [
31             'this is a list of strings that should be wrapped',
32             'and consistently quoted',
33             "because it's important to test quoting"]
34     except Exception as e:
35         pass
36 '''
37
38
39 EXPECTED_CHROMIUM_OUTPUT = '''
40 def foo():
41   """triple-quoted docstring"""
42   try:
43     bar = 'bar'
44     long_list = [
45         'this is a list of strings that should be wrapped',
46         'and consistently quoted']
47     longer_list = [
48         'this is a list of strings that should be wrapped',
49         'and consistently quoted',
50         "because it's important to test quoting"]
51   except Exception as e:
52     pass
53 '''
54
55 EXPECTED_ONLY_DOUBLE_QUOTED_OUTPUT = '''
56 def foo():
57     """triple-quoted docstring"""
58     try:
59         bar = "bar"
60         long_list = ["this is a list of strings that should be wrapped", "and consistently quoted"]
61         longer_list = ["this is a list of strings that should be wrapped", "and consistently quoted", "because it's important to test quoting"]
62     except Exception, e:
63         pass
64 '''
65
66
67 class TestMain(unittest.TestCase):
68     maxDiff = 4096
69
70     def test_files_blink(self):
71         host = MockSystemHost()
72         host.filesystem.files = {
73             'test.py': ACTUAL_INPUT}
74         main(host, ['test.py'])
75         self.assertEqual(host.filesystem.files, {
76             'test.py': EXPECTED_BLINK_OUTPUT,
77             'test.py.bak': ACTUAL_INPUT})
78
79     def test_files_blink_no_backup(self):
80         host = MockSystemHost()
81         host.filesystem.files = {
82             'test.py': ACTUAL_INPUT}
83         main(host, ['--no-backups', 'test.py'])
84         self.assertEqual(host.filesystem.files, {
85             'test.py': EXPECTED_BLINK_OUTPUT})
86
87     def test_stdin_blink(self):
88         host = MockSystemHost()
89         host.stdin = StringIO.StringIO(ACTUAL_INPUT)
90         main(host, ['-'])
91         self.assertMultiLineEqual(host.stdout.getvalue(), EXPECTED_BLINK_OUTPUT)
92
93     def test_stdin_chromium(self):
94         host = MockSystemHost()
95         host.stdin = StringIO.StringIO(ACTUAL_INPUT)
96         main(host, ['--chromium', '-'])
97         self.assertMultiLineEqual(host.stdout.getvalue(), EXPECTED_CHROMIUM_OUTPUT)
98
99     def test_stdin_no_changes(self):
100         host = MockSystemHost()
101         host.stdin = StringIO.StringIO(ACTUAL_INPUT)
102         main(host, ['--no-autopep8', '--leave-strings-alone', '-'])
103         self.assertMultiLineEqual(host.stdout.getvalue(), ACTUAL_INPUT)
104
105     def test_stdin_only_double_quoting(self):
106         host = MockSystemHost()
107         host.stdin = StringIO.StringIO(ACTUAL_INPUT)
108         main(host, ['--no-autopep8', '--double-quote-strings', '-'])
109         self.assertMultiLineEqual(host.stdout.getvalue(), EXPECTED_ONLY_DOUBLE_QUOTED_OUTPUT)