-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from diffplug/feat/carriage-return
Carriage return handling
- Loading branch information
Showing
8 changed files
with
252 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
selfie-lib/src/jvmTest/kotlin/com/diffplug/selfie/LineReaderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2023 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.selfie | ||
|
||
import io.kotest.matchers.shouldBe | ||
import kotlin.test.Test | ||
|
||
class LineReaderTest { | ||
|
||
@Test | ||
fun shouldFindUnixSeparatorFromBinary() { | ||
val reader = LineReader.forBinary("This is a new line\n".encodeToByteArray()) | ||
reader.unixNewlines() shouldBe true | ||
reader.readLine() shouldBe "This is a new line" | ||
} | ||
|
||
@Test | ||
fun shouldFindWindowsSeparatorFromBinary() { | ||
val reader = LineReader.forBinary("This is a new line\r\n".encodeToByteArray()) | ||
reader.unixNewlines() shouldBe false | ||
reader.readLine() shouldBe "This is a new line" | ||
} | ||
|
||
@Test | ||
fun shouldFindUnixSeparatorFromString() { | ||
val reader = LineReader.forString("This is a new line\n") | ||
reader.unixNewlines() shouldBe true | ||
reader.readLine() shouldBe "This is a new line" | ||
} | ||
|
||
@Test | ||
fun shouldFindWindowsSeparatorFromString() { | ||
val reader = LineReader.forString("This is a new line\r\n") | ||
reader.unixNewlines() shouldBe false | ||
reader.readLine() shouldBe "This is a new line" | ||
} | ||
|
||
@Test | ||
fun shouldGetUnixLineSeparatorWhenThereIsNone() { | ||
val reader = LineReader.forBinary("This is a new line".encodeToByteArray()) | ||
reader.unixNewlines() shouldBe true | ||
reader.readLine() shouldBe "This is a new line" | ||
} | ||
|
||
@Test | ||
fun shouldReadNextLineWithoutProblem() { | ||
val reader = LineReader.forBinary("First\r\nSecond\r\n".encodeToByteArray()) | ||
reader.unixNewlines() shouldBe false | ||
reader.readLine() shouldBe "First" | ||
reader.unixNewlines() shouldBe false | ||
reader.readLine() shouldBe "Second" | ||
reader.unixNewlines() shouldBe false | ||
} | ||
|
||
@Test | ||
fun shouldUseFirstLineSeparatorAndIgnoreNext() { | ||
val reader = LineReader.forBinary("First\r\nAnother separator\n".encodeToByteArray()) | ||
reader.unixNewlines() shouldBe false | ||
reader.readLine() shouldBe "First" | ||
reader.unixNewlines() shouldBe false | ||
reader.readLine() shouldBe "Another separator" | ||
reader.unixNewlines() shouldBe false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/CarriageReturnTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (C) 2023 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.selfie.junit5 | ||
|
||
import kotlin.test.Test | ||
import org.junit.jupiter.api.MethodOrderer | ||
import org.junit.jupiter.api.Order | ||
import org.junit.jupiter.api.TestMethodOrder | ||
import org.junitpioneer.jupiter.DisableIfTestFails | ||
|
||
/** Verify selfie's carriage-return handling. */ | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation::class) | ||
@DisableIfTestFails | ||
class CarriageReturnTest : Harness("undertest-junit5") { | ||
@Test @Order(1) | ||
fun noSelfie() { | ||
ut_snapshot().deleteIfExists() | ||
ut_snapshot().assertDoesNotExist() | ||
} | ||
val expectedContent = | ||
""" | ||
╔═ git_makes_carriage_returns_unrepresentable ═╗ | ||
hard | ||
to | ||
preserve | ||
this | ||
╔═ [end of file] ═╗ | ||
""" | ||
.trimIndent() | ||
|
||
@Test @Order(2) | ||
fun write_and_assert_ss_has_unix_newlines() { | ||
gradleWriteSS() | ||
ut_snapshot().assertContent(expectedContent) | ||
} | ||
|
||
@Test @Order(3) | ||
fun if_ss_has_cr_then_it_will_stay_cr() { | ||
val contentWithCr = expectedContent.replace("\n", "\r\n") | ||
ut_snapshot().setContent(contentWithCr) | ||
gradleWriteSS() | ||
ut_snapshot().assertContent(contentWithCr) | ||
} | ||
|
||
@Test @Order(4) | ||
fun go_back_to_unix_and_it_stays_unix() { | ||
ut_snapshot().setContent(expectedContent) | ||
gradleWriteSS() | ||
ut_snapshot().assertContent(expectedContent) | ||
} | ||
|
||
@Test @Order(5) | ||
fun deleteSelfie() { | ||
ut_snapshot().deleteIfExists() | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
undertest-junit5/src/test/kotlin/undertest/junit5/UT_CarriageReturnTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package undertest.junit5 | ||
|
||
import com.diffplug.selfie.expectSelfie | ||
import kotlin.test.Test | ||
|
||
class UT_CarriageReturnTest { | ||
@Test fun git_makes_carriage_returns_unrepresentable() { | ||
expectSelfie("hard\r\nto\npreserve\r\nthis\r\n").toMatchDisk() | ||
} | ||
} |