diff options
Diffstat (limited to 'tests/proc')
-rw-r--r-- | tests/proc/mdefaultprocparam.nim | 5 | ||||
-rw-r--r-- | tests/proc/tcolonisproc.nim | 19 | ||||
-rw-r--r-- | tests/proc/tdefaultprocparam.nim | 90 | ||||
-rw-r--r-- | tests/proc/tlambdadonotation.nim | 78 | ||||
-rw-r--r-- | tests/proc/tnamedparams.nim | 12 | ||||
-rw-r--r-- | tests/proc/tnamedparams2.nim | 15 | ||||
-rw-r--r-- | tests/proc/tnamedparams3.nim | 10 | ||||
-rw-r--r-- | tests/proc/tparamsindefault.nim | 120 | ||||
-rw-r--r-- | tests/proc/tprocvar.nim | 39 | ||||
-rw-r--r-- | tests/proc/tprocvarmismatch.nim | 18 |
10 files changed, 406 insertions, 0 deletions
diff --git a/tests/proc/mdefaultprocparam.nim b/tests/proc/mdefaultprocparam.nim new file mode 100644 index 000000000..4a17277c0 --- /dev/null +++ b/tests/proc/mdefaultprocparam.nim @@ -0,0 +1,5 @@ + + +proc p*(f = (proc(): string = "hi")) = + echo f() + diff --git a/tests/proc/tcolonisproc.nim b/tests/proc/tcolonisproc.nim new file mode 100644 index 000000000..c10dabcf1 --- /dev/null +++ b/tests/proc/tcolonisproc.nim @@ -0,0 +1,19 @@ +discard """ +output: ''' +1 +2 +''' +""" + +proc p(a, b: int, c: proc ()) = + c() + +when false: + # language spec changed: + p(1, 3): + echo 1 + echo 3 + +p(1, 1, proc() = + echo 1 + echo 2) diff --git a/tests/proc/tdefaultprocparam.nim b/tests/proc/tdefaultprocparam.nim new file mode 100644 index 000000000..90edfa8c9 --- /dev/null +++ b/tests/proc/tdefaultprocparam.nim @@ -0,0 +1,90 @@ +discard """ +output: ''' +hi +hi +topLevel|topLevel| +topLevel2|topLevel2| +inProc|inProc| +inProc2|inProc2| +topLevel|9 +topLevel2|10 +inProc|7 +inProc2|8 +must have been the wind.. +I'm there +must have been the wind.. +I'm there +symbol'a'symbol'a' +symbol'b'symbol'b' +symbol'a'symbol'b' +symbol'a'9 +symbol'b'9 +symbol'a'0 +''' +""" +import mdefaultprocparam + +p() + +proc testP = + p() + +testP() + +proc p2(s: string, count = s): string = s & count + +proc testP2 = + echo p2 """inProc|""" + echo p2 """inProc2|""" + +echo p2 """topLevel|""" +echo p2 """topLevel2|""" + +testP2() + +import macros +macro dTT(a: typed) = echo a.treeRepr + +proc p3(s: string, count = len(s)): string = s & $count + +proc testP3 = + echo p3 """inProc|""" + echo p3 """inProc2|""" + +echo p3 """topLevel|""" +echo p3 """topLevel2|""" + +testP3() + +proc cut(s: string, c = len(s)): string = + s[0..<s.len-c] + +echo "must have been the wind.." & cut "I'm gone" +echo cut("I'm gone", 4) & "there" + +proc testCut = + echo "must have been the wind.." & cut "I'm gone" + echo cut("I'm gone", 4) & "there" + +testCut() + +var a = "symbol'a'" +var b = "symbol'b'" + +block: + echo p2(a) +block: + echo p2(b) +block: + echo p2(a, b) +block: + echo p3(a) + echo p3(b) + echo p3(a, 0) + +# bug #12252 +proc foo(a = 0, b = a.high, c = high(typeof(a))) = + discard + +foo() + diff --git a/tests/proc/tlambdadonotation.nim b/tests/proc/tlambdadonotation.nim new file mode 100644 index 000000000..3160c0972 --- /dev/null +++ b/tests/proc/tlambdadonotation.nim @@ -0,0 +1,78 @@ +discard """ +output: ''' +issue #11812 +issue #10899 +123 +issue #11367 +event consumed! +''' +""" + +echo "issue #11812" + +proc run(a: proc()) = a() + +proc main() = + var test: int + run(proc() = test = 0) + run do: + test = 0 + +main() + + +echo "issue #10899" + +proc foo(x: proc {.closure.}) = + x() + +proc bar = + var x = 123 + # foo proc = echo x #[ ok ]# + foo: echo x #[ SIGSEGV: Illegal storage access. (Attempt to read from nil?) ]# + +bar() + +echo "issue #11367" + +type + + EventCB = proc() + + Emitter = object + cb: EventCB + + Subscriber = object + discard + +proc newEmitter(): Emitter = + result + +proc on_event(self: var Emitter, cb: EventCB) = + self.cb = cb + +proc emit(self: Emitter) = + self.cb() + +proc newSubscriber(): Subscriber = + result + +proc consume(self: Subscriber) = + echo "event consumed!" + +proc main2() = + var emitter = newEmitter() + var subscriber = newSubscriber() + + proc foo() = + subscriber.consume() + + emitter.on_event() do (): + subscriber.consume() + + # this works + # emitter.on_event(foo) + + emitter.emit() + +main2() diff --git a/tests/proc/tnamedparams.nim b/tests/proc/tnamedparams.nim new file mode 100644 index 000000000..d0774f0d8 --- /dev/null +++ b/tests/proc/tnamedparams.nim @@ -0,0 +1,12 @@ +discard """ + errormsg: "type mismatch: got <input: string, filename: string, line: int literal(1), col: int literal(23)>" + file: "tnamedparams.nim" + line: 8 +""" +import pegs + +discard parsePeg( + input = "input", + filename = "filename", + line = 1, + col = 23) diff --git a/tests/proc/tnamedparams2.nim b/tests/proc/tnamedparams2.nim new file mode 100644 index 000000000..9acdeed87 --- /dev/null +++ b/tests/proc/tnamedparams2.nim @@ -0,0 +1,15 @@ +import pegs + +discard parsePeg( + pattern = "input", + filename = "filename", + line = 1, + col = 23) + +# bug #12196 +type + Renderer = object + +var xs0, x0, xs1, x1: int +proc init(xs=xs0; x=x0; renderer: Renderer; r: byte) = discard +init(xs=xs1, x=x1, r=3, renderer=Renderer()) diff --git a/tests/proc/tnamedparams3.nim b/tests/proc/tnamedparams3.nim new file mode 100644 index 000000000..e736c338c --- /dev/null +++ b/tests/proc/tnamedparams3.nim @@ -0,0 +1,10 @@ +discard """ + errormsg: "type mismatch: got <int literal(5), b: bool>" + line: 10 +""" + +# bug #2993 +proc test(i: int, a, b: bool) = discard +#test(5, b = false) #Missing param a + +5.test(b = false) #Missing param a diff --git a/tests/proc/tparamsindefault.nim b/tests/proc/tparamsindefault.nim new file mode 100644 index 000000000..3fe917f2b --- /dev/null +++ b/tests/proc/tparamsindefault.nim @@ -0,0 +1,120 @@ +discard """ +output: ''' +@[1, 2, 3]@[1, 2, 3] +a +a +1 +3 is an int +2 is an int +miau is a string +f1 1 1 1 +f1 2 3 3 +f1 10 20 30 +f2 100 100 100 +f2 200 300 300 +f2 300 400 400 +f3 10 10 20 +f3 10 15 25 +true true +false true +world +typedescDefault +''' +""" + +template reject(x) = + assert(not compiles(x)) + +block: + # https://github.com/nim-lang/Nim/issues/7756 + proc foo[T](x: seq[T], y: seq[T] = x) = + echo x, y + + let a = @[1, 2, 3] + foo(a) + +block: + # https://github.com/nim-lang/Nim/issues/1201 + proc issue1201(x: char|int = 'a') = echo x + + issue1201() + issue1201('a') + issue1201(1) + + # https://github.com/nim-lang/Nim/issues/7000 + proc test(a: int|string = 2) = + when a is int: + echo a, " is an int" + elif a is string: + echo a, " is a string" + + test(3) # works + test() # works + test("miau") + +block: + # https://github.com/nim-lang/Nim/issues/3002 and similar + proc f1(a: int, b = a, c = b) = + echo "f1 ", a, " ", b, " ", c + + proc f2(a: int, b = a, c: int = b) = + echo "f2 ", a, " ", b, " ", c + + proc f3(a: int, b = a, c = a + b) = + echo "f3 ", a, " ", b, " ", c + + f1 1 + f1(2, 3) + f1 10, 20, 30 + 100.f2 + 200.f2 300 + 300.f2(400) + + 10.f3() + 10.f3(15) + + reject: + # This is a type mismatch error: + proc f4(a: int, b = a, c: float = b) = discard + + reject: + # undeclared identifier + proc f5(a: int, b = c, c = 10) = discard + + reject: + # undeclared identifier + proc f6(a: int, b = b) = discard + + reject: + # undeclared identifier + proc f7(a = a) = discard + +block: + proc f(a: var int, b: ptr int, c = addr(a)) = + echo addr(a) == b, " ", b == c + + var x = 10 + f(x, addr(x)) + f(x, nil, nil) + +block: + # https://github.com/nim-lang/Nim/issues/1046 + proc pySubstr(s: string, start: int, endd = s.len()): string = + var + revStart = start + revEnd = endd + + if start < 0: + revStart = s.len() + start + if endd < 0: + revEnd = s.len() + endd + + return s[revStart .. revEnd-1] + + echo pySubstr("Hello world", -5) + + +# bug #11660 + +func typedescDefault(T: typedesc; arg: T = 0) = debugEcho "typedescDefault" +typedescDefault(int) diff --git a/tests/proc/tprocvar.nim b/tests/proc/tprocvar.nim new file mode 100644 index 000000000..14f24efdc --- /dev/null +++ b/tests/proc/tprocvar.nim @@ -0,0 +1,39 @@ +discard """ + output: ''' +papbpcpdpe7 +''' +""" + +block genericprocvar: + proc foo[T](thing: T) = + discard thing + var a: proc (thing: int) {.nimcall.} = foo[int] + + +block tprocvar2: + proc pa() {.cdecl.} = write(stdout, "pa") + proc pb() {.cdecl.} = write(stdout, "pb") + proc pc() {.cdecl.} = write(stdout, "pc") + proc pd() {.cdecl.} = write(stdout, "pd") + proc pe() {.cdecl.} = write(stdout, "pe") + + const algos = [pa, pb, pc, pd, pe] + var x: proc (a, b: int): int {.cdecl.} + + proc ha(c, d: int): int {.cdecl.} = + echo(c + d) + result = c + d + + for a in items(algos): + a() + + x = ha + discard x(3, 4) + + +block tprocvars: + proc doSomething(v: int, x: proc(v:int):int): int = return x(v) + proc doSomething(v: int, x: proc(v:int)) = x(v) + + doAssert doSomething(10, proc(v: int): int = return v div 2) == 5 + diff --git a/tests/proc/tprocvarmismatch.nim b/tests/proc/tprocvarmismatch.nim new file mode 100644 index 000000000..4d6be9be6 --- /dev/null +++ b/tests/proc/tprocvarmismatch.nim @@ -0,0 +1,18 @@ +discard """ + errormsg: "type mismatch" + line: 17 + file: "tprocvarmismatch.nim" +""" + +type + TCallback = proc (a, b: int) + +proc huh(x, y: var int) = + x = 0 + y = x+1 + +proc so(c: TCallback) = + c(2, 4) + +so(huh) + |