Skip to content

Commit

Permalink
refactor: create an Alcotest.testable to check structure of generat…
Browse files Browse the repository at this point in the history
…ed JSON is wanted

Signed-off-by: Luca Zhang <[email protected]>
  • Loading branch information
duobei committed Apr 9, 2024
1 parent f511cb1 commit 5f004f2
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions ocaml/sdk-gen/go/test_gen_go.ml
Original file line number Diff line number Diff line change
Expand Up @@ -267,37 +267,47 @@ module TemplatesTest = Generic.MakeStateless (struct
]
end)

let generated_json_tests =
module TestGeneratedJson = struct
let merge (obj1 : Mustache.Json.t) (obj2 : Mustache.Json.t) =
match (obj1, obj2) with
| `O list1, `O list2 ->
`O (list1 @ list2)
| _ ->
`O []
in
let jsons () =
let json = enums |> merge record |> merge header in
let objects = Json.xenapi objects in
check_true "Mustache.Json of records has right structure"
@@ List.for_all (fun (_, obj) -> is_same_struct obj json) objects
in
let errors_and_messages () =

let pp_json = Fmt.of_to_string string_of_json

let generated_json = Alcotest.testable pp_json is_same_struct

let verify description expected actual =
Alcotest.(check @@ generated_json) description expected actual

let json = enums |> merge record |> merge header

let testing (name, obj) json () = verify name obj json

let test_case ((name, _) as test_case) json =
(name, `Quick, testing test_case json)

let errors_and_messages_tests =
let errors = `O [("api_errors", `A Json.api_errors)] in
let messages = `O [("api_messages", `A Json.api_messages)] in
check_true "Mustache.Json of errors and messages has right structure"
@@ (is_same_struct errors api_errors && is_same_struct messages api_messages)
in
[
("jsons", `Quick, jsons)
; ("errors_and_messages", `Quick, errors_and_messages)
]
[
test_case ("api_errors", errors) api_errors
; test_case ("api_messages", messages) api_messages
]

let tests =
(objects |> Json.xenapi |> List.map (fun obj -> test_case obj json))
@ errors_and_messages_tests
end

let tests =
make_suite "gen_go_binding_"
[
("snake_to_camel", SnakeToCamelTest.tests)
; ("templates", TemplatesTest.tests)
; ("generated_mustache_jsons", generated_json_tests)
; ("generated_mustache_jsons", TestGeneratedJson.tests)
]

let () = Alcotest.run "Gen go binding" tests

0 comments on commit 5f004f2

Please sign in to comment.