|
| 1 | +import os |
| 2 | + |
| 3 | +from wpiformat.config import Config |
| 4 | +from wpiformat.bracenewline import BraceNewline |
| 5 | + |
| 6 | + |
| 7 | +def test_bracenewline(): |
| 8 | + task = BraceNewline() |
| 9 | + |
| 10 | + inputs = [] |
| 11 | + outputs = [] |
| 12 | + |
| 13 | + # Brackets on same line |
| 14 | + inputs.append(("./Test.cpp", |
| 15 | + "void func1() {}" + os.linesep + \ |
| 16 | + "void func2() {}" + os.linesep)) |
| 17 | + outputs.append(( |
| 18 | + "void func1() {}" + os.linesep + \ |
| 19 | + os.linesep + \ |
| 20 | + "void func2() {}" + os.linesep, True, True)) |
| 21 | + |
| 22 | + # Brackets on next line |
| 23 | + inputs.append(("./Test.cpp", |
| 24 | + "void func1() {" + os.linesep + \ |
| 25 | + "}" + os.linesep + \ |
| 26 | + "void func2() {" + os.linesep + \ |
| 27 | + "}" + os.linesep)) |
| 28 | + outputs.append(( |
| 29 | + "void func1() {" + os.linesep + \ |
| 30 | + "}" + os.linesep + \ |
| 31 | + os.linesep + \ |
| 32 | + "void func2() {" + os.linesep + \ |
| 33 | + "}" + os.linesep, True, True)) |
| 34 | + |
| 35 | + # Comments after brackets |
| 36 | + inputs.append(("./Test.cpp", |
| 37 | + "void func1() {" + os.linesep + \ |
| 38 | + "} // This is a comment" + os.linesep + \ |
| 39 | + "void func2() {" + os.linesep + \ |
| 40 | + "} /* This is a comment */" + os.linesep + \ |
| 41 | + "void func3() {" + os.linesep + \ |
| 42 | + "}" + os.linesep)) |
| 43 | + outputs.append(( |
| 44 | + "void func1() {" + os.linesep + \ |
| 45 | + "} // This is a comment" + os.linesep + \ |
| 46 | + os.linesep + \ |
| 47 | + "void func2() {" + os.linesep + \ |
| 48 | + "} /* This is a comment */" + os.linesep + \ |
| 49 | + os.linesep + \ |
| 50 | + "void func3() {" + os.linesep + \ |
| 51 | + "}" + os.linesep, True, True)) |
| 52 | + |
| 53 | + # Don't add line separators to uncondensed if statements (after last brace |
| 54 | + # is OK) |
| 55 | + inputs.append(("./Test.cpp", |
| 56 | + "void func1() {" + os.linesep + \ |
| 57 | + " if (1) {" + os.linesep + \ |
| 58 | + " }" + os.linesep + \ |
| 59 | + " else {" + os.linesep + \ |
| 60 | + " }" + os.linesep + \ |
| 61 | + "}" + os.linesep)) |
| 62 | + outputs.append((inputs[len(inputs) - 1][1], False, True)) |
| 63 | + |
| 64 | + # Don't add line separators to condensed if statements (after last brace |
| 65 | + # is OK) |
| 66 | + inputs.append(("./Test.cpp", |
| 67 | + "void func1() {" + os.linesep + \ |
| 68 | + " if (1) {" + os.linesep + \ |
| 69 | + " } else if () {" + os.linesep + \ |
| 70 | + " } else {" + os.linesep + \ |
| 71 | + " // comment" + os.linesep + \ |
| 72 | + " }" + os.linesep + \ |
| 73 | + "}" + os.linesep)) |
| 74 | + outputs.append((inputs[len(inputs) - 1][1], False, True)) |
| 75 | + |
| 76 | + # Don't add line separators before #endif statements |
| 77 | + inputs.append(("./Main.cpp", |
| 78 | + "using decay_t = typename decay<T>::type;" + os.linesep + \ |
| 79 | + "} // namespace std" + os.linesep + \ |
| 80 | + "#endif" + os.linesep)) |
| 81 | + outputs.append((inputs[len(inputs) - 1][1], False, True)) |
| 82 | + |
| 83 | + # Don't insert line separators within multiline comments |
| 84 | + inputs.append(("./Main.java", |
| 85 | + "/* to fine tune the pid loop." + os.linesep + \ |
| 86 | + " *" + os.linesep + \ |
| 87 | + " * @return the {@link PIDController} used by this {@link PIDSubsystem}" + os.linesep + \ |
| 88 | + " */" + os.linesep + \ |
| 89 | + "public PIDController getPIDController() {" + os.linesep)) |
| 90 | + outputs.append((inputs[len(inputs) - 1][1], False, True)) |
| 91 | + |
| 92 | + # Don't insert line separators within single line comments |
| 93 | + inputs.append(("./Main.java", |
| 94 | + "// @return the {@link PIDController} used by this {@link PIDSubsystem}" + os.linesep + \ |
| 95 | + "public PIDController getPIDController() {" + os.linesep)) |
| 96 | + outputs.append((inputs[len(inputs) - 1][1], False, True)) |
| 97 | + |
| 98 | + assert len(inputs) == len(outputs) |
| 99 | + |
| 100 | + config_file = Config(os.path.abspath(os.getcwd()), ".styleguide") |
| 101 | + for i in range(len(inputs)): |
| 102 | + output, file_changed, success = task.run_pipeline( |
| 103 | + config_file, inputs[i][0], inputs[i][1]) |
| 104 | + assert output == outputs[i][0] |
| 105 | + assert file_changed == outputs[i][1] |
| 106 | + assert success == outputs[i][2] |
0 commit comments