Skip to content

Commit

Permalink
Nim regression test for canBeImplicit
Browse files Browse the repository at this point in the history
  • Loading branch information
flyx committed Dec 21, 2023
1 parent c7e5a8a commit db088ba
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ task build, "Compile the YAML module into a library":
task test, "Run all tests":
--r
--verbosity:0
setCommand "c", "test/tests"
setCommand "c", "test/tnimregress"

task lexerTests, "Run lexer tests":
--r
Expand Down
46 changes: 46 additions & 0 deletions test/tnimregress.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest, macros

type
Person = object
name: string

ContainerKind = enum
ckString, ckInt, ckBool, ckPerson, ckNone

Container = object
case kind: ContainerKind
of ckString:
strVal: string
of ckInt:
intVal: int
of ckBool:
boolVal: bool
of ckPerson:
personVal: Person
of ckNone:
discard

proc recListLen(n: NimNode): int {.compileTime.} =
if n.kind == nnkRecList: result = n.len
else: result = 1

proc canBeImplicit(t: typedesc): string {.compileTime.} =
## returns empty string if type can be implicit, else the reason why it can't
let tDesc = getType(t)
if tDesc.kind != nnkObjectTy: return "type is not an object"
if tDesc[2].len != 1 or tDesc[2][0].kind != nnkRecCase:
return "type doesn't exclusively contain record case"
var foundEmptyBranch = false
for i in 1.. tDesc[2][0].len - 1:
case tDesc[2][0][i][1].recListlen # branch contents
of 0:
if foundEmptyBranch: return "record case has more than one empty branch"
else: foundEmptyBranch = true
of 1: discard
else: return "record case contains more than one field"

suite "Nim Regression Tests":
test "canBeImplicit":
const res = canBeImplicit(Container)
assert len(res) == 0, "unexpected error for canBeImplicit: " & res

0 comments on commit db088ba

Please sign in to comment.