Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for createRpcSigsFromNim and createSingleRpcSig #184

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use port 0 in tests to let the OS choose

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()