]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/yapftests/main_test.py
b5d9b926e0eb35f3b9672808c0885e36abf5416b
1 # -*- coding: utf-8 -*-
2 # Copyright 2015 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Tests for yapf.__init__.main."""
19 from contextlib
import contextmanager
20 from io
import StringIO
24 from yapftests
import yapf_test_helper
28 """IO is a thin wrapper around StringIO.
30 This is strictly to wrap the Python 3 StringIO object so that it can supply a
37 self
.string_io
= StringIO()
40 if isinstance(s
, bytes
):
42 self
.string_io
.write(s
)
45 return self
.string_io
.getvalue()
48 self
.buffer = self
.Buffer()
54 return self
.buffer.getvalue()
58 def captured_output():
59 new_out
, new_err
= IO(), IO()
60 old_out
, old_err
= sys
.stdout
, sys
.stderr
62 sys
.stdout
, sys
.stderr
= new_out
, new_err
63 yield sys
.stdout
, sys
.stderr
65 sys
.stdout
, sys
.stderr
= old_out
, old_err
69 def patched_input(code
):
70 """Monkey patch code as though it were coming from stdin."""
73 for line
in code
.splitlines():
77 def patch_raw_input(lines
=lines()):
81 orig_raw_import
= yapf
._raw
_input
82 yapf
._raw
_input
= patch_raw_input
85 yapf
._raw
_input
= orig_raw_import
88 class RunMainTest(yapf_test_helper
.YAPFTest
):
90 def testShouldHandleYapfError(self
):
91 """run_main should handle YapfError and sys.exit(1)."""
92 expected_message
= 'yapf: input filenames did not match any python files\n'
93 sys
.argv
= ['yapf', 'foo.c']
94 with
captured_output() as (out
, err
):
95 with self
.assertRaises(SystemExit):
97 self
.assertEqual(out
.getvalue(), '')
98 self
.assertEqual(err
.getvalue(), expected_message
)
101 class MainTest(yapf_test_helper
.YAPFTest
):
103 def testNoPythonFilesMatched(self
):
104 with self
.assertRaisesRegex(yapf
.errors
.YapfError
,
105 'did not match any python files'):
106 yapf
.main(['yapf', 'foo.c'])
108 def testEchoInput(self
):
109 code
= 'a = 1\nb = 2\n'
110 with
patched_input(code
):
111 with
captured_output() as (out
, _
):
113 self
.assertEqual(ret
, 0)
114 self
.assertEqual(out
.getvalue(), code
)
116 def testEchoInputWithStyle(self
):
117 code
= 'def f(a = 1\n\n):\n return 2*a\n'
118 yapf_code
= 'def f(a=1):\n return 2 * a\n'
119 with
patched_input(code
):
120 with
captured_output() as (out
, _
):
121 ret
= yapf
.main(['-', '--style=yapf'])
122 self
.assertEqual(ret
, 0)
123 self
.assertEqual(out
.getvalue(), yapf_code
)
125 def testEchoBadInput(self
):
126 bad_syntax
= ' a = 1\n'
127 with
patched_input(bad_syntax
):
128 with
captured_output() as (_
, _
):
129 with self
.assertRaisesRegex(yapf
.errors
.YapfError
, 'unexpected indent'):
133 with
captured_output() as (out
, _
):
134 ret
= yapf
.main(['-', '--style-help', '--style=pep8'])
135 self
.assertEqual(ret
, 0)
136 help_message
= out
.getvalue()
137 self
.assertIn('indent_width=4', help_message
)
138 self
.assertIn('The number of spaces required before a trailing comment.',