diff options
Diffstat (limited to 'tests/casestmt')
-rw-r--r-- | tests/casestmt/t12785.nim | 47 | ||||
-rw-r--r-- | tests/casestmt/t18964.nim | 12 | ||||
-rw-r--r-- | tests/casestmt/tcase_issues.nim | 7 | ||||
-rw-r--r-- | tests/casestmt/tcaseexpr1.nim | 26 | ||||
-rw-r--r-- | tests/casestmt/tcasestmt.nim | 79 | ||||
-rw-r--r-- | tests/casestmt/tcstring.nim | 52 | ||||
-rw-r--r-- | tests/casestmt/tincompletecaseobject.nim | 4 | ||||
-rw-r--r-- | tests/casestmt/tincompletecaseobject2.nim | 19 | ||||
-rw-r--r-- | tests/casestmt/trangeexhaustiveness.nim | 7 |
9 files changed, 177 insertions, 76 deletions
diff --git a/tests/casestmt/t12785.nim b/tests/casestmt/t12785.nim deleted file mode 100644 index bf0f30d42..000000000 --- a/tests/casestmt/t12785.nim +++ /dev/null @@ -1,47 +0,0 @@ -discard """ - cmd: '''nim c --newruntime $file''' - output: '''copied -copied -2 -copied -copied -2 -destroyed -destroyed''' -""" - -type - ObjWithDestructor = object - a: int -proc `=destroy`(self: var ObjWithDestructor) = - echo "destroyed" - -proc `=`(self: var ObjWithDestructor, other: ObjWithDestructor) = - echo "copied" - -proc test(a: range[0..1], arg: ObjWithDestructor) = - var iteration = 0 - while true: - {.computedGoto.} - - let - b = int(a) * 2 - c = a - d = arg - e = arg - - discard c - discard d - discard e - - inc iteration - - case a - of 0: - assert false - of 1: - echo b - if iteration == 2: - break - -test(1, ObjWithDestructor()) \ No newline at end of file diff --git a/tests/casestmt/t18964.nim b/tests/casestmt/t18964.nim new file mode 100644 index 000000000..1d2de2bbc --- /dev/null +++ b/tests/casestmt/t18964.nim @@ -0,0 +1,12 @@ +discard """ +errormsg: "invalid order of case branches" +""" + +import macros + +macro genCase(val: string): untyped = + result = nnkCaseStmt.newTree(val, + nnkElse.newTree(quote do: echo "else"), + nnkOfBranch.newTree(newLit("miauz"), quote do: echo "first branch")) + +genCase("miauz") diff --git a/tests/casestmt/tcase_issues.nim b/tests/casestmt/tcase_issues.nim new file mode 100644 index 000000000..20a79df2c --- /dev/null +++ b/tests/casestmt/tcase_issues.nim @@ -0,0 +1,7 @@ +discard """ + targets: "c js" +""" + +block: # bug #24031 + case 0 + else: discard \ No newline at end of file diff --git a/tests/casestmt/tcaseexpr1.nim b/tests/casestmt/tcaseexpr1.nim index 3d4b6c087..4f5bbf100 100644 --- a/tests/casestmt/tcaseexpr1.nim +++ b/tests/casestmt/tcaseexpr1.nim @@ -1,17 +1,23 @@ discard """ - errormsg: "type mismatch: got <string> but expected 'int'" - line: 33 - file: "tcaseexpr1.nim" - - errormsg: "not all cases are covered; missing: {C}" - line: 27 - file: "tcaseexpr1.nim" + cmd: "nim check $options $file" + action: "reject" + nimout: ''' +tcaseexpr1.nim(33, 10) Error: not all cases are covered; missing: {C} +tcaseexpr1.nim(39, 12) Error: type mismatch: got <string> but expected 'int literal(10)' +''' """ -# NOTE: This spec is wrong. Spec doesn't support multiple error -# messages. The first one is simply overridden by the second one. -# This just has never been noticed. + + + + + + + + + +# line 20 type E = enum A, B, C diff --git a/tests/casestmt/tcasestmt.nim b/tests/casestmt/tcasestmt.nim index b7454ef99..66de4183d 100644 --- a/tests/casestmt/tcasestmt.nim +++ b/tests/casestmt/tcasestmt.nim @@ -41,6 +41,11 @@ block t8333: case 0 of 'a': echo 0 else: echo 1 +block: # issue #11422 + var c: int = 5 + case c + of 'a' .. 'c': discard + else: discard block emptyset_when: @@ -184,33 +189,33 @@ block tcasestm: doAssert(bstatic == false) var bb: bool - doassert(not compiles( + doAssert(not compiles( bb = case str2: of "": raise newException(ValueError, "Invalid boolean") elif str.startsWith("Y"): true elif str.startsWith("N"): false )) - doassert(not compiles( + doAssert(not compiles( bb = case str2: of "Y": true of "N": false )) - doassert(not compiles( + doAssert(not compiles( bb = case str2: of "Y": true of "N": raise newException(ValueError, "N not allowed") )) - doassert(not compiles( + doAssert(not compiles( bb = case str2: of "Y": raise newException(ValueError, "Invalid Y") else: raise newException(ValueError, "Invalid N") )) - doassert(not compiles( + doAssert(not compiles( bb = case str2: of "Y": raise newException(ValueError, "Invalid Y") @@ -219,7 +224,7 @@ block tcasestm: )) - doassert(not compiles( + doAssert(not compiles( bb = case str2: of "Y": "invalid Y".quit(3) @@ -250,3 +255,65 @@ proc negativeOrNot(num: int): string = doAssert negativeOrNot(-1) == "negative" doAssert negativeOrNot(10000000) == "zero or positive" doAssert negativeOrNot(0) == "zero or positive" + +######################################################## +# issue #13490 +import strutils +func foo(input: string): int = + try: + parseInt(input) + except: + return + +func foo2(b, input: string): int = + case b: + of "Y": + for c in input: + result = if c in '0'..'9': parseInt($c) + else: break + of "N": + for c in input: + result = if c in '0'..'9': parseInt($c) + else: continue + else: return + + +static: + doAssert(foo("3") == 3) + doAssert(foo("a") == 0) + doAssert(foo2("Y", "a2") == 0) + doAssert(foo2("Y", "2a") == 2) + doAssert(foo2("N", "a3") == 3) + doAssert(foo2("z", "2") == 0) + +doAssert(foo("3") == 3) +doAssert(foo("a") == 0) +doAssert(foo2("Y", "a2") == 0) +doAssert(foo2("Y", "2a") == 2) +doAssert(foo2("N", "a3") == 3) +doAssert(foo2("z", "2") == 0) + + +# bug #20031 +proc main(a: uint64) = + case a + else: + discard + +static: + main(10) +main(10) + +block: + # Just needs to compile + proc bar(): int {.discardable.} = discard + + proc foo() {.noreturn.} = discard + + case "*" + of "*": + bar() + else: + # Make sure this noreturn doesn't + # cause the discardable to not discard + foo() diff --git a/tests/casestmt/tcstring.nim b/tests/casestmt/tcstring.nim new file mode 100644 index 000000000..288373402 --- /dev/null +++ b/tests/casestmt/tcstring.nim @@ -0,0 +1,52 @@ +discard """ + targets: "c cpp js" +""" + +type Result = enum none, a, b, c, d, e, f + +proc foo1(x: cstring): Result = + const y = cstring"hash" + const arr = [cstring"it", cstring"finally"] + result = none + case x + of "Andreas", "Rumpf": result = a + of cstring"aa", "bb": result = b + of "cc", y, "when": result = c + of "will", arr, "be", "generated": result = d + of nil: result = f + +var results = [ + foo1("Rumpf"), foo1("Andreas"), + foo1("aa"), foo1(cstring"bb"), + foo1("cc"), foo1("hash"), + foo1("finally"), foo1("generated"), + foo1("no"), foo1("another no"), + foo1(nil)] +doAssert results == [a, a, b, b, c, c, d, d, none, none, f], $results + +proc foo2(x: cstring): Result = + const y = cstring"hash" + const arr = [cstring"it", cstring"finally"] + doAssert not (compiles do: + result = case x + of "Andreas", "Rumpf": a + of cstring"aa", "bb": b + of "cc", y, "when": c + of "will", arr, "be", "generated": d) + case x + of "Andreas", "Rumpf": a + of cstring"aa", "bb": b + of "cc", y, "when": c + of "will", arr, "be", "generated": d + of nil: f + else: e + +results = [ + foo2("Rumpf"), foo2("Andreas"), + foo2("aa"), foo2(cstring"bb"), + foo2("cc"), foo2("hash"), + foo2("finally"), foo2("generated"), + foo2("no"), foo2("another no"), + foo2(nil)] + +doAssert results == [a, a, b, b, c, c, d, d, e, e, f], $results diff --git a/tests/casestmt/tincompletecaseobject.nim b/tests/casestmt/tincompletecaseobject.nim index 909ee4e1c..aa5deda7a 100644 --- a/tests/casestmt/tincompletecaseobject.nim +++ b/tests/casestmt/tincompletecaseobject.nim @@ -1,6 +1,6 @@ discard """ errormsg: ''' -not all cases are covered; missing: {nnkComesFrom, nnkDotCall, nnkHiddenCallConv, nnkVarTuple, nnkCurlyExpr, nnkRange, nnkCheckedFieldExpr, nnkDerefExpr, nnkElifExpr, nnkElseExpr, nnkLambda, nnkDo, nnkBind, nnkClosedSymChoice, nnkHiddenSubConv, nnkConv, nnkStaticExpr, nnkAddr, nnkHiddenAddr, nnkHiddenDeref, nnkObjDownConv, nnkObjUpConv, nnkChckRangeF, nnkChckRange64, nnkChckRange, nnkStringToCString, nnkCStringToString, nnkFastAsgn, nnkGenericParams, nnkFormalParams, nnkOfInherit, nnkImportAs, nnkConverterDef, nnkMacroDef, nnkTemplateDef, nnkIteratorDef, nnkOfBranch, nnkElifBranch, nnkExceptBranch, nnkElse, nnkAsmStmt, nnkTypeDef, nnkFinally, nnkContinueStmt, nnkImportStmt, nnkImportExceptStmt, nnkExportStmt, nnkExportExceptStmt, nnkFromStmt, nnkIncludeStmt, nnkUsingStmt, nnkBlockExpr, nnkStmtListType, nnkBlockType, nnkWith, nnkWithout, nnkTypeOfExpr, nnkObjectTy, nnkTupleTy, nnkTupleClassTy, nnkTypeClassTy, nnkStaticTy, nnkRecList, nnkRecCase, nnkRecWhen, nnkVarTy, nnkConstTy, nnkMutableTy, nnkDistinctTy, nnkProcTy, nnkIteratorTy, nnkSharedTy, nnkEnumTy, nnkEnumFieldDef, nnkArglist, nnkPattern, nnkReturnToken, nnkClosure, nnkGotoState, nnkState, nnkBreakState, nnkFuncDef, nnkTupleConstr} +not all cases are covered; missing: {nnkComesFrom, nnkDotCall, nnkHiddenCallConv, nnkVarTuple, nnkCurlyExpr, nnkRange, nnkCheckedFieldExpr, nnkDerefExpr, nnkElifExpr, nnkElseExpr, nnkLambda, nnkDo, nnkBind, nnkClosedSymChoice, nnkHiddenSubConv, nnkConv, nnkStaticExpr, nnkAddr, nnkHiddenAddr, nnkHiddenDeref, nnkObjDownConv, nnkObjUpConv, nnkChckRangeF, nnkChckRange64, nnkChckRange, nnkStringToCString, nnkCStringToString, nnkFastAsgn, nnkGenericParams, nnkFormalParams, nnkOfInherit, nnkImportAs, nnkConverterDef, nnkMacroDef, nnkTemplateDef, nnkIteratorDef, nnkOfBranch, nnkElifBranch, nnkExceptBranch, nnkElse, nnkAsmStmt, nnkTypeDef, nnkFinally, nnkContinueStmt, nnkImportStmt, nnkImportExceptStmt, nnkExportStmt, nnkExportExceptStmt, nnkFromStmt, nnkIncludeStmt, nnkUsingStmt, nnkBlockExpr, nnkStmtListType, nnkBlockType, nnkWith, nnkWithout, nnkTypeOfExpr, nnkObjectTy, nnkTupleTy, nnkTupleClassTy, nnkTypeClassTy, nnkStaticTy, nnkRecList, nnkRecCase, nnkRecWhen, nnkVarTy, nnkConstTy, nnkMutableTy, nnkDistinctTy, nnkProcTy, nnkIteratorTy, nnkSharedTy, nnkEnumTy, nnkEnumFieldDef, nnkArgList, nnkPattern, nnkReturnToken, nnkClosure, nnkGotoState, nnkState, nnkBreakState, nnkFuncDef, nnkTupleConstr} ''' """ @@ -62,7 +62,7 @@ type nnkSharedTy, # 'shared T' nnkEnumTy, nnkEnumFieldDef, - nnkArglist, nnkPattern + nnkArgList, nnkPattern nnkReturnToken, nnkClosure, nnkGotoState, diff --git a/tests/casestmt/tincompletecaseobject2.nim b/tests/casestmt/tincompletecaseobject2.nim index c080cfeb1..bbeae1909 100644 --- a/tests/casestmt/tincompletecaseobject2.nim +++ b/tests/casestmt/tincompletecaseobject2.nim @@ -1,12 +1,5 @@ discard """ cmd: "nim check $file" -errormsg: "not all cases are covered; missing: {A, B}" -nimout: ''' -tincompletecaseobject2.nim(18, 1) Error: not all cases are covered; missing: {' ', '!', '\"', '#', '$', '%', '&', '\'', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'} -tincompletecaseobject2.nim(22, 1) Error: not all cases are covered; missing: {B, C, D} -tincompletecaseobject2.nim(25, 1) Error: not all cases are covered; missing: {A, C} -tincompletecaseobject2.nim(28, 1) Error: not all cases are covered; missing: {A, B} -''' """ type ABCD = enum A, B, C, D @@ -15,15 +8,19 @@ type AliasRangeABC = RangeABC PrintableChars = range[' ' .. '~'] -case PrintableChars 'x': +case PrintableChars 'x': #[tt.Error +^ not all cases are covered; missing: {' ', '!', '\"', '#', '$$', '%', '&', '\'', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'}]# of '0'..'9', 'A'..'Z', 'a'..'z': discard of '(', ')': discard -case AliasABCD A: +case AliasABCD A: #[tt.Error +^ not all cases are covered; missing: {B, C, D}]# of A: discard -case RangeABC A: +case RangeABC A: #[tt.Error +^ not all cases are covered; missing: {A, C}]# of B: discard -case AliasRangeABC A: +case AliasRangeABC A: #[tt.Error +^ not all cases are covered; missing: {A, B}]# of C: discard diff --git a/tests/casestmt/trangeexhaustiveness.nim b/tests/casestmt/trangeexhaustiveness.nim new file mode 100644 index 000000000..2b7f3558e --- /dev/null +++ b/tests/casestmt/trangeexhaustiveness.nim @@ -0,0 +1,7 @@ +block: # issue #22661 + template foo(a: typed) = + a + + foo: + case false + of false..true: discard |