Skip to content

Commit

Permalink
add UT for StringUtils
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Kao <[email protected]>
  • Loading branch information
seankao-az committed Jun 27, 2024
1 parent 8ed8255 commit 07250e7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ lazy val pplSparkIntegration = (project in file("ppl-spark-integration"))
"org.scalatest" %% "scalatest" % "3.2.15" % "test",
"org.scalatest" %% "scalatest-flatspec" % "3.2.15" % "test",
"org.scalatestplus" %% "mockito-4-6" % "3.2.15.0" % "test",
"org.junit.jupiter" % "junit-jupiter-api" % "5.9.0" % "test",
"org.junit.jupiter" % "junit-jupiter-engine" % "5.9.0" % "test",
"com.stephenn" %% "scalatest-json-jsonassert" % "0.2.5" % "test",
"com.github.sbt" % "junit-interface" % "0.13.3" % "test"),
libraryDependencies ++= deps(sparkVersion),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public static String unquoteText(String text) {
/**
* Unquote Identifier which has ` as mark.
*
* @param identifier identifier that possibly enclosed by double quotes or back ticks
* @return An unquoted string whose outer pair of (double/back-tick) quotes have been removed
* @param identifier identifier that possibly enclosed by backticks
* @return An unquoted string whose outer pair of backticks have been removed
*/
public static String unquoteIdentifier(String identifier) {
if (isQuoted(identifier, "`")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.opensearch.sql.common.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opensearch.sql.common.utils.StringUtils.unquoteIdentifier;
import static org.opensearch.sql.common.utils.StringUtils.unquoteText;

import org.junit.Test;

public class StringUtilsTest {
@Test
public void unquoteIdentifierTest() {
assertEquals("test", unquoteIdentifier("test"));
assertEquals("test", unquoteIdentifier("`test`"));

assertEquals("\"test\"", unquoteIdentifier("\"test\""));
assertEquals("\'test\'", unquoteIdentifier("\'test\'"));
}

@Test
public void unquoteTextTest() {
assertEquals("test", unquoteText("test"));
assertEquals("test", unquoteText("'test'"));

assertEquals("test'", unquoteText("'test'''"));
assertEquals("test\"", unquoteText("\"test\"\"\""));

assertEquals("te``st", unquoteText("'te``st'"));
assertEquals("te``st", unquoteText("\"te``st\""));

assertEquals("te'st", unquoteText("'te''st'"));
assertEquals("te''st", unquoteText("\"te''st\""));

assertEquals("te\"\"st", unquoteText("'te\"\"st'"));
assertEquals("te\"st", unquoteText("\"te\"\"st\""));

assertEquals("''", unquoteText("''''''"));
assertEquals("\"\"", unquoteText("\"\"\"\"\"\""));

assertEquals("test'", unquoteText("'test''"));

assertEquals("", unquoteText(""));
assertEquals("'", unquoteText("'"));
assertEquals("\"", unquoteText("\""));

assertEquals("hello'", unquoteText("'hello''"));
assertEquals("don't", unquoteText("'don't'"));
assertEquals("don\"t", unquoteText("\"don\"t\""));

assertEquals("hel\\lo'", unquoteText("'hel\\lo''"));
assertEquals("hel'lo", unquoteText("'hel'lo'"));
assertEquals("hel\"lo", unquoteText("\"hel\"lo\""));
assertEquals("hel\\'\\lo", unquoteText("'hel\\\\''\\\\lo'"));
}
}

0 comments on commit 07250e7

Please sign in to comment.