2023-08-29 18:46:26 +00:00
|
|
|
import ../yaml/[ presenter, data, stream ]
|
|
|
|
import unittest
|
2023-07-31 17:16:24 +00:00
|
|
|
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())
|
|
|
|
|
2023-08-29 18:46:26 +00:00
|
|
|
let minimalOptions = PresentationOptions(outputVersion: ovNone)
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
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())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "- 1\n- 2\n", PresentationOptions(outputVersion: ovNone, containers: cBlock))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
test "Forced multiline flow sequence":
|
|
|
|
var input = inputSingle(startSeqEvent(), scalarEvent("1"), scalarEvent("2"), endSeqEvent())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "[\n 1,\n 2\n]\n", PresentationOptions(outputVersion: ovNone, condenseFlow: false))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
test "Compact flow mapping":
|
|
|
|
var input = inputSingle(startMapEvent(), scalarEvent("1"), scalarEvent("2"), endMapEvent())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "{1: 2}\n", PresentationOptions(outputVersion: ovNone, containers: cFlow))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
test "Simple block mapping":
|
|
|
|
var input = inputSingle(startMapEvent(), scalarEvent("1"), scalarEvent("2"), endMapEvent())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "1: 2\n", PresentationOptions(outputVersion: ovNone))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
test "Forced multiline flow mapping":
|
|
|
|
var input = inputSingle(startMapEvent(), scalarEvent("1"), scalarEvent("2"), endMapEvent())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "{\n 1: 2\n}\n", PresentationOptions(outputVersion: ovNone, condenseFlow: false, containers: cFlow))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
test "Forced JSON mapping":
|
|
|
|
var input = inputSingle(startMapEvent(), scalarEvent("1"), scalarEvent("2"), endMapEvent())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "{\n \"1\": 2\n}\n", PresentationOptions(outputVersion: ovNone, condenseFlow: false, containers: cFlow, quoting: sqJson))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
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())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "a:\n - 1\n - 2\n", PresentationOptions(outputVersion: ovNone, containers: cBlock))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
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())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "a:\n - 1\n", PresentationOptions(outputVersion: ovNone, containers: cBlock))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
test "Compact notation with 4 spaces indentation":
|
|
|
|
var input = inputSingle(startSeqEvent(), scalarEvent("a"), startMapEvent(), scalarEvent("1"), scalarEvent("2"),
|
|
|
|
scalarEvent("3"), scalarEvent("4"), endMapEvent(), endSeqEvent())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "- a\n- 1: 2\n 3: 4\n", PresentationOptions(outputVersion: ovNone, indentationStep: 4))
|
2023-07-31 17:16:24 +00:00
|
|
|
|
|
|
|
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())
|
2023-08-29 18:46:26 +00:00
|
|
|
assertOutput(input, "root:\n - a\n - 1: 2\n 3: 4\n", PresentationOptions(outputVersion: ovNone, indentationStep: 4))
|