Skip to content

Commit

Permalink
bugfixes and test infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Jul 21, 2024
1 parent a0d642a commit 4b54948
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/nifbuilder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ proc addStrLit*(b: var Builder; s: string; suffix = "") =
addSep b
b.put '"'
for c in s:
if c in ControlChars:
if needsEscape c:
b.escape c
else:
b.put c
Expand Down
28 changes: 28 additions & 0 deletions tests/tester.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import std / [os, strutils]
import "../src/lib" / [nifreader, nifbuilder, nif_linkedtree]

const
Expand Down Expand Up @@ -145,3 +146,30 @@ proc testNifBuilder() =
assert buildSomething(b) == ExpectedNifBuilderResult

testNifBuilder()

proc fatal(msg: string) = quit "FAILURE " & msg

proc exec(cmd: string) =
if execShellCmd(cmd) != 0: fatal cmd

proc withExt(f, ext: string): string =
result = f.changeFileExt(ext)
if not fileExists(result):
fatal "cannot find output file " & result

proc testXelim() =
exec "nim c src/xelim/xelim"
var toRemove: seq[string] = @[]
for k, f in walkDir("tests/xelim"):
if f.endsWith(".nif") and not f.endsWith(".xelim.nif") and not f.endsWith(".expected.nif"):
exec "src/xelim/xelim " & f
let r = f.withExt(".xelim.nif")
let e = f.withExt(".expected.nif")
if not os.sameFileContent(r, e):
fatal "files have not the same content: " & e & " " & r
else:
toRemove.add r
for rem in toRemove:
removeFile rem

testXelim()
35 changes: 35 additions & 0 deletions tests/xelim/t1.expected.nif
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

(stmts
(var :tmp.0.01 . .
(auto) .)
(if
(elif
(and
(gt 4 5)
(le 2 3))
(stmts
(asgn tmp.0.01
(call bar))))
(else
(stmts
(var :tmp.1.01 . .
(auto) .)
(if
(elif cond
(stmts
(asgn tmp.1.01 "action")))
(else
(stmts
(asgn tmp.1.01 "abc"))))
(asgn tmp.0.01 tmp.1.01))))
(let :x.1 . . . tmp.0.01)
(while
(true)
(stmts
(call echo "hi\00")
(if
(elif
(lt 4 5)
(call echo "body"))
(else
(break))))))
5 changes: 4 additions & 1 deletion tests/xelim/t1.nif
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(.nif24)
(stmts
(let :x.1 ... (if (elif (plus 4 5) (call bar)) (else (call baz))))
(let :x.1 ... (if (elif (and (gt 4 5) (le 2 3)) (call bar)) (else
(if (elif cond "action") (else "abc"))
)))
(while (expr (call echo "hi\00") (lt 4 5)) (call echo "body"))
)

0 comments on commit 4b54948

Please sign in to comment.