Skip to content

Commit

Permalink
revert workarounds from nim-lang#16698 and add allowPrefixMatch optio…
Browse files Browse the repository at this point in the history
…nal param to greedyOrderedSubsetLines
  • Loading branch information
timotheecour committed May 6, 2021
1 parent 2961df0 commit 87cd3c0
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 37 deletions.
10 changes: 8 additions & 2 deletions testament/lib/stdtest/testutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,25 @@ template flakyAssert*(cond: untyped, msg = "", notifySuccess = true) =
when not defined(js):
import std/strutils

proc greedyOrderedSubsetLines*(lhs, rhs: string): bool =
proc greedyOrderedSubsetLines*(lhs, rhs: string, allowPrefixMatch = false): bool =
## Returns true if each stripped line in `lhs` appears in rhs, using a greedy matching.
# xxx improve error reporting by showing the last matched pair
iterator splitLinesClosure(): string {.closure.} =
for line in splitLines(rhs.strip):
yield line
template isMatch(lhsi, rhsi): bool =
if allowPrefixMatch:
startsWith(rhsi, lhsi):
else:
lhsi == rhsi

var rhsIter = splitLinesClosure
var currentLine = strip(rhsIter())

for line in lhs.strip.splitLines:
let line = line.strip
if line.len != 0:
while line != currentLine:
while not isMatch(line, currentLine):
currentLine = strip(rhsIter())
if rhsIter.finished:
return false
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tccodecheck.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
discard """
targets: "c"
ccodecheck: "baz"
"""

Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tcolumn.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
errormsg: "undeclared identifier: 'undeclared'"
targets: "c"
line: 9
column: 7
"""
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/terrormsg.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
errormsg: "wrong error message"
targets: "c"
line: 9
column: 6
"""
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/texitcode1.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
discard """
targets: "c"
exitcode: 1
"""
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tfile.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
discard """
targets: "c"
errormsg: "undeclared identifier: 'undefined'"
file: "notthisfile.nim"
"""
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tline.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
discard """
targets: "c"
errormsg: "undeclared identifier: 'undeclared'"
line: 10
column: 6
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tmaxcodesize.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
discard """
targets: "c"
maxcodesize: 1
"""

Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tnimout.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
discard """
targets: "c"
nimout: "Hello World!"
action: compile
"""
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tnimoutfull.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
discard """
targets: "c"
nimout: '''
msg1
msg2
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/toutput.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
discard """
targets: "c"
output: '''
done
'''
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/toutputsub.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
outputsub: "something else"
targets: "c"
"""

echo "Hello World!"
1 change: 0 additions & 1 deletion testament/tests/shouldfail/treject.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
action: "reject"
targets: "c"
"""

# Because we set action="reject", we expect this line not to compile. But the
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tsortoutput.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
sortoutput: true
targets: "c"
output: '''
2
1
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/ttimeout.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
timeout: "0.1"
targets: "c"
"""

import os
Expand Down
1 change: 0 additions & 1 deletion testament/tests/shouldfail/tvalgrind.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
valgrind: true
targets: "c"
cmd: "nim $target --gc:arc -d:useMalloc $options $file"
"""

Expand Down
2 changes: 2 additions & 0 deletions tests/misc/trunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ discard """
## tests that don't quite fit the mold and are easier to handle via `execCmdEx`
## A few others could be added to here to simplify code.
## Note: this test is a bit slow but tests a lot of things; please don't disable.
## Note: if needed, we could use `matrix: "-d:case1; -d:case2"` to split this
## into several independent tests while retaining the common test helpers.

import std/[strformat,os,osproc,unittest,compilesettings]
from std/sequtils import toSeq,mapIt
Expand Down
51 changes: 31 additions & 20 deletions tests/testament/tshould_not_work.nim
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
discard """
cmd: "testament/testament --directory:testament --colors:off --backendLogging:off --nim:$nim category shouldfail"
action: compile
nimout: '''
FAIL: tests/shouldfail/tccodecheck.nim c
const expected = """
FAIL: tests/shouldfail/tccodecheck.nim
Failure: reCodegenFailure
Expected:
baz
FAIL: tests/shouldfail/tcolumn.nim c
FAIL: tests/shouldfail/tcolumn.nim
Failure: reLinesDiffer
FAIL: tests/shouldfail/terrormsg.nim c
FAIL: tests/shouldfail/terrormsg.nim
Failure: reMsgsDiffer
FAIL: tests/shouldfail/texitcode1.nim c
FAIL: tests/shouldfail/texitcode1.nim
Failure: reExitcodesDiffer
FAIL: tests/shouldfail/tfile.nim c
FAIL: tests/shouldfail/tfile.nim
Failure: reFilesDiffer
FAIL: tests/shouldfail/tline.nim c
FAIL: tests/shouldfail/tline.nim
Failure: reLinesDiffer
FAIL: tests/shouldfail/tmaxcodesize.nim c
FAIL: tests/shouldfail/tmaxcodesize.nim
Failure: reCodegenFailure
max allowed size: 1
FAIL: tests/shouldfail/tnimout.nim c
FAIL: tests/shouldfail/tnimout.nim
Failure: reMsgsDiffer
FAIL: tests/shouldfail/tnimoutfull.nim c
FAIL: tests/shouldfail/tnimoutfull.nim
Failure: reMsgsDiffer
FAIL: tests/shouldfail/toutput.nim c
FAIL: tests/shouldfail/toutput.nim
Failure: reOutputsDiffer
FAIL: tests/shouldfail/toutputsub.nim c
FAIL: tests/shouldfail/toutputsub.nim
Failure: reOutputsDiffer
FAIL: tests/shouldfail/treject.nim c
FAIL: tests/shouldfail/treject.nim
Failure: reFilesDiffer
FAIL: tests/shouldfail/tsortoutput.nim c
FAIL: tests/shouldfail/tsortoutput.nim
Failure: reOutputsDiffer
FAIL: tests/shouldfail/ttimeout.nim c
FAIL: tests/shouldfail/ttimeout.nim
Failure: reTimeout
FAIL: tests/shouldfail/tvalgrind.nim c
FAIL: tests/shouldfail/tvalgrind.nim
Failure: reExitcodesDiffer
'''
"""

import std/[os,strformat,osproc]
import stdtest/testutils

proc main =
const nim = getCurrentCompilerExe()
# TODO: bin/testament instead? like other tools (eg bin/nim, bin/nimsuggest etc)
let testamentExe = "testament/testament"
let cmd = fmt"{testamentExe} --directory:testament --colors:off --backendLogging:off --nim:{nim} category shouldfail"
let (outp, status) = execCmdEx(cmd)
doAssert status == 1, $status

let ok = greedyOrderedSubsetLines(expected, outp, allowPrefixMatch = true)
doAssert ok, &"\nexpected:\n{expected}\noutp:\n{outp}"
main()

0 comments on commit 87cd3c0

Please sign in to comment.