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.
|
|
|
|
|
2017-07-10 18:31:40 +00:00
|
|
|
import os, osproc, terminal, strutils, streams, macros, unittest, sets
|
2016-10-09 13:05:18 +00:00
|
|
|
import testEventParser, commonTestUtils
|
|
|
|
import "../yaml"
|
|
|
|
|
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
|
|
|
|
tagLib = initExtendedTagLibrary()
|
|
|
|
parser = newYamlParser(tagLib)
|
|
|
|
actualIn = newFileStream(path / "in.yaml")
|
|
|
|
actual = parser.parse(actualIn)
|
|
|
|
expectedIn = newFileStream(path / "test.event")
|
|
|
|
expected = parseEventStream(expectedIn, tagLib)
|
|
|
|
defer:
|
|
|
|
actualIn.close()
|
|
|
|
expectedIn.close()
|
|
|
|
var i = 1
|
|
|
|
try:
|
|
|
|
while not actual.finished():
|
|
|
|
let actualEvent = actual.next()
|
|
|
|
if expected.finished():
|
2017-07-10 18:31:40 +00:00
|
|
|
result = errorExpected
|
|
|
|
if not result:
|
|
|
|
echoError("At token #" & $i & ": Expected stream end, got " &
|
|
|
|
$actualEvent.kind)
|
|
|
|
return
|
2016-10-09 13:05:18 +00:00
|
|
|
let expectedEvent = expected.next()
|
|
|
|
if expectedEvent != actualEvent:
|
2017-07-10 18:31:40 +00:00
|
|
|
result = errorExpected
|
|
|
|
if not result:
|
|
|
|
printDifference(expectedEvent, actualEvent)
|
|
|
|
echoError("At token #" & $i &
|
|
|
|
": Actual tokens do not match expected tokens")
|
|
|
|
return
|
2016-10-09 13:05:18 +00:00
|
|
|
i.inc()
|
|
|
|
if not expected.finished():
|
2017-07-10 18:31:40 +00:00
|
|
|
result = errorExpected
|
|
|
|
if not result:
|
|
|
|
echoError("Got fewer tokens than expected, first missing " &
|
|
|
|
"token: " & $expected.next().kind)
|
|
|
|
return
|
|
|
|
result = not errorExpected
|
|
|
|
if not result:
|
|
|
|
echo "Expected error, but parsed without error."
|
2016-10-09 13:05:18 +00:00
|
|
|
except:
|
2017-07-10 18:31:40 +00:00
|
|
|
result = errorExpected
|
|
|
|
if not result:
|
|
|
|
let e = getCurrentException()
|
|
|
|
if e.parent of YamlParserError:
|
|
|
|
let pe = (ref YamlParserError)(e.parent)
|
|
|
|
echo "line ", pe.line, ", column ", pe.column, ": ", pe.msg
|
|
|
|
echo pe.lineContent
|
|
|
|
else: echo e.msg
|
|
|
|
echoError("Catched an exception at token #" & $i &
|
|
|
|
" test was not successful")
|
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")
|
|
|
|
|
2020-07-22 09:30:19 +00:00
|
|
|
let errorTests = toHashSet(staticExec("cd " & (absolutePath / "tags" / "error") &
|
2017-07-10 18:31:40 +00:00
|
|
|
" && ls -1d *").splitLines())
|
2020-07-22 09:30:19 +00:00
|
|
|
let ignored = toHashSet(["3MYT", "JDH8", "2EBW", "9KAX", "AB8U", "B63P", "FBC9",
|
2018-10-11 12:41:40 +00:00
|
|
|
"Q5MG", "S98Z", ".git", "name", "tags", "meta"])
|
2017-07-10 18:31:40 +00:00
|
|
|
|
2016-10-09 13:05:18 +00:00
|
|
|
result = newStmtList()
|
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
|
2016-10-09 13:53:47 +00:00
|
|
|
let title = slurp(dirPath / "===")
|
2017-07-10 18:31:40 +00:00
|
|
|
|
2016-10-09 13:53:47 +00:00
|
|
|
result.add(newCall("test",
|
|
|
|
newLit(strip(title) & " [" &
|
2017-07-10 18:31:40 +00:00
|
|
|
testId & ']'), newCall("doAssert", newCall("parserTest",
|
|
|
|
newLit(dirPath), newLit(errorTests.contains(testId))))))
|
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()
|