Skip to content

Commit

Permalink
0.1.2: any statement is a test
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Aug 5, 2020
1 parent 222671f commit 7fe0a0a
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 72 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Because `testament` and `testutils` are going nowhere fast.

## Goals

- we expect everything to work; any statement is a test
- better stack traces and test source output
- less magical syntax and less output omission
- aim to run many cheap tests in fewer files
Expand All @@ -33,14 +34,16 @@ expandMacros:
block goats:
## this is a test of goats
discard
block pigs:
## a test of pigs
discard
var r = 3
block sometimes_the_wolf_is_nice:
break
assert true
inc r
block sheepies:
Expand All @@ -61,7 +64,7 @@ expandMacros:
block:
discard "unnamed test"
test "a test: block":
test "a test: block is fine":
discard
block omission:
Expand All @@ -84,6 +87,8 @@ expandMacros:
block assertions:
assert 2 == 4 div 2
assert 2 != 4 div 2
assert "any statement is a test" != ""
```

![demonstration](docs/demo.svg "demonstration")
Expand Down
97 changes: 50 additions & 47 deletions docs/demo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/testes.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ <h1><a class="toc-backref" href="#18">Templates</a></h1>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-08-05 02:25:15 UTC</small>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-08-05 20:11:11 UTC</small>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/theindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ <h1 class="title">Index</h1>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-08-05 02:25:15 UTC</small>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-08-05 20:11:11 UTC</small>
</div>
</div>
</div>
Expand Down
46 changes: 28 additions & 18 deletions testes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,22 @@ proc wrapExcept(t: Test): NimNode =

proc makeTest(n: NimNode; name: string): Test =
assert not n.isNil
result = Test(name: name, orig: copyNimTree(n))
result = Test(name: name, orig: n)
let beuno = genSym(nskLabel, "beuno") # all good, bro
let arrrg = genSym(nskLabel, "arrrg") # bad news, pal

# emit a success if we exited the block normally
n.add result.success
n.add nnkBreakStmt.newTree(arrrg)
result.n = nnkBlockStmt.newTree(beuno, n)
result.n = copyNimTree(n).newStmtList
result.n.add result.success

# emit a failure if we broke out of the success block
result.n = nnkBlockStmt.newTree(arrrg,
newStmtList(result.n, result.failure))
when false:
# emit a success if we exited the block normally
n.add result.success
n.add nnkBreakStmt.newTree(arrrg)
result.n = nnkBlockStmt.newTree(beuno, n)

# emit a failure if we broke out of the success block
result.n = nnkBlockStmt.newTree(arrrg,
newStmtList(result.n, result.failure))

# wrap it to catch any exceptions
result.n = result.wrapExcept
Expand All @@ -180,8 +184,9 @@ proc makeTest(n: NimNode; name: string): Test =
when not defined(release):
result.n = nnkWhenStmt.newTree(
nnkElifBranch.newTree(
newCall(ident"compiles", nnkBlockStmt.newTree(newEmptyNode(),
result.orig)),
newCall(ident"compiles",
nnkBlockStmt.newTree(genSym(nskLabel, "compiles"),
newStmtList(result.orig))),
result.n),
nnkElse.newTree(result.compilerr))

Expand Down Expand Up @@ -217,7 +222,7 @@ proc findName(n: NimNode; index: int): string =
## generate a name for a test block
assert not n.isNil
block:
if len(n) == 2:
if len(n) == 2 and n.kind == nnkBlockStmt:
# grab the body of the block,
var body = n.last
# and the first node
Expand All @@ -235,6 +240,9 @@ proc findName(n: NimNode; index: int): string =
"test #" & $index & " " & shortenRepr(n)
# and we're done.
break
else:
result = shortenRepr(n)
break
# else we had some kind of parse error
echo treeRepr(n)
result = "test #" & $index & " (parse error)"
Expand All @@ -249,7 +257,8 @@ const
nnkBreakStmt, nnkAsmStmt, nnkImportStmt, nnkImportExceptStmt,
nnkExportStmt, nnkExportExceptStmt, nnkFromStmt, nnkIncludeStmt,
nnkTypeSection, nnkMixinStmt, nnkBindStmt, nnkProcDef, nnkIteratorDef,
nnkConverterDef, nnkTemplateDef, nnkFuncDef, nnkMacroDef
nnkConverterDef, nnkTemplateDef, nnkFuncDef, nnkMacroDef, nnkCommand,
nnkCall

}

Expand All @@ -259,7 +268,8 @@ const
nnkBlockStmt, nnkIfStmt, nnkWhileStmt, nnkForStmt, nnkTryStmt,
nnkReturnStmt, nnkYieldStmt, nnkDiscardStmt, nnkContinueStmt,
nnkAsmStmt, nnkImportStmt, nnkImportExceptStmt, nnkExportStmt,
nnkExportExceptStmt, nnkFromStmt, nnkIncludeStmt
nnkExportExceptStmt, nnkFromStmt, nnkIncludeStmt, nnkCommand,
nnkCall, nnkWhenStmt

}

Expand All @@ -268,18 +278,18 @@ macro testes*(tests: untyped) =
result = newStmtList()
for index, n in pairs(tests):
var n = n.rewriteTestBlock
if n.kind notin testable:
result.add output(repr(n).prefixLines("").newLit)
result.add n
else:
if n.kind in testable:
var test: Test
if len(n) < 2 or len(n.last) == 0:
if false and len(n) < 2 or len(n.last) == 0:
test.name = "test #$1 omitted" % [ $index ]
test.n = test.output("🔵 " & test.name)
else:
let name = findName(n, index)
test = makeTest(n, name)
result.add test.n
else:
result.add output(repr(n).prefixLines("").newLit)
result.add n

template suite*(title, tests: untyped): untyped =
## suite, suite testes
Expand Down
2 changes: 1 addition & 1 deletion testes.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.1.1"
version = "0.1.2"
author = "disruptek"
description = "a cure for salty testes"
license = "MIT"
Expand Down
8 changes: 6 additions & 2 deletions tests/testicles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ expandMacros:

block goats:
## this is a test of goats
discard

block pigs:
## a test of pigs
discard

var r = 3

block sometimes_the_wolf_is_nice:
break
assert true
inc r

block sheepies:
Expand All @@ -33,7 +35,7 @@ expandMacros:
block:
discard "unnamed test"

test "a test: block":
test "a test: block is fine":
discard

block omission:
Expand All @@ -56,3 +58,5 @@ expandMacros:
block assertions:
assert 2 == 4 div 2
assert 2 != 4 div 2

assert "any statement is a test" != ""

0 comments on commit 7fe0a0a

Please sign in to comment.