forked from flyx/NimYAML
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* PresentationOptions has gained more fine-tuning options: * directivesEnd: specifies when `---` is written. ref flyx#135 * containers: specifies whether containers use block or flow style * suppressAddrs: if set, suppresses output of attributes * quoting: specifies how strings should be quoted * condenseFlow: specifies whether flow sequences should be on a single line * explicitKeys: specifies whether mapping keys should always have '?' * PresentationStyle is now a list of presets that set multiple options in PresentationOptions. * Does not output trailing spaces anymore. ref flyx#135 * Writes compact notation, i.e. a mapping in a sequence starts on the line with the sequence's `-`, unless attributes are written * Added tests for the presenter * Existing code might change behavior because of whitespace, `---` and compact notation. The API has been extended so that existing code is affected as little as possible.
- Loading branch information
Showing
17 changed files
with
700 additions
and
462 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
%YAML 1.2 | ||
%TAG !n! tag:nimyaml.org,2016: | ||
--- !n!system:seq(tag:nimyaml.org;2016:custom:Person) | ||
- | ||
name: Karl Koch | ||
--- !n!system:seq(tag:nimyaml.org;2016:custom:Person) | ||
- name: Karl Koch | ||
age: 23 | ||
- | ||
name: Peter Pan | ||
- name: Peter Pan | ||
age: 12 |
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
%YAML 1.1 | ||
%TAG !n! tag:nimyaml.org,2016: | ||
--- | ||
!n!system:seq(tag:nimyaml.org;2016:custom:Person) [ | ||
--- !n!system:seq(tag:nimyaml.org;2016:custom:Person) | ||
[ | ||
!n!custom:Person { | ||
? !n!field "name" | ||
: !!str "Karl Koch", | ||
? !n!field "age" | ||
: !n!system:int32 "23" | ||
? !n!field "name" | ||
: !!str "Karl Koch", | ||
? !n!field "age" | ||
: !n!system:int32 "23" | ||
}, | ||
!n!custom:Person { | ||
? !n!field "name" | ||
: !!str "Peter Pan", | ||
? !n!field "age" | ||
: !n!system:int32 "12" | ||
? !n!field "name" | ||
: !!str "Peter Pan", | ||
? !n!field "age" | ||
: !n!system:int32 "12" | ||
} | ||
] |
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ name: Node 1 | |
left: | ||
name: Node 2 | ||
left: ~ | ||
right: &b | ||
right: &b | ||
name: Node 3 | ||
left: *a | ||
right: ~ | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
%YAML 1.2 | ||
%TAG !n! tag:nimyaml.org,2016: | ||
--- !Mob | ||
--- !Mob | ||
!n!field level: !n!system:int32 42 | ||
!n!field experience: !n!system:int32 1800 | ||
!n!field drops: !Drops [!!str Sword of Mob Slaying] |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
[ | ||
{ | ||
"name": "Karl Koch", | ||
|
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 @@ | ||
import ../yaml | ||
import unittest, strutils | ||
import commonTestUtils | ||
|
||
proc inputSingle(events: varargs[Event]): BufferYamlStream = | ||
result = newBufferYamlStream() | ||
result.put(startStreamEvent()) | ||
result.put(startDocEvent()) | ||
for event in events: result.put(event) | ||
result.put(endDocEvent()) | ||
result.put(endStreamEvent()) | ||
|
||
let minimalOptions = defineOptions(outputVersion = ovNone) | ||
|
||
proc assertOutput( | ||
input: YamlStream, expected: string, | ||
options: PresentationOptions = minimalOptions) = | ||
var output = present(input, options) | ||
assertStringEqual expected, output | ||
|
||
suite "Presenter": | ||
test "Scalar with tag": | ||
var input = inputSingle(scalarEvent("droggeljug", yTagString)) | ||
assertOutput(input, "--- !!str\ndroggeljug\n") | ||
|
||
test "Scalar without tag": | ||
var input = inputSingle(scalarEvent("droggeljug")) | ||
assertOutput(input, "droggeljug\n") | ||
|
||
test "Compact flow sequence": | ||
var input = inputSingle(startSeqEvent(), scalarEvent("1"), scalarEvent("2"), endSeqEvent()) | ||
assertOutput(input, "[1, 2]\n") | ||
|
||
test "Forced block sequence": | ||
var input = inputSingle(startSeqEvent(), scalarEvent("1"), scalarEvent("2"), endSeqEvent()) | ||
assertOutput(input, "- 1\n- 2\n", defineOptions(outputVersion = ovNone, containers = cBlock)) | ||
|
||
test "Forced multiline flow sequence": | ||
var input = inputSingle(startSeqEvent(), scalarEvent("1"), scalarEvent("2"), endSeqEvent()) | ||
assertOutput(input, "[\n 1,\n 2\n]\n", defineOptions(outputVersion = ovNone, condenseFlow = false)) | ||
|
||
test "Compact flow mapping": | ||
var input = inputSingle(startMapEvent(), scalarEvent("1"), scalarEvent("2"), endMapEvent()) | ||
assertOutput(input, "{1: 2}\n", defineOptions(outputVersion = ovNone, containers = cFlow)) | ||
|
||
test "Simple block mapping": | ||
var input = inputSingle(startMapEvent(), scalarEvent("1"), scalarEvent("2"), endMapEvent()) | ||
assertOutput(input, "1: 2\n", defineOptions(outputVersion = ovNone)) | ||
|
||
test "Forced multiline flow mapping": | ||
var input = inputSingle(startMapEvent(), scalarEvent("1"), scalarEvent("2"), endMapEvent()) | ||
assertOutput(input, "{\n 1: 2\n}\n", defineOptions(outputVersion = ovNone, condenseFlow = false, containers = cFlow)) | ||
|
||
test "Forced JSON mapping": | ||
var input = inputSingle(startMapEvent(), scalarEvent("1"), scalarEvent("2"), endMapEvent()) | ||
assertOutput(input, "{\n \"1\": 2\n}\n", defineOptions(outputVersion = ovNone, condenseFlow = false, containers = cFlow, quoting = sqJson)) | ||
|
||
test "Nested flow sequence": | ||
var input = inputSingle(startMapEvent(), scalarEvent("a"), startSeqEvent(), scalarEvent("1"), scalarEvent("2"), endSeqEvent(), endMapEvent()) | ||
assertOutput(input, "a: [1, 2]\n") | ||
|
||
test "Nested block sequence": | ||
var input = inputSingle(startMapEvent(), scalarEvent("a"), startSeqEvent(), scalarEvent("1"), scalarEvent("2"), endSeqEvent(), endMapEvent()) | ||
assertOutput(input, "a:\n - 1\n - 2\n", defineOptions(outputVersion = ovNone, containers = cBlock)) | ||
|
||
test "Compact notation: mapping in sequence": | ||
var input = inputSingle(startSeqEvent(), scalarEvent("a"), startMapEvent(), scalarEvent("1"), scalarEvent("2"), | ||
scalarEvent("3"), scalarEvent("4"), endMapEvent(), endSeqEvent()) | ||
assertOutput(input, "- a\n- 1: 2\n 3: 4\n") | ||
|
||
test "No compact notation: sequence in mapping": | ||
var input = inputSingle(startMapEvent(), scalarEvent("a"), startSeqEvent(), scalarEvent("1"), endSeqEvent(), endMapEvent()) | ||
assertOutput(input, "a:\n - 1\n", defineOptions(outputVersion = ovNone, containers = cBlock)) | ||
|
||
test "Compact notation with 4 spaces indentation": | ||
var input = inputSingle(startSeqEvent(), scalarEvent("a"), startMapEvent(), scalarEvent("1"), scalarEvent("2"), | ||
scalarEvent("3"), scalarEvent("4"), endMapEvent(), endSeqEvent()) | ||
assertOutput(input, "- a\n- 1: 2\n 3: 4\n", defineOptions(outputVersion = ovNone, indentationStep = 4)) | ||
|
||
test "Compact notation with 4 spaces indentation, more complex input": | ||
var input = inputSingle(startMapEvent(), scalarEvent("root"), startSeqEvent(), scalarEvent("a"), | ||
startMapEvent(), scalarEvent("1"), scalarEvent("2"), scalarEvent("3"), scalarEvent("4"), endMapEvent(), | ||
endSeqEvent(), endMapEvent()) | ||
assertOutput(input, "root:\n - a\n - 1: 2\n 3: 4\n", defineOptions(outputVersion = ovNone, indentationStep = 4)) |
Oops, something went wrong.