|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import pytest |
| 5 | +import textwrap |
| 6 | + |
| 7 | +import normalizeForInterpreter |
| 8 | + |
| 9 | + |
| 10 | +class TestNormalizationScript(object): |
| 11 | + """Basic unit tests for the normalization script.""" |
| 12 | + |
| 13 | + def test_basicNormalization(self, capsys): |
| 14 | + src = 'print("this is a test")' |
| 15 | + normalizeForInterpreter.normalize_lines(src) |
| 16 | + captured = capsys.readouterr() |
| 17 | + assert captured.out == src |
| 18 | + |
| 19 | + def test_moreThanOneLine(self, capsys): |
| 20 | + src = textwrap.dedent("""\ |
| 21 | + # Some rando comment |
| 22 | +
|
| 23 | + def show_something(): |
| 24 | + print("Something") |
| 25 | + """ |
| 26 | + ) |
| 27 | + normalizeForInterpreter.normalize_lines(src) |
| 28 | + captured = capsys.readouterr() |
| 29 | + assert captured.out == src |
| 30 | + |
| 31 | + def test_withHangingIndent(self, capsys): |
| 32 | + src = textwrap.dedent("""\ |
| 33 | + x = 22 |
| 34 | + y = 30 |
| 35 | + z = -10 |
| 36 | + result = x + y + z |
| 37 | +
|
| 38 | + if result == 42: |
| 39 | + print("The answer to life, the universe, and everything") |
| 40 | + """ |
| 41 | + ) |
| 42 | + normalizeForInterpreter.normalize_lines(src) |
| 43 | + captured = capsys.readouterr() |
| 44 | + assert captured.out == src |
| 45 | + |
| 46 | + def test_clearOutExtraneousNewlines(self, capsys): |
| 47 | + src = textwrap.dedent("""\ |
| 48 | + value_x = 22 |
| 49 | +
|
| 50 | + value_y = 30 |
| 51 | +
|
| 52 | + value_z = -10 |
| 53 | +
|
| 54 | + print(value_x + value_y + value_z) |
| 55 | +
|
| 56 | + """ |
| 57 | + ) |
| 58 | + expectedResult = textwrap.dedent("""\ |
| 59 | + value_x = 22 |
| 60 | + value_y = 30 |
| 61 | + value_z = -10 |
| 62 | + print(value_x + value_y + value_z) |
| 63 | +
|
| 64 | + """ |
| 65 | + ) |
| 66 | + normalizeForInterpreter.normalize_lines(src) |
| 67 | + result = capsys.readouterr() |
| 68 | + assert result.out == expectedResult |
| 69 | + |
| 70 | + def test_clearOutExtraLinesAndWhitespace(self, capsys): |
| 71 | + src = textwrap.dedent("""\ |
| 72 | + if True: |
| 73 | + x = 22 |
| 74 | +
|
| 75 | + y = 30 |
| 76 | +
|
| 77 | + z = -10 |
| 78 | +
|
| 79 | + print(x + y + z) |
| 80 | +
|
| 81 | + """ |
| 82 | + ) |
| 83 | + expectedResult = textwrap.dedent("""\ |
| 84 | + if True: |
| 85 | + x = 22 |
| 86 | + y = 30 |
| 87 | + z = -10 |
| 88 | +
|
| 89 | + print(x + y + z) |
| 90 | +
|
| 91 | + """ |
| 92 | + ) |
| 93 | + normalizeForInterpreter.normalize_lines(src) |
| 94 | + result = capsys.readouterr() |
| 95 | + assert result.out == expectedResult |
0 commit comments