-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from lordofthejars/issue-330
Issue #330 Implementing several minimal features in PactDsl
- Loading branch information
Showing
5 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
pact-jvm-consumer/src/main/java/au/com/dius/pact/consumer/dsl/QuoteUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package au.com.dius.pact.consumer.dsl; | ||
|
||
/** | ||
* Util class for dealing with Json format | ||
*/ | ||
public final class QuoteUtil { | ||
|
||
private QuoteUtil() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Reads the input text with possible single quotes as delimiters | ||
* and returns a String correctly formatted. | ||
* <p>For convenience, single quotes as well as double quotes | ||
* are allowed to delimit strings. If single quotes are | ||
* used, any quotes, single or double, in the string must be | ||
* escaped (prepend with a '\'). | ||
* | ||
* @param text the input data | ||
* @return String without single quotes | ||
*/ | ||
public static String convert(String text) { | ||
StringBuilder builder = new StringBuilder(); | ||
boolean single_context = false; | ||
for (int i = 0; i < text.length(); i++) { | ||
char ch = text.charAt(i); | ||
if (ch == '\\') { | ||
i = i + 1; | ||
if (i < text.length()) { | ||
ch = text.charAt(i); | ||
if (!(single_context && ch == '\'')) { | ||
// unescape ' inside single quotes | ||
builder.append('\\'); | ||
} | ||
} | ||
} else if (ch == '\'') { | ||
// Turn ' into ", for proper string | ||
ch = '"'; | ||
single_context = ! single_context; | ||
} | ||
builder.append(ch); | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
pact-jvm-consumer/src/test/java/au/com/dius/pact/consumer/dsl/QuoteUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package au.com.dius.pact.consumer.dsl; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class QuoteUtilTest { | ||
|
||
@Test | ||
public void testSingleQuotes() { | ||
final String converted = QuoteUtil.convert("{'name': 'Alex'}"); | ||
assertEquals("{\"name\": \"Alex\"}", converted); | ||
} | ||
|
||
@Test | ||
public void testSkipDoubleQuotes() { | ||
final String converted = QuoteUtil.convert("{\"name\": \"Alex\"}"); | ||
assertEquals("{\"name\": \"Alex\"}", converted); | ||
} | ||
|
||
} |