From fde6933b6d50f6d50d38c65a490622d553236da6 Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Thu, 21 Dec 2023 16:47:42 +0100 Subject: [PATCH] Nim regression test for canBeImplicit --- .github/workflows/action.yml | 14 +++-------- config.nims | 2 +- test/tnimregress.nim | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 test/tnimregress.nim diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 72b26c16..0e78fdf6 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -13,9 +13,8 @@ jobs: - windows-latest - macOS-latest nim-version: - - '2.0.x' - - stable - - devel + - '2.0.0' + - '2.0.2' steps: - name: Checkout uses: actions/checkout@v2 @@ -47,11 +46,4 @@ jobs: - name: Test run: | - nim lexerTests - nim parserTests - nim jsonTests - nim domTests - nim nativeTests - nim quickstartTests - nim hintsTests - nim presenterTests + nim test diff --git a/config.nims b/config.nims index 5ddb0b3f..5db7d417 100644 --- a/config.nims +++ b/config.nims @@ -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 diff --git a/test/tnimregress.nim b/test/tnimregress.nim new file mode 100644 index 00000000..ded9107f --- /dev/null +++ b/test/tnimregress.nim @@ -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 + \ No newline at end of file