Skip to content

Commit

Permalink
more testing for escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
cottand committed Jun 16, 2024
1 parent 1848120 commit 6b6df0e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
27 changes: 20 additions & 7 deletions src/main/java/org/nixos/idea/util/NixIndStringUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,31 @@ object NixIndStringUtil {
}

when (c) {
// ''' is escaped to ''
// '' is the string delimiter
// ''\ escapes any character, but we can only cover known ones in advance:
'\'' -> when {
// ''' is escaped to ''
prev2Chars() == "''" -> append("''")
// '' is the string delimiter
else -> continue
}

'\\' -> when {
prev2Chars() == "''" -> continue
prevChar() == '\'' -> continue
else -> append(c)
}
// ''\ escapes any character, but we can only cover known ones in advance:
'r' -> if (prev3Chars() == "''\\") append('\r') else append(c)
'n' -> if (prev3Chars() == "''\\") append('\n') else append(c)
't' -> if (prev3Chars() == "''\\") append('\t') else append(c)
else -> append(c)

'$' -> if (prevChar() == '$') append(c) else continue
'{' -> if (prevChar() == '$') append("\${") else append(c)

else -> if (prev3Chars() == "''\\") when (c) {
'r' -> if (prev3Chars() == "''\\") append('\r') else append(c)
'n' -> if (prev3Chars() == "''\\") append('\n') else append(c)
't' -> if (prev3Chars() == "''\\") append('\t') else append(c)
else -> append("''\\").append(c)
} else {
append(c)
}
}
}
}
Expand Down
43 changes: 15 additions & 28 deletions src/test/java/org/nixos/idea/util/NixIndStringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,28 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

final class NixIndStringUtilTest {
// @ParameterizedTest(name = "[{index}] {0} -> {1}")
// @CsvSource(textBlock = """
// '' , ""
// abc , "abc"
// " , "\\\""
// \\ , "\\\\"
// \\x , "\\\\x"
// a${b}c , "a\\${b}c"
// '\n' , "\\n"
// '\r' , "\\r"
// '\t' , "\\t"
// # supplementary character, i.e. character form a supplementary plane,
// # which needs a surrogate pair to be represented in UTF-16
// \uD83C\uDF09 , "\uD83C\uDF09"
// """)
// void quote(String unescaped, String expectedResult) {
// assertEquals(expectedResult, NixStringUtil.quote(unescaped));
// }

@ParameterizedTest(name = "[{index}] {0} -> {1}")
@CsvSource(textBlock = """
'' , ''
@CsvSource(quoteCharacter = '|', textBlock = """
|| , ||
abc , abc
" , \\"
\\ , \\\\
\\x , \\\\x
a${b}c , a\\${b}c
'\n' , \\n
'\r' , \\r
'\t' , \\t
" , "
\\ , \\
\\x , \\x
a${b}c , a${b}c
|\n| , |\n|
|\r| , |\r|
|\t| , |\t|
|''\\t| , |\t|
|''\\r| , |\r|
|''\\n| , |\n|
|'''| , |''|
$$ , $
# supplementary character, i.e. character form a supplementary plane,
# which needs a surrogate pair to be represented in UTF-16
\uD83C\uDF09 , \uD83C\uDF09
""")
void escape(String unescaped, String expectedResult) {
NixIndStringUtil.INSTANCE.escape("''\\t");
var str = NixIndStringUtil.INSTANCE.escape(unescaped);
assertEquals(expectedResult, str);
}
Expand Down

0 comments on commit 6b6df0e

Please sign in to comment.