Skip to content

Commit

Permalink
Rewrote large parts of presenter
Browse files Browse the repository at this point in the history
 * 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
flyx committed Jul 31, 2023
1 parent 84e59c1 commit 189844a
Show file tree
Hide file tree
Showing 17 changed files with 700 additions and 462 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ test/tserialization
test/tjson
test/tparser
test/tquickstart
test/tpresenter
test/*.exe
test/*.pdb
test/*.ilk
test/*.js
server/server
bench/jsonBench
bench/yamlBench
Expand All @@ -22,4 +24,8 @@ doc/rstPreproc
doc/tmp.rst
doc/**/code
nimsuggest.log
server/server_cfg.nim
result
vendor
.vscode
*.swp
5 changes: 5 additions & 0 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ task hintsTests, "Run hints tests":
--verbosity:0
setCommand "c", "test/thints"

task presenterTests, "Run presenter tests":
--r
--verbosity:0
setCommand "c", "test/tpresenter"

task documentation, "Generate documentation":
exec "mkdir -p docout"
withDir "doc":
Expand Down
8 changes: 3 additions & 5 deletions doc/snippets/quickstart/00/01-out.yaml
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
11 changes: 6 additions & 5 deletions doc/snippets/quickstart/02/00-code.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ personList.add(Person(name: "Karl Koch", age: 23))
personList.add(Person(name: "Peter Pan", age: 12))

var s = newFileStream("out.yaml", fmWrite)
dump(personList, s, options = defineOptions(
style = psCanonical,
indentationStep = 3,
newlines = nlLF,
outputVersion = ov1_1))
dump(personList, s, tagStyle = tsAll,
options = defineOptions(
style = psCanonical,
indentationStep = 3,
newlines = nlLF,
outputVersion = ov1_1))
s.close()
20 changes: 10 additions & 10 deletions doc/snippets/quickstart/02/01-out.yaml
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"
}
]
6 changes: 3 additions & 3 deletions doc/snippets/quickstart/03/01-out.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
%YAML 1.2
%TAG !n! tag:nimyaml.org,2016:
--- !n!custom:NodeObj &a
--- !n!custom:NodeObj &a
name: Node 1
left:
left:
name: Node 2
left: !!null ~
right: &b
right: &b
name: Node 3
left: *a
right: !!null ~
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/quickstart/04/01-in.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Node 1
left:
name: Node 2
left: ~
right: &b
right: &b
name: Node 3
left: *a
right: ~
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/quickstart/05/01-out.yaml
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]
2 changes: 1 addition & 1 deletion doc/snippets/quickstart/06/00-code.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ personList.add(Person(name: "Peter Pan", age: 12))

var s = newFileStream("out.yaml", fmWrite)
dump(personList, s,
options = defineOptions(style = psJson))
options = defineOptions(style = psJson))
s.close()
1 change: 0 additions & 1 deletion doc/snippets/quickstart/06/01-out.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[
{
"name": "Karl Koch",
Expand Down
28 changes: 27 additions & 1 deletion test/commonTestUtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,30 @@ template ensure*(input: var YamlStream,
printDifference(expected[i], token)
fail()
break
i.inc()
i.inc()

template assertStringEqual*(expected, actual: string) =
if expected != actual:
# if they are unequal, walk through the strings and check each
# character for a better error message
if expected.len != actual.len:
echo "Expected and actual string's length differs.\n"
echo "Expected length: ", expected.len, "\n"
echo "Actual length: ", actual.len, "\n"
# check length up to smaller of the two strings
for i in countup(0, min(expected.high, actual.high)):
if expected[i] != actual[i]:
echo "string mismatch at character #", i, "(expected:\'",
expected[i], "\', was \'", actual[i], "\'):\n"
echo "expected:\n", expected, "\nactual:\n", actual, "\n"
assert(false)
# if we haven't raised an assertion error here, the problem is that
# one string is longer than the other
let minInd = min(expected.len, actual.len) # len instead of high to continue
# after shorter string
if expected.high > actual.high:
echo "Expected continues with: '", expected[minInd .. ^1], "'"
assert false
else:
echo "Actual continues with: '", actual[minInd .. ^1], "'"
assert false
2 changes: 1 addition & 1 deletion test/tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# distribution, for details about the copyright.

{.warning[UnusedImport]: off.}
import tlex, tjson, tserialization, tparser, tquickstart, tannotations, thints
import tlex, tjson, tserialization, tparser, tquickstart, tannotations, thints, tpresenter

when not defined(gcArc) or defined(gcOrc):
import tdom
84 changes: 84 additions & 0 deletions test/tpresenter.nim
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))
Loading

0 comments on commit 189844a

Please sign in to comment.