Skip to content

Commit

Permalink
Add test for createRpcSigsFromNim and createSingleRpcSig
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jan 4, 2024
1 parent 2f6a605 commit 31b7417
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
4 changes: 2 additions & 2 deletions json_rpc/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ macro createRpcSigsFromString*(clientType: untyped, sigString: static[string]):
## Nim type.
cresteSignaturesFromString(clientType, sigString)

macro createSingleRpcSig*(clientType: untyped, alias: static[string], procDecl: typed): untyped =
macro createSingleRpcSig*(clientType: untyped, alias: static[string], procDecl: untyped): untyped =
## Takes a single forward declarations in Nim and builds them into RPC
## calls, based on their parameters.
## Inputs are marshalled to json, and results are put into the signature's
Expand All @@ -125,7 +125,7 @@ macro createSingleRpcSig*(clientType: untyped, alias: static[string], procDecl:
procDecl.expectKind nnkProcDef
result = createRpcFromSig(clientType, procDecl, ident(alias))

macro createRpcSigsFromNim*(clientType: untyped, procList: typed): untyped =
macro createRpcSigsFromNim*(clientType: untyped, procList: untyped): untyped =
## Takes a list of forward declarations in Nim and builds them into RPC
## calls, based on their parameters.
## Inputs are marshalled to json, and results are put into the signature's
Expand Down
1 change: 1 addition & 0 deletions tests/private/file_callsigs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
# those terms.

proc shh_uninstallFilter(id: int): bool
proc getFilter(id: Variant): string
110 changes: 109 additions & 1 deletion tests/test_callsigs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,127 @@
# those terms.

import
../json_rpc/client
unittest2,
../json_rpc/rpcclient,
../json_rpc/rpcserver

from os import getCurrentDir, DirSep
from strutils import rsplit
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]

type
Variant = int | bool | string

createRpcSigs(RpcClient, sourceDir & "/private/file_callsigs.nim")

createSingleRpcSig(RpcClient, "bottle"):
proc get_Bottle(id: int): bool

createSingleRpcSig(RpcClient, "mouse"):
proc getVariant(id: Variant): bool

createRpcSigsFromNim(RpcClient):
proc get_Banana(id: int): bool
proc get_Combo(id, index: int, name: string): bool
proc get_Name(id: int): string
proc getJsonString(name: string): JsonString
proc getVariant(id: Variant): bool

proc installHandlers(s: RpcServer) =
s.rpc("shh_uninstallFilter") do(id: int) -> bool:
if id == 123:
return true
else:
return false

s.rpc("get_Bottle") do(id: int) -> bool:
if id == 456:
return true
else:
return false

s.rpc("get_Banana") do(id: int) -> bool:
if id == 789:
return true
else:
return false

s.rpc("get_Combo") do(id, index: int, name: string) -> bool:
if index == 77 and name == "banana":
return true
return false

s.rpc("get_Name") do(id: int) -> string:
if id == 99:
return "king kong"
return "godzilla"

s.rpc("getJsonString") do(name: string) -> JsonString:
if name == "me":
return "true".JsonString
return "123".JsonString

s.rpc("getVariant") do(id: string) -> bool:
if id == "33":
return true
return false

s.rpc("getFilter") do(id: string) -> string:
if id == "cow":
return "moo"
return "meow"

suite "test callsigs":
var server = newRpcSocketServer(["127.0.0.1:8545"])
server.installHandlers()
var client = newRpcSocketClient()

server.start()
waitFor client.connect("127.0.0.1", Port(8545))

test "callsigs from file":
let res = waitFor client.shh_uninstallFilter(123)
check res == true

let res2 = waitFor client.getFilter("cow")
check res2 == "moo"

test "callsigs alias":
let res = waitFor client.bottle(456)
check res == true

let res2 = waitFor client.mouse("33")
check res2 == true

let res3 = waitFor client.mouse("55")
check res3 == false

expect JsonRpcError:
let res4 = waitFor client.mouse(33)
check res4 == true

test "callsigs from nim":
let res = waitFor client.get_Banana(789)
check res == true

let res2 = waitFor client.get_Name(99)
check res2 == "king kong"

let res3 = waitFor client.get_Combo(0, 77, "banana")
check res3 == true

let res4 = waitFor client.getJsonString("me")
check res4 == "true".JsonString

let res5 = waitFor client.getVariant("33")
check res5 == true

let res6 = waitFor client.getVariant("55")
check res6 == false

expect JsonRpcError:
let res4 = waitFor client.getVariant(33)
check res4 == true

server.stop()
waitFor server.closeWait()

0 comments on commit 31b7417

Please sign in to comment.