@@ -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}
0 commit comments