-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nim regression test for canBeImplicit
- Loading branch information
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|