2016-10-09 13:05:18 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
|
|
|
# (c) Copyright 2016 Felix Krause
|
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2020-11-04 15:40:37 +00:00
|
|
|
import os, terminal, strutils, streams, macros, unittest, sets
|
2016-10-09 13:05:18 +00:00
|
|
|
import testEventParser, commonTestUtils
|
2023-08-29 18:46:26 +00:00
|
|
|
import ../yaml/[ data, parser, stream ]
|
2016-10-09 13:05:18 +00:00
|
|
|
|
2016-12-12 18:29:39 +00:00
|
|
|
const
|
|
|
|
testSuiteFolder = "yaml-test-suite"
|
2016-10-09 13:05:18 +00:00
|
|
|
|
|
|
|
proc echoError(msg: string) =
|
|
|
|
styledWriteLine(stdout, fgRed, "[error] ", fgWhite, msg, resetStyle)
|
|
|
|
|
|
|
|
|
2017-07-10 18:31:40 +00:00
|
|
|
proc parserTest(path: string, errorExpected : bool): bool =
|
2016-10-09 13:05:18 +00:00
|
|
|
var
|
2020-11-04 15:40:37 +00:00
|
|
|
parser: YamlParser
|
2021-03-23 17:51:05 +00:00
|
|
|
parser.init()
|
2020-11-04 15:40:37 +00:00
|
|
|
var
|
2016-10-09 13:05:18 +00:00
|
|
|
actualIn = newFileStream(path / "in.yaml")
|
|
|
|
actual = parser.parse(actualIn)
|
|
|
|
expectedIn = newFileStream(path / "test.event")
|
2021-03-23 17:51:05 +00:00
|
|
|
expected = parseEventStream(expectedIn)
|
2016-10-09 13:05:18 +00:00
|
|
|
defer:
|
|
|
|
actualIn.close()
|
|
|
|
expectedIn.close()
|
|
|
|
var i = 1
|
|
|
|
try:
|
2020-11-04 15:40:37 +00:00
|
|
|
while true:
|
2016-10-09 13:05:18 +00:00
|
|
|
let actualEvent = actual.next()
|
|
|
|
let expectedEvent = expected.next()
|
|
|
|
if expectedEvent != actualEvent:
|
2017-07-10 18:31:40 +00:00
|
|
|
result = errorExpected
|
|
|
|
if not result:
|
2020-11-04 21:47:52 +00:00
|
|
|
echoError("At event #" & $i &
|
|
|
|
": Actual events do not match expected events")
|
|
|
|
echo ".. expected event:"
|
|
|
|
echo " ", expectedEvent
|
|
|
|
echo ".. actual event:"
|
|
|
|
echo " ", actualEvent
|
|
|
|
echo ".. difference:"
|
|
|
|
stdout.write(" ")
|
2017-07-10 18:31:40 +00:00
|
|
|
printDifference(expectedEvent, actualEvent)
|
2020-11-04 21:47:52 +00:00
|
|
|
|
2017-07-10 18:31:40 +00:00
|
|
|
return
|
2016-10-09 13:05:18 +00:00
|
|
|
i.inc()
|
2020-11-04 15:40:37 +00:00
|
|
|
if actualEvent.kind == yamlEndStream:
|
|
|
|
break
|
2017-07-10 18:31:40 +00:00
|
|
|
result = not errorExpected
|
|
|
|
if not result:
|
|
|
|
echo "Expected error, but parsed without error."
|
2023-03-18 12:54:45 +00:00
|
|
|
except CatchableError as e:
|
2017-07-10 18:31:40 +00:00
|
|
|
result = errorExpected
|
|
|
|
if not result:
|
2020-11-04 21:47:52 +00:00
|
|
|
echoError("Caught an exception at event #" & $i &
|
|
|
|
" test was not successful")
|
2017-07-10 18:31:40 +00:00
|
|
|
if e.parent of YamlParserError:
|
|
|
|
let pe = (ref YamlParserError)(e.parent)
|
2020-11-04 15:40:37 +00:00
|
|
|
echo "line ", pe.mark.line, ", column ", pe.mark.column, ": ", pe.msg
|
2017-07-10 18:31:40 +00:00
|
|
|
echo pe.lineContent
|
|
|
|
else: echo e.msg
|
2016-10-09 13:05:18 +00:00
|
|
|
|
|
|
|
macro genTests(): untyped =
|
2016-10-09 13:30:50 +00:00
|
|
|
let
|
2017-02-14 21:06:48 +00:00
|
|
|
pwd = staticExec("pwd").strip
|
2016-12-12 18:29:39 +00:00
|
|
|
absolutePath = '"' & (pwd / testSuiteFolder) & '"'
|
2016-10-09 13:53:47 +00:00
|
|
|
echo "[tparser] Generating tests from " & absolutePath
|
2017-07-10 18:31:40 +00:00
|
|
|
discard staticExec("git submodule init && git submodule update --remote")
|
|
|
|
|
2021-10-22 15:42:48 +00:00
|
|
|
var ignored = toHashSet([".git", "name", "tags", "meta"])
|
2023-03-10 21:21:18 +00:00
|
|
|
|
|
|
|
proc genTest(target: var NimNode, dirPath: string, testId: string) {.compileTime.} =
|
|
|
|
let title = slurp(dirPath / "===")
|
|
|
|
|
|
|
|
target.add(newCall("test",
|
|
|
|
newLit(strip(title) & " [" &
|
|
|
|
testId & ']'), newCall("doAssert", newCall("parserTest",
|
|
|
|
newLit(dirPath), newLit(fileExists(dirPath / "error"))))))
|
|
|
|
|
2016-10-09 13:05:18 +00:00
|
|
|
result = newStmtList()
|
2023-03-10 21:21:18 +00:00
|
|
|
|
2016-10-09 13:53:47 +00:00
|
|
|
# walkDir for some crude reason does not work with travis build
|
|
|
|
let dirItems = staticExec("ls -1d " & absolutePath / "*")
|
|
|
|
for dirPath in dirItems.splitLines():
|
2017-02-14 21:06:48 +00:00
|
|
|
if dirPath.strip.len == 0: continue
|
2017-07-10 18:31:40 +00:00
|
|
|
let testId = dirPath[^4..^1]
|
|
|
|
if ignored.contains(testId): continue
|
2023-03-10 21:21:18 +00:00
|
|
|
if fileExists(dirPath / "==="):
|
|
|
|
genTest(result, dirPath, testId)
|
|
|
|
else:
|
|
|
|
let testItems = staticExec("ls -1d " & dirPath / "*")
|
|
|
|
var start = len(dirPath) + 1
|
|
|
|
for itemPath in testItems.splitLines():
|
|
|
|
if itemPath.strip.len == 0: continue
|
|
|
|
let testItemId = testId / itemPath[start..^1]
|
|
|
|
if fileExists(itemPath / "==="):
|
|
|
|
genTest(result, itemPath, testItemId)
|
|
|
|
|
|
|
|
|
2016-12-12 18:29:39 +00:00
|
|
|
result = newCall("suite", newLit("Parser Tests (from yaml-test-suite)"), result)
|
2016-10-09 13:05:18 +00:00
|
|
|
|
2017-02-14 21:06:48 +00:00
|
|
|
genTests()
|