use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.
the class VObjectReaderTest method parameter_value_escaping.
/**
* Tests the various escaping mechanisms for parameter values.
*/
@Test
public void parameter_value_escaping() throws Exception {
SyntaxStyle style = SyntaxStyle.OLD;
{
// 1: backslash that doesn't escape anything
// 2: caret-escaped caret
// 3: caret-escaped newline (lowercase n)
// 4: caret-escaped newline (uppercase N)
// 5: caret-escaped double quote
// 6: caret that doesn't escape anything
// 7: backslash-escaped semi-colon (must be escaped in 2.1)
// 8: un-escaped double quote (no special meaning in 2.1)
// 9: backslash-escaped backslash
// @formatter:off
String string = "PROP;PARAM=1\\ 2^^ 3^n 4^N 5^' 6^ 7\\; 8\" 9\\\\:";
for (boolean caretDecodingEnabled : new boolean[] { false, true }) {
// caret decoding has no effect in old style
VObjectReader reader = reader(string, style);
reader.setCaretDecodingEnabled(caretDecodingEnabled);
VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
reader.parse(listener);
InOrder inorder = inOrder(listener);
inorder.verify(listener).onProperty(eq(property().name("PROP").param("PARAM", "1\\ 2^^ 3^n 4^N 5^' 6^ 7; 8\" 9\\").value("").build()), any(Context.class));
// @formatter:off
String[] lines = string.split("\r\n");
int line = 0;
assertContexts(listener, context(lines[line], ++line));
// @formatter:on
}
}
style = SyntaxStyle.NEW;
{
// 1: backslash that doesn't escape anything
// 2: caret-escaped caret
// 3: caret-escaped newline (lowercase n)
// 4: caret-escaped newline (uppercase N)
// 5: backslash-escaped newline (lowercase n)
// 7: caret-escaped double quote
// 8: backslash-escaped backslash
// 9: caret that doesn't escape anything
// @formatter:off
String string = "PROP;PARAM=1\\ 2^^ 3^n 4^N 5\\n 7^' 8\\\\ 9^ :";
// @formatter:on
Map<Boolean, String> expectedParamValues = new HashMap<Boolean, String>();
expectedParamValues.put(false, "1\\ 2^^ 3^n 4^N 5\\n 7^' 8\\\\ 9^ ");
expectedParamValues.put(true, "1\\ 2^ 3" + NEWLINE + " 4^N 5\\n 7\" 8\\\\ 9^ ");
for (Boolean caretDecodingEnabled : expectedParamValues.keySet()) {
String expectedParamValue = expectedParamValues.get(caretDecodingEnabled);
VObjectReader reader = reader(string, style);
reader.setCaretDecodingEnabled(caretDecodingEnabled);
VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
reader.parse(listener);
InOrder inorder = inOrder(listener);
inorder.verify(listener).onProperty(eq(property().name("PROP").param("PARAM", expectedParamValue).value("").build()), any(Context.class));
// @formatter:off
String[] lines = string.split("\r\n");
int line = 0;
assertContexts(listener, context(lines[line], ++line));
// @formatter:on
}
}
}
use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.
the class VObjectReaderTest method special_characters_in_group_and_name.
/**
* When there are special characters in the group and property name.
*/
@Test
public void special_characters_in_group_and_name() throws Exception {
// @formatter:off
String string = "g=,\"roup.P.=,\"ROP:value";
for (SyntaxStyle style : SyntaxStyle.values()) {
VObjectReader reader = reader(string, style);
VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
reader.parse(listener);
InOrder inorder = inOrder(listener);
inorder.verify(listener).onProperty(eq(new VObjectProperty("g=,\"roup", "P.=,\"ROP", "value")), any(Context.class));
// @formatter:off
String[] lines = string.split("\r\n");
int line = 0;
assertContexts(listener, context(lines[line], ++line));
// @formatter:on
}
}
use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.
the class VObjectReaderTest method parameter_values_in_double_quotes.
/**
* New style syntax lets you surround parameter values in double quotes.
* Doing this lets you put special characters like semi-colons in the
* property value.
*/
@Test
public void parameter_values_in_double_quotes() throws Exception {
// @formatter:off
String string = "PROP;PARAM=\"a;b:c,d\":value";
// @formatter:on
Map<SyntaxStyle, VObjectProperty> styleToProperty = new HashMap<SyntaxStyle, VObjectProperty>();
styleToProperty.put(SyntaxStyle.OLD, property().name("PROP").param("PARAM", "\"a").param(null, "b").value("c,d\":value").build());
styleToProperty.put(SyntaxStyle.NEW, property().name("PROP").param("PARAM", "a;b:c,d").value("value").build());
for (SyntaxStyle style : styleToProperty.keySet()) {
VObjectProperty expectedProperty = styleToProperty.get(style);
VObjectReader reader = reader(string, style);
VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
reader.parse(listener);
InOrder inorder = inOrder(listener);
inorder.verify(listener).onProperty(eq(expectedProperty), any(Context.class));
// @formatter:off
String[] lines = string.split("\r\n");
int line = 0;
assertContexts(listener, context(lines[line], ++line));
// @formatter:on
}
}
use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.
the class VObjectReaderTest method empty_lines.
/**
* Empty lines should be ignored.
*/
@Test
public void empty_lines() throws Exception {
// @formatter:off
String string = "PROP1:value1\r\n" + "\r\n" + "PROP2:value2\r\n" + "\r\n" + "\n" + "\r" + "PROP3:value3";
for (SyntaxStyle style : SyntaxStyle.values()) {
VObjectReader reader = reader(string, style);
VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
reader.parse(listener);
InOrder inorder = inOrder(listener);
inorder.verify(listener).onProperty(eq(property().name("PROP1").value("value1").build()), any(Context.class));
inorder.verify(listener).onProperty(eq(property().name("PROP2").value("value2").build()), any(Context.class));
inorder.verify(listener).onProperty(eq(property().name("PROP3").value("value3").build()), any(Context.class));
// @formatter:off
assertContexts(listener, context("PROP1:value1", 1), context("PROP2:value2", 3), context("PROP3:value3", 7));
// @formatter:on
}
}
use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.
the class VObjectReaderTest method wrong_newlines.
/**
* Incorrect newline sequences should be accepted.
*/
@Test
public void wrong_newlines() throws Exception {
// @formatter:off
String string = "PROP1:value1\r" + "PROP2:value2\n" + "PROP3:value3";
for (SyntaxStyle style : SyntaxStyle.values()) {
VObjectReader reader = reader(string, style);
VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
reader.parse(listener);
InOrder inorder = inOrder(listener);
inorder.verify(listener).onProperty(eq(property().name("PROP1").value("value1").build()), any(Context.class));
inorder.verify(listener).onProperty(eq(property().name("PROP2").value("value2").build()), any(Context.class));
inorder.verify(listener).onProperty(eq(property().name("PROP3").value("value3").build()), any(Context.class));
// @formatter:off
int line = 0;
assertContexts(listener, context("PROP1:value1", ++line), context("PROP2:value2", ++line), context("PROP3:value3", ++line));
// @formatter:on
}
}
Aggregations