mirror of https://github.com/status-im/NimYAML.git
Integrated parser and quickstart tests
* Now executed when calling `nim tests` * Refactored parser tests to use unittest * Fixed some pwd issues in tests
This commit is contained in:
parent
4bde3a7986
commit
846b836e92
|
@ -25,15 +25,16 @@ available as tags in this repository and can be fetched via nimble:
|
||||||
## Developers
|
## Developers
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
nim tests # runs unit tests (serialization, dom, json)
|
nim tests # runs all tests
|
||||||
# for parser tests, see yamlTestSuite
|
nim lexerTests # run lexer tests
|
||||||
|
nim parserTests # run parser tests (git-clones yaml-dev-kit)
|
||||||
nim serializationTests # runs serialization tests
|
nim serializationTests # runs serialization tests
|
||||||
|
nim quickstartTests # run tests for quickstart snippets from documentation
|
||||||
nim documentation # builds documentation to folder docout
|
nim documentation # builds documentation to folder docout
|
||||||
nim server # builds the REST server used for the testing ground
|
nim server # builds the REST server used for the testing ground
|
||||||
nim bench # runs benchmarks, requires libyaml
|
nim bench # runs benchmarks, requires libyaml
|
||||||
nim clean # guess
|
nim clean # guess
|
||||||
nim build # build a library
|
nim build # build a library
|
||||||
nim yamlTestSuite # execute YAML test suite (git-clones yaml-dev-kit)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
NimYAML needs at least Nim 0.15.0 in order to compile.
|
NimYAML needs at least Nim 0.15.0 in order to compile.
|
||||||
|
|
16
config.nims
16
config.nims
|
@ -8,15 +8,25 @@ task tests, "Run all tests":
|
||||||
--verbosity:0
|
--verbosity:0
|
||||||
setCommand "c", "test/tests"
|
setCommand "c", "test/tests"
|
||||||
|
|
||||||
task yamlTestSuite, "Run YAML 1.2 test suite":
|
task lexerTests, "Run lexer tests":
|
||||||
--r
|
--r
|
||||||
--verbosity:0
|
--verbosity:0
|
||||||
setCommand "c", "test/yamlTestSuite"
|
setCommand "c", "test/tlex"
|
||||||
|
|
||||||
|
task parserTests, "Run parser tests":
|
||||||
|
--r
|
||||||
|
--verbosity:0
|
||||||
|
setCommand "c", "test/tparser"
|
||||||
|
|
||||||
task serializationTests, "Run serialization tests":
|
task serializationTests, "Run serialization tests":
|
||||||
--r
|
--r
|
||||||
--verbosity:0
|
--verbosity:0
|
||||||
setCommand "c", "test/serializing"
|
setCommand "c", "test/tserialization"
|
||||||
|
|
||||||
|
task quickstartTests, "Run quickstart tests":
|
||||||
|
--r
|
||||||
|
--verbosity:0
|
||||||
|
setCommand "c", "test/tquickstart"
|
||||||
|
|
||||||
task documentation, "Generate documentation":
|
task documentation, "Generate documentation":
|
||||||
exec "mkdir -p docout"
|
exec "mkdir -p docout"
|
||||||
|
|
|
@ -4,4 +4,4 @@
|
||||||
# See the file "copying.txt", included in this
|
# See the file "copying.txt", included in this
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
|
|
||||||
import tlex, tjson, tserialization, tdom
|
import tlex, tjson, tserialization, tdom, tparser, tquickstart
|
|
@ -0,0 +1,106 @@
|
||||||
|
# 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"
|
||||||
|
|
||||||
|
const devKitFolder = "yaml-dev-kit"
|
||||||
|
|
||||||
|
proc echoError(msg: string) =
|
||||||
|
styledWriteLine(stdout, fgRed, "[error] ", fgWhite, msg, resetStyle)
|
||||||
|
|
||||||
|
proc ensureDevKitCloneCorrect() {.compileTime.} =
|
||||||
|
if dirExists(devKitFolder):
|
||||||
|
var isCorrectClone = true
|
||||||
|
if dirExists(devKitFolder / ".git"):
|
||||||
|
let remoteUrl =
|
||||||
|
staticExec("cd " & devKitFolder & " && git remote get-url origin")
|
||||||
|
if remoteUrl != "https://github.com/ingydotnet/yaml-dev-kit.git":
|
||||||
|
isCorrectClone = false
|
||||||
|
let branches = staticExec("cd " & devKitFolder & " && git branch")
|
||||||
|
if "* data" notin branches.splitLines():
|
||||||
|
isCorrectClone = false
|
||||||
|
if isCorrectClone:
|
||||||
|
let updateOutput = staticExec("git pull")
|
||||||
|
#if uError != 0:
|
||||||
|
# echo "could not update yaml-dev-kit! please fix this problem and compile again."
|
||||||
|
# echo "output:\n"
|
||||||
|
# echo "$ git pull"
|
||||||
|
# echo updateOutput
|
||||||
|
# quit 1
|
||||||
|
else:
|
||||||
|
echo devKitFolder, " exists, but is not in expected state. Make sure it is a git repo,"
|
||||||
|
echo "cloned from https://github.com/ingydotnet/yaml-dev-kit.git, and the data branch"
|
||||||
|
echo "is active. Alternatively, delete the folder " & devKitFolder & '.'
|
||||||
|
quit 1
|
||||||
|
else:
|
||||||
|
let cloneOutput = staticExec("git clone https://github.com/ingydotnet/yaml-dev-kit.git -b data")
|
||||||
|
#if cError != 0:
|
||||||
|
# echo "could not clone https://github.com/ingydotnet/yaml-dev-kit.git. Make sure"
|
||||||
|
# echo "you are connected to the internet and your proxy settings are correct. output:\n"
|
||||||
|
# echo "$ git clone https://github.com/ingydotnet/yaml-dev-kit.git"
|
||||||
|
# echo cloneOutput
|
||||||
|
# quit 1
|
||||||
|
|
||||||
|
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 =
|
||||||
|
ensureDevKitCloneCorrect()
|
||||||
|
result = newStmtList()
|
||||||
|
let pwd = staticExec("pwd")
|
||||||
|
for kind, dirName in walkDir(devKitFolder, true):
|
||||||
|
if kind == pcDir:
|
||||||
|
if dirName in [".git", "name", "tags", "meta"]: continue
|
||||||
|
# see https://github.com/nim-lang/Nim/issues/4871
|
||||||
|
let title = slurp(pwd / devKitFolder / dirName / "===")
|
||||||
|
result.add(newCall("test",
|
||||||
|
newLit(strip(title) & " [" &
|
||||||
|
dirName & ']'), newCall("doAssert", newCall("parserTest",
|
||||||
|
newLit(devKitFolder / dirName)))))
|
||||||
|
result = newCall("suite", newLit("Parser Tests (from yaml-dev-kit)"), result)
|
||||||
|
|
||||||
|
genTests()
|
|
@ -1,23 +1,21 @@
|
||||||
import unittest, os, osproc, macros, strutils, streams
|
import unittest, os, osproc, macros, strutils, streams
|
||||||
|
|
||||||
proc inputTest(path: string): bool =
|
proc inputTest(basePath, path: string): bool =
|
||||||
let
|
let
|
||||||
inFileOrig = path / "01-in.yaml"
|
absolutePath = basePath / path
|
||||||
inFileDest = path / "in.yaml"
|
inFileOrig = absolutePath / "01-in.yaml"
|
||||||
codeFileOrig = path / "00-code.nim"
|
inFileDest = absolutePath / "in.yaml"
|
||||||
codeFileDest = path / "code.nim"
|
codeFileOrig = absolutePath / "00-code.nim"
|
||||||
exeFileDest = when defined(windows): path / "code.exe" else: path / "code"
|
codeFileDest = absolutePath / "code.nim"
|
||||||
currentDir = getCurrentDir()
|
exeFileDest = when defined(windows): absolutePath / "code.exe" else:
|
||||||
basePath = currentDir / ".."
|
absolutePath / "code"
|
||||||
absolutePath = currentDir / path
|
|
||||||
copyFile(inFileOrig, inFileDest)
|
copyFile(inFileOrig, inFileDest)
|
||||||
copyFile(codeFileOrig, codeFileDest)
|
copyFile(codeFileOrig, codeFileDest)
|
||||||
defer:
|
defer:
|
||||||
removeFile(inFileDest)
|
removeFile(inFileDest)
|
||||||
removeFile(codeFileDest)
|
removeFile(codeFileDest)
|
||||||
var process = startProcess("nim c --hints:off -p:" & escape(basePath) &
|
var process = startProcess("nim c --hints:off -p:" & escape(basePath) &
|
||||||
" code.nim", path, [], nil, {poStdErrToStdOut, poEvalCommand})
|
" code.nim", absolutePath, [], nil, {poStdErrToStdOut, poEvalCommand})
|
||||||
setCurrentDir(currentDir) # workaround for https://github.com/nim-lang/Nim/issues/4867
|
|
||||||
defer:
|
defer:
|
||||||
process.close()
|
process.close()
|
||||||
if process.waitForExit() != 0:
|
if process.waitForExit() != 0:
|
||||||
|
@ -30,7 +28,6 @@ proc inputTest(path: string): bool =
|
||||||
process.close()
|
process.close()
|
||||||
process = startProcess(absolutePath / "code", absolutePath, [], nil,
|
process = startProcess(absolutePath / "code", absolutePath, [], nil,
|
||||||
{poStdErrToStdOut, poEvalCommand})
|
{poStdErrToStdOut, poEvalCommand})
|
||||||
setCurrentDir(currentDir) # workaround for https://github.com/nim-lang/Nim/issues/4867
|
|
||||||
if process.waitForExit() != 0:
|
if process.waitForExit() != 0:
|
||||||
echo "executable output:"
|
echo "executable output:"
|
||||||
echo "==================\n"
|
echo "==================\n"
|
||||||
|
@ -38,22 +35,20 @@ proc inputTest(path: string): bool =
|
||||||
result = false
|
result = false
|
||||||
else: result = true
|
else: result = true
|
||||||
|
|
||||||
proc outputTest(path: string): bool =
|
proc outputTest(basePath, path: string): bool =
|
||||||
let
|
let
|
||||||
codeFileOrig = path / "00-code.nim"
|
absolutePath = basePath / path
|
||||||
codeFileDest = path / "code.nim"
|
codeFileOrig = absolutePath / "00-code.nim"
|
||||||
exeFileDest = when defined(windows): path / "code.exe" else: path / "code"
|
codeFileDest = absolutePath / "code.nim"
|
||||||
currentDir = getCurrentDir()
|
exeFileDest = when defined(windows): absolutePath / "code.exe" else:
|
||||||
basePath = currentDir / ".."
|
absolutePath / "code"
|
||||||
absolutePath = currentDir / path
|
outFileExpected = absolutePath / "01-out.yaml"
|
||||||
outFileExpected = path / "01-out.yaml"
|
outFileActual = absolutePath / "out.yaml"
|
||||||
outFileActual = path / "out.yaml"
|
|
||||||
copyFile(codeFileOrig, codeFileDest)
|
copyFile(codeFileOrig, codeFileDest)
|
||||||
defer: removeFile(codeFileDest)
|
defer: removeFile(codeFileDest)
|
||||||
var process = startProcess("nim c --hints:off -p:" & escape(basePath) &
|
var process = startProcess("nim c --hints:off -p:" & escape(basePath) &
|
||||||
" code.nim", path, [], nil, {poStdErrToStdOut, poEvalCommand})
|
" code.nim", absolutePath, [], nil, {poStdErrToStdOut, poEvalCommand})
|
||||||
defer: process.close()
|
defer: process.close()
|
||||||
setCurrentDir(currentDir) # workaround for https://github.com/nim-lang/Nim/issues/4867
|
|
||||||
if process.waitForExit() != 0:
|
if process.waitForExit() != 0:
|
||||||
echo "compiler output:"
|
echo "compiler output:"
|
||||||
echo "================\n"
|
echo "================\n"
|
||||||
|
@ -64,7 +59,6 @@ proc outputTest(path: string): bool =
|
||||||
process.close()
|
process.close()
|
||||||
process = startProcess(absolutePath / "code", absolutePath, [], nil,
|
process = startProcess(absolutePath / "code", absolutePath, [], nil,
|
||||||
{poStdErrToStdOut, poEvalCommand})
|
{poStdErrToStdOut, poEvalCommand})
|
||||||
setCurrentDir(currentDir) # workaround for https://github.com/nim-lang/Nim/issues/4867
|
|
||||||
if process.waitForExit() != 0:
|
if process.waitForExit() != 0:
|
||||||
echo "executable output:"
|
echo "executable output:"
|
||||||
echo "==================\n"
|
echo "==================\n"
|
||||||
|
@ -108,13 +102,17 @@ proc outputTest(path: string): bool =
|
||||||
proc testsFor(path: string, root: bool = true, titlePrefix: string = ""):
|
proc testsFor(path: string, root: bool = true, titlePrefix: string = ""):
|
||||||
NimNode {.compileTime.} =
|
NimNode {.compileTime.} =
|
||||||
result = newStmtList()
|
result = newStmtList()
|
||||||
let title = titlePrefix & slurp(path / "title").splitLines()[0]
|
let
|
||||||
|
baseDir = staticExec("pwd")
|
||||||
|
title = titlePrefix & slurp(baseDir / path / "title").splitLines()[0]
|
||||||
if fileExists(path / "00-code.nim"):
|
if fileExists(path / "00-code.nim"):
|
||||||
var test = newCall("test", newLit(title))
|
var test = newCall("test", newLit(title))
|
||||||
if fileExists(path / "01-in.yaml"):
|
if fileExists(path / "01-in.yaml"):
|
||||||
test.add(newCall("doAssert", newCall("inputTest", newLit(path))))
|
test.add(newCall("doAssert", newCall("inputTest", newLit(baseDir),
|
||||||
|
newLit(path))))
|
||||||
elif fileExists(path / "01-out.yaml"):
|
elif fileExists(path / "01-out.yaml"):
|
||||||
test.add(newCall("doAssert", newCall("outputTest", newLit(path))))
|
test.add(newCall("doAssert", newCall("outputTest", newLit(baseDir),
|
||||||
|
newLit(path))))
|
||||||
else:
|
else:
|
||||||
echo "Error: neither 01-in.yaml nor 01-out.yaml exists in " & path & '!'
|
echo "Error: neither 01-in.yaml nor 01-out.yaml exists in " & path & '!'
|
||||||
quit 1
|
quit 1
|
||||||
|
@ -126,6 +124,6 @@ proc testsFor(path: string, root: bool = true, titlePrefix: string = ""):
|
||||||
if root:
|
if root:
|
||||||
result = newCall("suite", newLit(title), result)
|
result = newCall("suite", newLit(title), result)
|
||||||
|
|
||||||
macro genTests(): untyped = testsFor("../doc/snippets/quickstart")
|
macro genTests(): untyped = testsFor("doc/snippets/quickstart")
|
||||||
|
|
||||||
genTests()
|
genTests()
|
|
@ -1,83 +0,0 @@
|
||||||
# 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, terminal, strutils, streams
|
|
||||||
import testEventParser, commonTestUtils
|
|
||||||
import "../yaml"
|
|
||||||
|
|
||||||
const gitCmd =
|
|
||||||
"git clone https://github.com/ingydotnet/yaml-dev-kit.git -b data"
|
|
||||||
|
|
||||||
proc echoError(msg: string) =
|
|
||||||
styledWriteLine(stdout, fgRed, "[error] ", fgWhite, msg, resetStyle)
|
|
||||||
|
|
||||||
proc echoInfo(msg: string) =
|
|
||||||
styledWriteLine(stdout, fgGreen, "[info] ", fgWhite, msg, resetStyle)
|
|
||||||
|
|
||||||
removeDir("yaml-dev-kit")
|
|
||||||
if execShellCmd(gitCmd) != 0:
|
|
||||||
echoError("Could not check out yaml-dev-kit (no internet connection?)")
|
|
||||||
quit(1)
|
|
||||||
|
|
||||||
var gotErrors = false
|
|
||||||
|
|
||||||
for kind, dirPath in walkDir("yaml-dev-kit"):
|
|
||||||
block curTest:
|
|
||||||
if kind == pcDir:
|
|
||||||
if dirPath[^4..^1] in [".git", "name", "tags", "meta"]: continue
|
|
||||||
var
|
|
||||||
tagLib = initExtendedTagLibrary()
|
|
||||||
parser = newYamlParser(tagLib)
|
|
||||||
actualIn = newFileStream(dirPath / "in.yaml")
|
|
||||||
actual = parser.parse(actualIn)
|
|
||||||
expectedIn = newFileStream(dirPath / "test.event")
|
|
||||||
expected = parseEventStream(expectedIn, tagLib)
|
|
||||||
styledWriteLine(stdout, fgBlue, "[test] ", fgWhite, dirPath[^4..^1],
|
|
||||||
": ", strip(readFile(dirPath / "===")), resetStyle)
|
|
||||||
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)
|
|
||||||
gotErrors = true
|
|
||||||
actualIn.close()
|
|
||||||
expectedIn.close()
|
|
||||||
break curTest
|
|
||||||
let expectedEvent = expected.next()
|
|
||||||
if expectedEvent != actualEvent:
|
|
||||||
printDifference(expectedEvent, actualEvent)
|
|
||||||
echoError("At token #" & $i &
|
|
||||||
": Actual tokens do not match expected tokens")
|
|
||||||
gotErrors = true
|
|
||||||
actualIn.close()
|
|
||||||
expectedIn.close()
|
|
||||||
break curTest
|
|
||||||
i.inc()
|
|
||||||
if not expected.finished():
|
|
||||||
echoError("Got fewer tokens than expected, first missing " &
|
|
||||||
"token: " & $expected.next().kind)
|
|
||||||
gotErrors = true
|
|
||||||
except:
|
|
||||||
gotErrors = true
|
|
||||||
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")
|
|
||||||
actualIn.close()
|
|
||||||
expectedIn.close()
|
|
||||||
|
|
||||||
if gotErrors:
|
|
||||||
echoError("There were errors while running the tests")
|
|
||||||
quit(1)
|
|
||||||
else:
|
|
||||||
echoInfo("All tests were successful")
|
|
||||||
quit(0)
|
|
Loading…
Reference in New Issue