Skip to content

Commit 3682c46

Browse files
committed
Implement toBe(LITERAL):
- parse '()' to extract literal values - TODO: do we need to handle nested '()' - TODO: handle '()' inside string literal
1 parent 0483b87 commit 3682c46

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SourceFile.kt

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class SourceFile(filename: String, content: String) {
3636
* Represents a section of the sourcecode which is a `.toBe(LITERAL)` call. It might also be
3737
* `.toBe_TODO()` or ` toBe LITERAL` (infix notation).
3838
*/
39-
inner class ToBeLiteral internal constructor(private val slice: Slice) {
39+
inner class ToBeLiteral
40+
internal constructor(private val slice: Slice, private val valueStart: Int) {
4041
/**
4142
* Modifies the parent [SourceFile] to set the value within the `toBe` call, and returns the net
4243
* change in newline count.
@@ -48,7 +49,7 @@ class SourceFile(filename: String, content: String) {
4849
throw Error(
4950
"There is an error in " +
5051
literalValue.format::class.simpleName +
51-
", the following value isn't roundtripping.\n" +
52+
", the following value isn't round tripping.\n" +
5253
"Please this error and the data below at https://github.com/diffplug/selfie/issues/new\n" +
5354
"```\n" +
5455
"ORIGINAL\n" +
@@ -73,20 +74,45 @@ class SourceFile(filename: String, content: String) {
7374
* `toBe_TODO()`.
7475
*/
7576
fun <T : Any> parseLiteral(literalFormat: LiteralFormat<T>): T {
76-
// this won't work, because we need to find the `.toBe` and parens
77-
TODO("return literalFormat.parse(slice.toString())")
77+
return literalFormat.parse(
78+
slice.subSequence(valueStart, slice.length - 1).toString(), language)
7879
}
7980
}
8081
fun parseToBe_TODO(lineOneIndexed: Int): ToBeLiteral {
82+
return parseToBeLike(".toBe_TODO(", lineOneIndexed)
83+
}
84+
fun parseToBe(lineOneIndexed: Int): ToBeLiteral {
85+
return parseToBeLike(".toBe(", lineOneIndexed)
86+
}
87+
private fun parseToBeLike(prefix: String, lineOneIndexed: Int): ToBeLiteral {
8188
val lineContent = contentSlice.unixLine(lineOneIndexed)
82-
val idx = lineContent.indexOf(".toBe_TODO()")
89+
val idx = lineContent.indexOf(prefix)
8390
if (idx == -1) {
8491
throw AssertionError(
85-
"Expected to find `.toBe_TODO()` on line $lineOneIndexed, but there was only `${lineContent}`")
92+
"Expected to find `$prefix...)` on line $lineOneIndexed, but there was only `${lineContent}`")
8693
}
87-
return ToBeLiteral(lineContent.subSequence(idx, idx + ".toBe_TODO()".length))
88-
}
89-
fun parseToBe(lineOneIndexed: Int): ToBeLiteral {
90-
TODO("More complicated because we have to actually parse the literal")
94+
var opened = 0
95+
val startIndex = idx + prefix.length
96+
var endIndex = -1
97+
// TODO: do we need to detect paired parenthesis ( ( ) )?
98+
for (i in startIndex ..< lineContent.length) {
99+
val ch = lineContent[i]
100+
// TODO: handle () inside string literal
101+
if (ch == '(') {
102+
opened += 1
103+
} else if (ch == ')') {
104+
if (opened == 0) {
105+
endIndex = i
106+
break
107+
} else {
108+
opened -= 1
109+
}
110+
}
111+
}
112+
if (endIndex == -1) {
113+
throw AssertionError(
114+
"Expected to find `$prefix...)` on line $lineOneIndexed, but there was only `${lineContent}`")
115+
}
116+
return ToBeLiteral(lineContent.subSequence(idx, endIndex + 1), prefix.length)
91117
}
92118
}

undertest-junit5/src/test/kotlin/undertest/junit5/UT_InlineIntTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import kotlin.test.Test
66

77
class UT_InlineIntTest {
88
@Test fun singleInt() {
9-
expectSelfie(1234).toBe(1234)
9+
expectSelfie(7777).toBe(7777)
1010
}
1111
}

0 commit comments

Comments
 (0)