Skip to content

Commit

Permalink
Add unit test for pythongen code
Browse files Browse the repository at this point in the history
This tests whether it can handle the case where a varaiant consist only
of zero-arg constructors.

Signed-off-by: Vincent Liu <[email protected]>
  • Loading branch information
Vincent-lau committed Feb 29, 2024
1 parent 6cacde3 commit 21ccda9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/lib/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(executable
(name suite)
(preprocess
(pps ppx_deriving_rpc))
(libraries rpclib.core rpclib.json rpclib.xml alcotest test_common))

(rule
Expand Down
49 changes: 49 additions & 0 deletions tests/lib/pythongen_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
type unit_variant =
| Empty
| Hollow
| Vacant
| Void
[@@deriving rpcty]

module UnitVInterface (R : Idl.RPC) = struct
open R

let unit_variant_p = Idl.Param.mk ~name:"unit_variant" unit_variant
let int_p = Idl.Param.mk Rpc.Types.int

let discard_v =
R.declare
"discard_v"
[ "constant function taking a unit variant and discards it by returning an integer"
]
(unit_variant_p @-> returning int_p Idl.DefaultError.err)


let implementation =
implement
{ Idl.Interface.name = "UnitVInterface"
; namespace = Some "UnitVInterface"
; description =
[ "Unit variant interface which does absolutely nothing. Only used to test \
whether the pythongen code can handle variants with zero argument \
constructors."
]
; version = 1, 0, 0
}
end

module UnitVCode : sig
val implementation : unit -> Codegen.Interface.t
end =
UnitVInterface (Codegen.Gen ())

let interfaces =
Codegen.Interfaces.create
~name:"unitv"
~title:"Unit Variant"
~description:[ "Interface for Unit variant" ]
~interfaces:[ UnitVCode.implementation () ]


let run () = Pythongen.of_interfaces interfaces |> Pythongen.string_of_ts |> ignore
let tests = [ "test gen python on variants with zero-arg constructors", `Quick, run ]
1 change: 1 addition & 0 deletions tests/lib/suite.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ let () =
; "Json", Json.tests
; "Xml_xapi", Xml_xapi.tests
; "Encoding", Encoding.tests
; "Pythongen_test", Pythongen_test.tests
]

0 comments on commit 21ccda9

Please sign in to comment.