Skip to content

Commit

Permalink
Merge pull request #178 from Vincent-lau/check-variants
Browse files Browse the repository at this point in the history
Check for list emptiness before checking variants
  • Loading branch information
mseri authored Feb 29, 2024
2 parents cbcdec7 + 4f9b5d2 commit bdd3ab5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/lib/pythongen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,12 @@ let rec typecheck : type a. a typ -> string -> t list =
variants
in
let check_contents =
List.fold_left
(fun acc x -> List.concat [ acc; check false x ])
(check true (List.hd variants_to_check))
(List.tl variants_to_check)
match variants_to_check with
| [] -> []
| v :: vs ->
List.fold_left
(fun acc x -> List.concat [ acc; check false x ])
(check true v) vs
in
let all_tags = List.map (fun (BoxedTag t) -> t.tname) variants in
let pylist =
Expand Down
49 changes: 48 additions & 1 deletion tests/rpc/test_pythongen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,50 @@ module Interface (R : Idl.RPC) = struct
end

module IfCode = Interface (Codegen.Gen ())
module UnitVInterface (R : Idl.RPC) = struct
open R

type unit_variant =
| Empty
| Hollow
| Vacant
| Void
[@@deriving rpcty]

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 unitv_interface =
Codegen.Interfaces.create
~name:"unitv"
~title:"Unit Variant"
~description:[ "Interface for Unit variant" ]
~interfaces:[ UnitVCode.implementation () ]

let interfaces =
Codegen.Interfaces.create
Expand Down Expand Up @@ -146,12 +190,15 @@ let check_exceptions () =
gen_python_bindings "python/bindings.py";
run_cmd "Exceptions should be correctly generated" "python python/exn_test.py"

let check_unit_variants () =
Pythongen.of_interfaces interfaces |> Pythongen.string_of_ts |> ignore

let tests =
[ ( "Check generated test interface bindings with pylint & pycodestyle"
, `Slow
, lint_bindings )
; "Check generated commandline bindings", `Slow, test_commandline
; "Check generated test class with commandline bindings", `Slow, check_test_class
; "Cehck generated exceptions", `Slow, check_exceptions
; "Check generated exceptions", `Slow, check_exceptions
; "Check python generation on variants with zero-arg constructors", `Quick, check_unit_variants
]

0 comments on commit bdd3ab5

Please sign in to comment.