Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Renamed Utils to Quote, added FeatureSpec

  • Loading branch information...
commit dcdedc468de74767e46e96f05289077d841bf82d 1 parent 502c2aa
@melezov authored
View
8 project/JvmIoBuild.scala
@@ -16,12 +16,10 @@ object BuildSettings {
// ---------------------------------------------------------------------------
object Dependencies {
- val scala = "org.scala-lang" % "scala-library" % (_: String)
- val scalaTest = "org.scalatest" %% "scalatest" % "2.0.M5b"
-
val ldUtil = libraryDependencies <++= (version, scalaVersion) ( (v, sV) => Seq(
- scala(sV) % (if (v endsWith "SNAPSHOT") "compile" else "test")
- , scalaTest % "test"
+ "org.scala-lang" % "scala-library" % sV % (if (v endsWith "SNAPSHOT") "compile" else "test")
+ , "org.scalatest" %% "scalatest" % "2.0.M5b" % "test"
+ , "junit" % "junit" % "4.11" % "test"
))
}
View
74 util/src/main/java/io/jvm/Quote.java
@@ -0,0 +1,74 @@
+package io.jvm;
+
+public class Quote {
+ public static final String escape(final String text, final char quote) {
+ if (text == null) return text;
+
+ final int len = text.length();
+ final char[] quoted;
+
+ char[] chars = null; // no need
+ int cnt = len;
+
+ if (len != 0) {
+ chars = text.toCharArray();
+ for (int i = len - 1; i >= 0; i --) {
+ if (chars[i] == quote) cnt++;
+ }
+ }
+
+ quoted = new char[cnt + 2];
+ quoted[0] = quoted[cnt + 1] = quote;
+
+ if (len != 0) {
+ if (len == cnt) {
+ System.arraycopy(chars, 0, quoted, 1, len);
+ } else {
+ for (int i = len - 1; i >= 0; i --) {
+ final char ch = chars[i];
+ quoted[cnt--] = ch;
+ if (ch == quote)
+ quoted[cnt--] = quote;
+ }
+ }
+ }
+
+ return new String(quoted);
+ }
+
+ public static final String escape(final String text, final char quote, final char escape) {
+ if (text == null) return text;
+
+ final int len = text.length();
+ final char[] quoted;
+
+ char[] chars = null; // no need
+ int cnt = len;
+
+ if (len != 0) {
+ chars = text.toCharArray();
+ for (int i = len - 1; i >= 0; i --) {
+ final char ch = chars[i];
+ if (ch == quote || ch == escape) cnt++;
+ }
+ }
+
+ quoted = new char[cnt + 2];
+ quoted[0] = quoted[cnt + 1] = quote;
+
+ if (len != 0) {
+ if (len == cnt) {
+ System.arraycopy(chars, 0, quoted, 1, len);
+ } else {
+ for (int i = len - 1; i >= 0; i --) {
+ final char ch = chars[i];
+ quoted[cnt--] = ch;
+ if (ch == quote || ch == escape)
+ quoted[cnt--] = escape;
+ }
+ }
+ }
+
+ return new String(quoted);
+ }
+}
View
49 util/src/main/java/io/jvm/util/Utils.java
@@ -1,49 +0,0 @@
-package io.jvm.util;
-
-public class Utils {
- public static final String quote(final String text, final char quote) {
- final int len = text.length();
- if (len == 0) {
- return new String(new char[]{ quote, quote });
- }
-
- final int total; {
- int cnt = 0;
- int start = 0;
- while (true) {
- final int ind = text.indexOf(quote, start);
- if (ind == -1) {
- break;
- }
-
- start = ind + 1;
- cnt++;
- }
- total = cnt;
- }
-
- final int newLen = len + 2 + total;
-
- final char[] quoted = new char[newLen]; {
- quoted[0] = quoted[newLen - 1] = quote;
-
- if (total == 0) {
- for (int i = 0; i < len; i++) {
- quoted[i + 1] = text.charAt(i);
- }
- } else {
- int cnt = 1;
- for (int i = 0; i < len; i++) {
- final char ch = text.charAt(i);
- quoted[cnt + i] = ch;
-
- if (ch == quote) {
- quoted[++cnt + i] = ch;
- }
- }
- }
- }
-
- return new String(quoted);
- }
-}
View
148 util/src/test/scala/io/jvm/test/QuoteFeatureSpec.scala
@@ -0,0 +1,148 @@
+package io.jvm
+package test
+
+import org.scalatest._
+
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+
+@RunWith(classOf[JUnitRunner])
+class QuoteFeatureSpec extends FeatureSpec with GivenWhenThen with MustMatchers {
+
+ feature("jvm.io knows how to escape a character by duplication"){
+ info("When quoting a character via duplication,")
+ info("every instance of the said character must be doubled")
+ info("and the quote character must be appended to both ends")
+
+ import Quote.escape
+
+ scenario("Escape (dup) is a stable function") {
+ Given("a null input string")
+ When("it is escaped via a character")
+ Then("a null string must be returned")
+ escape(null, 'q') must equal (null)
+ }
+
+ scenario("Escape (dup) handling of empty strings") {
+ Given("an empty input string")
+ When("it is escaped via a character")
+ Then("a string with two quote characters must be returned")
+ escape("", 'q') must equal ("qq")
+ }
+
+ scenario("Escape (dup) handling of a quote character") {
+ Given("a string consisting of a single character")
+ When("it is escaped via that character")
+ Then("a string with four quote characters must be returned")
+ escape("q", 'q') must equal ("q" * 4)
+ }
+
+ scenario("Escape (dup) handling of a non-quote character") {
+ Given("a string consisting of a single character")
+ When("it is escaped via a different character")
+ Then("the original string must be surrounded by the quote characters")
+ escape("a", 'q') must equal ('q' + "a" + 'q')
+ }
+
+ scenario("Escape (dup) handling of quote characters") {
+ Given("a string consisting of multiple quote characters")
+ When("it is escaped via that character")
+ Then("the string must be duplicated")
+ And("be surrounded by the quote characters")
+ escape("qqq", 'q') must equal ('q' + ("qqq" * 2) + 'q')
+ }
+
+ scenario("Escape (dup) handling of non-quote characters") {
+ Given("a string consisting of multiple non-quote characters")
+ When("it is escaped via a non-present character")
+ Then("the string must remain the same")
+ And("be surrounded by the quote characters")
+ escape("aaa", 'q') must equal ("q" + "aaa" + "q")
+ }
+
+ scenario("Escape (dup) handling of a sentence containing the quote characters") {
+ Given("a string consisting of some quote characters")
+ When("it is escaped via that character")
+ Then("the quoted characters must be duplicated")
+ And("the string must be surrounded by those characters")
+ escape("It's 'OK'!", '\'') must equal ("'It''s ''OK''!'")
+ }
+ }
+
+ feature("jvm.io knows how to escape a character via escapeing"){
+ info("When quoting a character via escaping, every instance of the said")
+ info("character and of its escape must be preceeded by the escape character")
+ info("and the quote character must be appended to both ends")
+
+ import Quote.escape
+
+ scenario("Escape (esc) is a stable function") {
+ Given("a null input string")
+ When("it is escaped via a character")
+ Then("a null string must be returned")
+ escape(null, 'q', 'r') must equal (null)
+ }
+
+ scenario("Escape (esc) handling of empty strings") {
+ Given("an empty input string")
+ When("it is escaped via a character")
+ Then("a string with two quote characters must be returned")
+ escape("", 'q', 'r') must equal ("qq")
+ }
+
+ scenario("Escape (esc) handling of a quote character") {
+ Given("a string consisting of a single character")
+ When("that character is the quote character")
+ Then("an escape charater must preceed the quote character in the string")
+ escape("q", 'q', 'r') must equal ("qrqq")
+ }
+
+ scenario("Escape (esc) handling of an escape character") {
+ Given("a string consisting of a single character")
+ When("that character is the escape character")
+ Then("an escape charater must be duplicated")
+ escape("r", 'q', 'r') must equal ("qrrq")
+ }
+
+ scenario("Escape (esc) handling of a non-quote character") {
+ Given("a string consisting of a single character")
+ When("it is escaped via a different character")
+ Then("the original string must be surrounded by the quote characters")
+ escape("a", 'q', 'r') must equal ('q' + "a" + 'q')
+ }
+
+ scenario("Escape (esc) handling of quote characters") {
+ Given("a string consisting of multiple quote characters")
+ When("it is escaped via that character")
+ Then("the string must be doubled in size")
+ And("have interleaved escape characters for each quote")
+ escape("qqq", 'q', 'r') must equal ('q' + ("rq" * "qqq".length) + 'q')
+ }
+
+ scenario("Escape (esc) handling of escape characters") {
+ Given("a string consisting of multiple escape characters")
+ When("it is escaped via a quote character")
+ Then("the string must be duplicated")
+ And("be surrounded by the quote characters")
+ escape("rrr", 'q', 'r') must equal ('q' + ("rrr" * 2) + 'q')
+ }
+
+ scenario("Escape (esc) handling of non-quote characters") {
+ Given("a string consisting of multiple non-quote, non-escape characters")
+ When("it is escaped via a non-present character")
+ And("the escape character is also not present")
+ Then("the string must remain the same")
+ And("be surrounded by the quote characters")
+ escape("aaa", 'q', 'r') must equal ("q" + "aaa" + "q")
+ }
+
+ scenario("Escape handling of a sentence containing the quote and escape characters") {
+ Given("a string consisting of some quote and escape characters")
+ When("it is escaped via those characters")
+ Then("the quoted characters must be escaped")
+ And("the escape characters must be duplicated")
+ And("the string must be surrounded by the quote characters")
+ escape("""It's '\OK\'!""", '\'', '\\') must equal ("""'It\'s \'\\OK\\\'!'""")
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.