diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/async/tasyncdial.nim | 53 | ||||
-rw-r--r-- | tests/osproc/texitsignal.nim | 36 | ||||
-rw-r--r-- | tests/parser/twrongcmdsyntax.nim | 6 | ||||
-rw-r--r-- | tests/stdlib/thttpclient.nim | 40 | ||||
-rw-r--r-- | tests/stdlib/tjsonmacro.nim | 50 | ||||
-rw-r--r-- | tests/stdlib/tnetdial.nim | 60 | ||||
-rw-r--r-- | tests/tuples/tunpack_asgn.nim | 2 | ||||
-rw-r--r-- | tests/tuples/tuple_subscript.nim | 40 |
8 files changed, 282 insertions, 5 deletions
diff --git a/tests/async/tasyncdial.nim b/tests/async/tasyncdial.nim new file mode 100644 index 000000000..d70e14020 --- /dev/null +++ b/tests/async/tasyncdial.nim @@ -0,0 +1,53 @@ +discard """ + file: "tasyncdial.nim" + output: ''' +OK AF_INET +OK AF_INET6 +''' +""" + +import + nativesockets, os, asyncdispatch + +proc setupServerSocket(hostname: string, port: Port, domain: Domain): AsyncFD = + ## Creates a socket, binds it to the specified address, and starts listening for connecitons. + ## Registers the descriptor with the dispatcher of the current thread + ## Raises OSError in case of an error. + let fd = newNativeSocket(domain) + setSockOptInt(fd, SOL_SOCKET, SO_REUSEADDR, 1) + var aiList = getAddrInfo(hostname, port, domain) + if bindAddr(fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32: + freeAddrInfo(aiList) + raiseOSError(osLastError()) + freeAddrInfo(aiList) + if listen(fd) != 0: + raiseOSError(osLastError()) + setBlocking(fd, false) + result = fd.AsyncFD + register(result) + +proc doTest(domain: static[Domain]) {.async.} = + const + testHost = when domain == Domain.AF_INET6: "::1" else: "127.0.0.1" + testPort = Port(17384) + let serverFd = setupServerSocket(testHost, testPort, domain) + let acceptFut = serverFd.accept() + let clientFdFut = dial(testHost, testPort) + + let serverClientFd = await acceptFut + serverFd.closeSocket() + + let clientFd = await clientFdFut + + let recvFut = serverClientFd.recv(2) + await clientFd.send("Hi") + let msg = await recvFut + + serverClientFd.closeSocket() + clientFd.closeSocket() + + if msg == "Hi": + echo "OK ", domain + +waitFor(doTest(Domain.AF_INET)) +waitFor(doTest(Domain.AF_INET6)) diff --git a/tests/osproc/texitsignal.nim b/tests/osproc/texitsignal.nim new file mode 100644 index 000000000..c0bed70ee --- /dev/null +++ b/tests/osproc/texitsignal.nim @@ -0,0 +1,36 @@ +discard """ + output: '''true +true''' + targets: "c" +""" + +import os, osproc +when not defined(windows): + import posix + +# Checks that the environment is passed correctly in startProcess +# To do that launches a copy of itself with a new environment. + +if paramCount() == 0: + # Parent process + + let p = startProcess( + getAppFilename(), + args = @["child"], + options = {poStdErrToStdOut, poUsePath, poParentStreams} + ) + + echo p.running() + + p.kill() + + when defined(windows): + # windows kill happens using TerminateProcess(h, 0), so we should get a + # 0 here + echo p.waitForExit() == 0 + else: + # on posix (non-windows), kill sends SIGKILL + echo p.waitForExit() == 128 + SIGKILL + +else: + sleep(5000) # should get killed before this \ No newline at end of file diff --git a/tests/parser/twrongcmdsyntax.nim b/tests/parser/twrongcmdsyntax.nim new file mode 100644 index 000000000..affe72c34 --- /dev/null +++ b/tests/parser/twrongcmdsyntax.nim @@ -0,0 +1,6 @@ +discard """ + errormsg: '''identifier expected, but found 'echo 4''' + line: 6 +""" + +echo 4 +2 diff --git a/tests/stdlib/thttpclient.nim b/tests/stdlib/thttpclient.nim index 62c1ebee7..40324d92a 100644 --- a/tests/stdlib/thttpclient.nim +++ b/tests/stdlib/thttpclient.nim @@ -1,11 +1,13 @@ discard """ cmd: "nim c --threads:on -d:ssl $file" + exitcode: 0 + output: "OK" """ import strutils from net import TimeoutError -import httpclient, asyncdispatch +import nativesockets, os, httpclient, asyncdispatch const manualTests = false @@ -112,6 +114,40 @@ proc syncTest() = except: doAssert false, "TimeoutError should have been raised." -syncTest() +proc makeIPv6HttpServer(hostname: string, port: Port): AsyncFD = + let fd = newNativeSocket(AF_INET6) + setSockOptInt(fd, SOL_SOCKET, SO_REUSEADDR, 1) + var aiList = getAddrInfo(hostname, port, AF_INET6) + if bindAddr(fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32: + freeAddrInfo(aiList) + raiseOSError(osLastError()) + freeAddrInfo(aiList) + if listen(fd) != 0: + raiseOSError(osLastError()) + setBlocking(fd, false) + + var serverFd = fd.AsyncFD + register(serverFd) + result = serverFd + + proc onAccept(fut: Future[AsyncFD]) {.gcsafe.} = + if not fut.failed: + let clientFd = fut.read() + clientFd.send("HTTP/1.1 200 OK\r\LContent-Length: 0\r\LConnection: Closed\r\L\r\L").callback = proc() = + clientFd.closeSocket() + serverFd.accept().callback = onAccept + serverFd.accept().callback = onAccept + +proc ipv6Test() = + var client = newAsyncHttpClient() + let serverFd = makeIPv6HttpServer("::1", Port(18473)) + var resp = waitFor client.request("http://[::1]:18473/") + doAssert(resp.status == "200 OK") + serverFd.closeSocket() + client.close() +syncTest() waitFor(asyncTest()) +ipv6Test() + +echo "OK" diff --git a/tests/stdlib/tjsonmacro.nim b/tests/stdlib/tjsonmacro.nim index 74b0d4dc3..323b3e1ee 100644 --- a/tests/stdlib/tjsonmacro.nim +++ b/tests/stdlib/tjsonmacro.nim @@ -159,11 +159,11 @@ when isMainModule: name: string age: int - Data = object + Data1 = object # TODO: Codegen bug when changed to ``Data``. person: Person list: seq[int] - var data = to(jsonNode, Data) + var data = to(jsonNode, Data1) doAssert data.person.name == "Nimmer" doAssert data.person.age == 21 doAssert data.list == @[1, 2, 3, 4] @@ -182,4 +182,48 @@ when isMainModule: } var result = to(node, TestEnum) - doAssert result.field == Bar \ No newline at end of file + doAssert result.field == Bar + + # Test ref type in field. + block: + var jsonNode = parseJson(""" + { + "person": { + "name": "Nimmer", + "age": 21 + }, + "list": [1, 2, 3, 4] + } + """) + + type + Person = ref object + name: string + age: int + + Data = object + person: Person + list: seq[int] + + var data = to(jsonNode, Data) + doAssert data.person.name == "Nimmer" + doAssert data.person.age == 21 + doAssert data.list == @[1, 2, 3, 4] + + jsonNode = parseJson(""" + { + "person": null, + "list": [1, 2, 3, 4] + } + """) + data = to(jsonNode, Data) + doAssert data.person.isNil + + block: + type + FooBar = object + field: float + + let x = parseJson("""{ "field": 5}""") + let data = to(x, FooBar) + doAssert data.field == 5.0 \ No newline at end of file diff --git a/tests/stdlib/tnetdial.nim b/tests/stdlib/tnetdial.nim new file mode 100644 index 000000000..da6088d70 --- /dev/null +++ b/tests/stdlib/tnetdial.nim @@ -0,0 +1,60 @@ +discard """ + cmd: "nim c --threads:on $file" + exitcode: 0 + output: "OK" +""" + +import os, net, nativesockets, asyncdispatch + +## Test for net.dial + +const port = Port(28431) + +proc initIPv6Server(hostname: string, port: Port): AsyncFD = + let fd = newNativeSocket(AF_INET6) + setSockOptInt(fd, SOL_SOCKET, SO_REUSEADDR, 1) + var aiList = getAddrInfo(hostname, port, AF_INET6) + if bindAddr(fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32: + freeAddrInfo(aiList) + raiseOSError(osLastError()) + freeAddrInfo(aiList) + if listen(fd) != 0: + raiseOSError(osLastError()) + setBlocking(fd, false) + + var serverFd = fd.AsyncFD + register(serverFd) + result = serverFd + +# Since net.dial is synchronous, we use main thread to setup server, +# and dial to it from another thread. + +proc testThread() {.thread.} = + let fd = net.dial("::1", port) + var s = newString(5) + doAssert fd.recv(addr s[0], 5) == 5 + if s == "Hello": + echo "OK" + fd.close() + +proc test() = + var t: Thread[void] + createThread(t, testThread) + + let serverFd = initIPv6Server("::1", port) + var done = false + + serverFd.accept().callback = proc(fut: Future[AsyncFD]) = + serverFd.closeSocket() + if not fut.failed: + let fd = fut.read() + fd.send("Hello").callback = proc() = + fd.closeSocket() + done = true + + while not done: + poll() + + joinThread(t) + +test() diff --git a/tests/tuples/tunpack_asgn.nim b/tests/tuples/tunpack_asgn.nim index a48fcff5d..1dc7ff074 100644 --- a/tests/tuples/tunpack_asgn.nim +++ b/tests/tuples/tunpack_asgn.nim @@ -21,6 +21,8 @@ proc pg[T](x, y: var T) = # test as a top level statement: var x, y, a, b: int +# test for regression: +(x, y) = (1, 2) (x, y) = fooBar() echo x, " ", y diff --git a/tests/tuples/tuple_subscript.nim b/tests/tuples/tuple_subscript.nim new file mode 100644 index 000000000..021793dc3 --- /dev/null +++ b/tests/tuples/tuple_subscript.nim @@ -0,0 +1,40 @@ +discard """ + output: '''5 +5 +str2 +str2 +4''' +""" + +proc`[]` (t: tuple, key: string): string = + for name, field in fieldPairs(t): + if name == key: + return $field + return "" + + +proc`[]` [A,B](t: tuple, key: string, op: (proc(x: A): B)): B = + for name, field in fieldPairs(t): + when field is A: + if name == key: + return op(field) + +proc`[]=`[T](t: var tuple, key: string, val: T) = + for name, field in fieldPairs(t): + when field is T: + if name == key: + field = val + +var tt = (a: 1, b: "str1") + +# test built in operator +tt[0] = 5 +echo tt[0] +echo `[]`(tt, 0) + + +# test overloaded operator +tt["b"] = "str2" +echo tt["b"] +echo `[]`(tt, "b") +echo tt["b", proc(s: string) : int = s.len] \ No newline at end of file |