|
| 1 | +/******************************************************************************* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2022, Jean-David Gadina - www.xs-labs.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the Software), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +import Foundation |
| 26 | +import Testing |
| 27 | + |
| 28 | +@Suite( "FormatterOutcome.classify" ) |
| 29 | +struct FormatterOutcomeTests |
| 30 | +{ |
| 31 | + private func data( _ string: String ) -> Data |
| 32 | + { |
| 33 | + Data( string.utf8 ) |
| 34 | + } |
| 35 | + |
| 36 | + @Test( "Returns .formatted when output differs and is non-empty" ) |
| 37 | + func formattedWhenChanged() throws |
| 38 | + { |
| 39 | + let outcome = try FormatterOutcome.classify( |
| 40 | + executable: "swiftformat", |
| 41 | + input: "let x=1", |
| 42 | + status: 0, |
| 43 | + standardOutput: self.data( "let x = 1\n" ), |
| 44 | + standardError: Data() |
| 45 | + ) |
| 46 | + |
| 47 | + #expect( outcome == .formatted( "let x = 1\n" ) ) |
| 48 | + } |
| 49 | + |
| 50 | + @Test( "Returns .unchanged when output equals input" ) |
| 51 | + func unchangedWhenIdentical() throws |
| 52 | + { |
| 53 | + let outcome = try FormatterOutcome.classify( |
| 54 | + executable: "swiftformat", |
| 55 | + input: "let x = 1\n", |
| 56 | + status: 0, |
| 57 | + standardOutput: self.data( "let x = 1\n" ), |
| 58 | + standardError: Data() |
| 59 | + ) |
| 60 | + |
| 61 | + #expect( outcome == .unchanged ) |
| 62 | + } |
| 63 | + |
| 64 | + @Test( "Returns .unchanged when output is empty after trimming" ) |
| 65 | + func unchangedWhenBlankOutput() throws |
| 66 | + { |
| 67 | + let outcome = try FormatterOutcome.classify( |
| 68 | + executable: "swiftformat", |
| 69 | + input: "let x = 1\n", |
| 70 | + status: 0, |
| 71 | + standardOutput: self.data( " \n\t " ), |
| 72 | + standardError: Data() |
| 73 | + ) |
| 74 | + |
| 75 | + #expect( outcome == .unchanged ) |
| 76 | + } |
| 77 | + |
| 78 | + @Test( "Returns .unchanged when output is completely empty" ) |
| 79 | + func unchangedWhenEmptyOutput() throws |
| 80 | + { |
| 81 | + let outcome = try FormatterOutcome.classify( |
| 82 | + executable: "swiftformat", |
| 83 | + input: "let x = 1\n", |
| 84 | + status: 0, |
| 85 | + standardOutput: Data(), |
| 86 | + standardError: Data() |
| 87 | + ) |
| 88 | + |
| 89 | + #expect( outcome == .unchanged ) |
| 90 | + } |
| 91 | + |
| 92 | + @Test( "Throws .failed with stderr text on a non-zero status" ) |
| 93 | + func throwsOnNonZeroStatus() |
| 94 | + { |
| 95 | + #expect( throws: FormatterError.failed( executable: "uncrustify", status: 1, message: "bad config at line 3" ) ) |
| 96 | + { |
| 97 | + try FormatterOutcome.classify( |
| 98 | + executable: "uncrustify", |
| 99 | + input: "int x;", |
| 100 | + status: 1, |
| 101 | + standardOutput: Data(), |
| 102 | + standardError: self.data( "bad config at line 3" ) |
| 103 | + ) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments