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.
|
|
|
|
|
|
|
|
import os, osproc, terminal, strutils, streams, macros, unittest
|
|
|
|
import testEventParser, commonTestUtils
|
|
|
|
import "../yaml"
|
|
|
|
|
2016-12-12 18:29:39 +00:00
|
|
|
const
|
|
|
|
testSuiteFolder = "yaml-test-suite"
|
|
|
|
testSuiteUrl = "https://github.com/yaml/yaml-test-suite.git"
|
2016-10-09 13:05:18 +00:00
|
|
|
|
|
|
|
proc echoError(msg: string) =
|
|
|
|
styledWriteLine(stdout, fgRed, "[error] ", fgWhite, msg, resetStyle)
|
|
|
|
|
2016-12-12 18:29:39 +00:00
|
|
|
proc ensureTestSuiteCloneCorrect(pwd: string) {.compileTime.} =
|
|
|
|
let absolutePath = pwd / testSuiteFolder
|
2016-10-09 13:30:50 +00:00
|
|
|
if dirExists(absolutePath):
|
2016-10-09 13:05:18 +00:00
|
|
|
var isCorrectClone = true
|
2016-10-09 13:30:50 +00:00
|
|
|
if dirExists(absolutePath / ".git"):
|
2016-10-09 13:05:18 +00:00
|
|
|
let remoteUrl =
|
2017-02-14 21:06:48 +00:00
|
|
|
staticExec("cd \"" & absolutePath & "\" && git remote get-url origin").strip
|
2016-12-12 18:29:39 +00:00
|
|
|
if remoteUrl != testSuiteUrl:
|
2016-10-09 13:05:18 +00:00
|
|
|
isCorrectClone = false
|
2017-02-14 21:06:48 +00:00
|
|
|
let branches = staticExec("cd \"" & absolutePath & "\" && git branch").strip
|
2016-10-09 13:05:18 +00:00
|
|
|
if "* data" notin branches.splitLines():
|
|
|
|
isCorrectClone = false
|
|
|
|
if isCorrectClone:
|
2016-10-09 13:30:50 +00:00
|
|
|
let updateOutput = staticExec("cd \"" & absolutePath & "\" && git pull")
|
2016-10-09 13:05:18 +00:00
|
|
|
#if uError != 0:
|
2016-12-12 18:29:39 +00:00
|
|
|
# echo "could not update yaml-test-suite! please fix this problem and compile again."
|
2016-10-09 13:05:18 +00:00
|
|
|
# echo "output:\n"
|
|
|
|
# echo "$ git pull"
|
|
|
|
# echo updateOutput
|
|
|
|
# quit 1
|
|
|
|
else:
|
2016-12-12 18:29:39 +00:00
|
|
|
echo testSuiteFolder, " exists, but is not in expected state. Make sure it is a git repo,"
|
|
|
|
echo "cloned from ", testSuiteUrl, ", and the data branch"
|
|
|
|
echo "is active. Alternatively, delete the folder " & testSuiteFolder & '.'
|
2016-10-09 13:05:18 +00:00
|
|
|
quit 1
|
|
|
|
else:
|
2016-10-09 13:30:50 +00:00
|
|
|
let cloneOutput = staticExec("cd \"" & pwd &
|
2016-12-12 18:29:39 +00:00
|
|
|
"\" && git clone " & testSuiteUrl & " -b data")
|
2016-10-09 13:05:18 +00:00
|
|
|
#if cError != 0:
|
2016-10-09 13:37:20 +00:00
|
|
|
if not(dirExists(absolutePath)) or not(dirExists(absolutePath / ".git")) or
|
|
|
|
not(dirExists(absolutePath / "229Q")):
|
2016-12-12 18:29:39 +00:00
|
|
|
echo "could not clone ", testSuiteUrl, ". Make sure"
|
2016-10-09 13:21:27 +00:00
|
|
|
echo "you are connected to the internet and your proxy settings are correct. output:\n"
|
2016-12-12 18:29:39 +00:00
|
|
|
echo "$ git clone ", testSuiteUrl, " -b data"
|
2016-10-09 13:21:27 +00:00
|
|
|
echo cloneOutput
|
|
|
|
quit 1
|
2016-10-09 13:05:18 +00:00
|
|
|
|
|
|
|
proc parserTest(path: string): bool =
|
|
|
|
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():
|
|
|
|
echoError("At token #" & $i & ": Expected stream end, got " &
|
|
|
|
$actualEvent.kind)
|
|
|
|
return false
|
|
|
|
let expectedEvent = expected.next()
|
|
|
|
if expectedEvent != actualEvent:
|
|
|
|
printDifference(expectedEvent, actualEvent)
|
|
|
|
echoError("At token #" & $i &
|
|
|
|
": Actual tokens do not match expected tokens")
|
|
|
|
return false
|
|
|
|
i.inc()
|
|
|
|
if not expected.finished():
|
|
|
|
echoError("Got fewer tokens than expected, first missing " &
|
|
|
|
"token: " & $expected.next().kind)
|
|
|
|
return false
|
|
|
|
except:
|
|
|
|
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")
|
|
|
|
return false
|
|
|
|
result = true
|
|
|
|
|
|
|
|
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
|
2016-12-12 18:29:39 +00:00
|
|
|
ensureTestSuiteCloneCorrect(pwd)
|
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
|
2016-10-09 13:53:47 +00:00
|
|
|
if dirPath[^4..^1] in [".git", "name", "tags", "meta"]: continue
|
|
|
|
let title = slurp(dirPath / "===")
|
|
|
|
result.add(newCall("test",
|
|
|
|
newLit(strip(title) & " [" &
|
|
|
|
dirPath[^4..^1] & ']'), newCall("doAssert", newCall("parserTest",
|
|
|
|
newLit(dirPath)))))
|
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()
|