forked from EsotericSoftware/yamlbeans
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tag Directive code changes (EsotericSoftware#147)
- Loading branch information
1 parent
53d156f
commit 817df21
Showing
6 changed files
with
124 additions
and
2 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
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,84 @@ | ||
package com.esotericsoftware.yamlbeans; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Iterator; | ||
|
||
import org.junit.Test; | ||
|
||
import com.esotericsoftware.yamlbeans.document.YamlDocumentReader; | ||
import com.esotericsoftware.yamlbeans.document.YamlElement; | ||
import com.esotericsoftware.yamlbeans.document.YamlSequence; | ||
|
||
public class TagDirectiveTest { | ||
|
||
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); | ||
|
||
@Test | ||
public void testTagDirective() throws YamlException { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("%TAG !yaml! tag:yaml.org,2002:").append(LINE_SEPARATOR); | ||
sb.append("---").append(LINE_SEPARATOR); | ||
sb.append("!yaml!str \"foo\""); | ||
|
||
YamlReader yamlReader = new YamlReader(sb.toString()); | ||
assertEquals("foo", yamlReader.read().toString()); | ||
} | ||
|
||
@Test(expected = YamlException.class) | ||
public void testRepeatedTagDirective() throws YamlException { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("%TAG %TAG ! !foo").append(LINE_SEPARATOR); | ||
sb.append("%TAG %TAG ! !foo").append(LINE_SEPARATOR); | ||
sb.append("---").append(LINE_SEPARATOR); | ||
sb.append("bar"); | ||
|
||
new YamlReader(sb.toString()).read(); | ||
} | ||
|
||
@Test | ||
public void testTagHandles() throws YamlException { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("%TAG ! !").append(LINE_SEPARATOR); | ||
sb.append("%TAG !yaml! tag:yaml.org,2002:").append(LINE_SEPARATOR); | ||
sb.append("%TAG !o! tag:ben-kiki.org,2000:").append(LINE_SEPARATOR); | ||
sb.append("---").append(LINE_SEPARATOR); | ||
sb.append("- !foo bar").append(LINE_SEPARATOR); | ||
sb.append("- !!str string").append(LINE_SEPARATOR); | ||
sb.append("- !o!type baz").append(LINE_SEPARATOR); | ||
YamlDocumentReader yamlDocumentReader = new YamlDocumentReader(sb.toString()); | ||
Iterator<YamlElement> iterator = yamlDocumentReader.read(YamlSequence.class).iterator(); | ||
|
||
YamlElement yamlElement = iterator.next(); | ||
assertEquals("!foo", yamlElement.getTag()); | ||
|
||
yamlElement = iterator.next(); | ||
assertEquals("tag:yaml.org,2002:str", yamlElement.getTag()); | ||
|
||
yamlElement = iterator.next(); | ||
assertEquals("tag:ben-kiki.org,2000:type", yamlElement.getTag()); | ||
} | ||
|
||
@Test | ||
public void testVersion() throws YamlException { | ||
String yaml = "!str test"; | ||
YamlConfig yamlConfig = new YamlConfig(); | ||
yamlConfig.readConfig.setDefaultVersion(new Version(1, 0)); | ||
YamlReader yamlReader = new YamlReader(yaml, yamlConfig); | ||
assertEquals("test", yamlReader.read()); | ||
|
||
yaml = "!!str test"; | ||
yamlConfig.readConfig.setDefaultVersion(new Version(1, 1)); | ||
yamlReader = new YamlReader(yaml, yamlConfig); | ||
assertEquals("test", yamlReader.read()); | ||
|
||
yaml = "!!str test"; | ||
yamlConfig.readConfig.setDefaultVersion(new Version(1, 0)); | ||
yamlReader = new YamlReader(yaml, yamlConfig); | ||
try { | ||
yamlReader.read(); | ||
} catch (YamlException e) { | ||
assertEquals("Error parsing YAML.", e.getMessage()); | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
test/com/esotericsoftware/yamlbeans/issues/issue101/Issue101Test.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,34 @@ | ||
package com.esotericsoftware.yamlbeans.issues.issue101; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
import com.esotericsoftware.yamlbeans.YamlException; | ||
import com.esotericsoftware.yamlbeans.YamlReader; | ||
|
||
public class Issue101Test { | ||
|
||
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); | ||
|
||
@Test | ||
public void test() throws YamlException { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("!com.esotericsoftware.yamlbeans.issues.issue101.Issue101Test$TestObject").append(LINE_SEPARATOR); | ||
sb.append("test: test"); | ||
YamlReader yamlReader = new YamlReader(sb.toString()); | ||
TestObject testObject = (TestObject) yamlReader.read(); | ||
assertEquals("test", testObject.test); | ||
|
||
sb = new StringBuilder(); | ||
sb.append("!<!com.esotericsoftware.yamlbeans.issues.issue101.Issue101Test$TestObject>").append(LINE_SEPARATOR); | ||
sb.append("test: test"); | ||
yamlReader = new YamlReader(sb.toString()); | ||
testObject = (TestObject) yamlReader.read(); | ||
assertEquals("test", testObject.test); | ||
} | ||
|
||
static class TestObject { | ||
public String test; | ||
} | ||
} |