-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Barebones of int selfie #23
Merged
Merged
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9fa1065
WIP: Int Selfie
jknack 4b9ea21
Add modeling for Literals.
nedtwigg d1c70a5
Selfie should pass inline writes along to the Router.
nedtwigg 338759e
Router should pass inline writes to WriteTracker.
nedtwigg a6245aa
WriteTracker should persist inline snapshots.
nedtwigg 7f4de60
Add testing for the IntFormat (found a bug!)
nedtwigg 002129d
Pipe the filesystem into InlineWriteTracker.persistWrites()
nedtwigg 689936c
Bump spotless and improve the test harness.
nedtwigg 465a08d
Add the inline test.
nedtwigg a1d9a52
Literal: parse literal and inject encoded value
jknack fc5cc07
Merge branch 'improve-test-harness' into feat/int-selfie
nedtwigg 0899b40
Mark `UT_InlineIntTest` as ignored so it doesn't kill other tests.
nedtwigg a5c3125
Merge branch 'main' into feat/int-selfie
nedtwigg 56a3437
Merge branch 'main' into feat/int-selfie
nedtwigg 0d13147
Give Slice a clean way to find a given line (and make it module-priva…
nedtwigg dc245b1
Fix a bug in `Harness` related to reading lines from test source files.
nedtwigg 1b6d271
Created a new `SourceFile` which is now used to write inline snapshots.
nedtwigg 1d77e6f
Simplify Slice to be as small as possible.
nedtwigg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 50 additions & 0 deletions
50
selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/Literals.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,50 @@ | ||
/* | ||
* 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 | ||
|
||
class LiteralValue<T : Any>(val expected: T?, val actual: T, val format: LiteralFormat<T>) { | ||
fun encodedActual(): String = format.encode(actual) | ||
} | ||
|
||
interface LiteralFormat<T : Any> { | ||
fun encode(value: T): String | ||
fun parse(str: String): T | ||
} | ||
|
||
class IntFormat : LiteralFormat<Int> { | ||
override fun encode(value: Int): String { | ||
// TODO: 1000000 is hard to read, 1_000_000 is much much better | ||
return value.toString() | ||
} | ||
override fun parse(str: String): Int { | ||
return str.replace("_", "").toInt() | ||
} | ||
} | ||
|
||
class StrFormat : LiteralFormat<String> { | ||
override fun encode(value: String): String { | ||
if (!value.contains("\n")) { | ||
// TODO: replace \t, maybe others... | ||
return "\"" + value.replace("\"", "\\\"") + "\"" | ||
} else { | ||
// TODO: test! It's okay to assume Java 15+ for now | ||
return "\"\"\"\n" + value + "\"\"\"" | ||
} | ||
} | ||
override fun parse(str: String): String { | ||
TODO("Harder than it seems!") | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/IntFormatTest.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,56 @@ | ||
/* | ||
* 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 IntFormatTest { | ||
@Test | ||
fun encode() { | ||
encode(0, "0") | ||
encode(1, "1") | ||
encode(-1, "-1") | ||
encode(999, "999") | ||
encode(-999, "-999") | ||
// TODO: add underscores | ||
encode(1_000, "1000") | ||
encode(-1_000, "-1000") | ||
encode(1_000_000, "1000000") | ||
encode(-1_000_000, "-1000000") | ||
} | ||
private fun encode(value: Int, expected: String) { | ||
val actual = IntFormat().encode(value) | ||
actual shouldBe expected | ||
} | ||
|
||
@Test | ||
fun decode() { | ||
decode("0", 0) | ||
decode("1", 1) | ||
decode("-1", -1) | ||
decode("999", 999) | ||
decode("9_99", 999) | ||
decode("9_9_9", 999) | ||
decode("-999", -999) | ||
decode("-9_99", -999) | ||
decode("-9_9_9", -999) | ||
} | ||
private fun decode(value: String, expected: Int) { | ||
val actual = IntFormat().parse(value) | ||
actual shouldBe expected | ||
} | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why it needs anything more than
line.conains(start)
. The trick is to dolinesFrom
only for things where you know the line is unique. If this was enduser API I would probably enforce that it must be unique, and throw an error with line numbers if more than one line matched. If we're having trouble with it in our test code, then I'd support adding a uniqueness check here.