Skip to content

Commit

Permalink
CP-47354: add unit tests for func_name_suffix and `string_of_ty_wit…
Browse files Browse the repository at this point in the history
…h_enums`

Signed-off-by: Luca Zhang <feiya.zhang@cloud.com>
  • Loading branch information
duobei committed Apr 30, 2024
1 parent aac6abc commit 7470964
Showing 1 changed file with 140 additions and 19 deletions.
159 changes: 140 additions & 19 deletions ocaml/sdk-gen/go/test_gen_go.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ let verify_field = function
| _ ->
false

let result_keys = ["type"; "func_partial_type"]
let result_keys = ["type"; "func_name_suffix"]

let verify_result_member = function
| "type", `String _ | "func_partial_type", `String _ ->
| "type", `String _ | "func_name_suffix", `String _ ->
true
| _ ->
false
Expand All @@ -94,7 +94,7 @@ let param_keys =
; "name"
; "name_internal"
; "doc"
; "func_partial_type"
; "func_name_suffix"
; "first"
]

Expand All @@ -105,7 +105,7 @@ let verify_param_member = function
| "name", `String _
| "name_internal", `String _
| "doc", `String _
| "func_partial_type", `String _ ->
| "func_name_suffix", `String _ ->
true
| _ ->
false
Expand Down Expand Up @@ -151,7 +151,7 @@ let verify_sesseion_message_member = function
schema_check result_keys verify_result_member result
| "params", `A params ->
List.for_all verify_param params
| "header_params", `A params ->
| "func_params", `A params ->
List.for_all verify_param params
| "errors", `A errors ->
List.for_all verify_error errors
Expand Down Expand Up @@ -179,7 +179,7 @@ let message_keys =
]

let session_message_keys =
["session_login"; "session_logout"; "header_params"] @ message_keys
["session_login"; "session_logout"; "func_params"] @ message_keys

let verify_message = function
| `O members ->
Expand Down Expand Up @@ -459,15 +459,15 @@ let session_messages : Mustache.Json.t =
reference if successful"
)
; ("async", `Bool false)
; ( "header_params"
; ( "func_params"
, `A
[
`O
[
("type", `String "string")
; ("name", `String "uname")
; ("name_internal", `String "uname")
; ("func_partial_type", `String "String")
; ("func_name_suffix", `String "String")
; ("first", `Bool true)
; ("session_class", `Bool true)
; ("session", `Bool false)
Expand All @@ -477,7 +477,7 @@ let session_messages : Mustache.Json.t =
("type", `String "string")
; ("name", `String "pwd")
; ("name_internal", `String "pwd")
; ("func_partial_type", `String "String")
; ("func_name_suffix", `String "String")
; ("session_class", `Bool true)
; ("session", `Bool false)
]
Expand All @@ -491,7 +491,7 @@ let session_messages : Mustache.Json.t =
("type", `String "string")
; ("name", `String "uname")
; ("name_internal", `String "uname")
; ("func_partial_type", `String "String")
; ("func_name_suffix", `String "String")
; ("first", `Bool true)
; ("session_class", `Bool true)
; ("session", `Bool true)
Expand All @@ -501,7 +501,7 @@ let session_messages : Mustache.Json.t =
("type", `String "string")
; ("name", `String "pwd")
; ("name_internal", `String "pwd")
; ("func_partial_type", `String "String")
; ("func_name_suffix", `String "String")
; ("session_class", `Bool true)
; ("session", `Bool true)
]
Expand All @@ -511,7 +511,7 @@ let session_messages : Mustache.Json.t =
, `O
[
("type", `String "SessionRef")
; ("func_partial_type", `String "SessionRef")
; ("func_name_suffix", `String "SessionRef")
]
)
; ("has_error", `Bool true)
Expand Down Expand Up @@ -539,15 +539,15 @@ let session_messages : Mustache.Json.t =
; ("method_name_exported", `String "Logout")
; ("description", `String "Logout Log out of a session")
; ("async", `Bool false)
; ( "header_params"
; ( "func_params"
, `A
[
`O
[
("type", `String "string")
; ("name", `String "test_param")
; ("name_internal", `String "testParam")
; ("func_partial_type", `String "String")
; ("func_name_suffix", `String "String")
; ("first", `Bool true)
; ("session_class", `Bool true)
; ("session", `Bool true)
Expand All @@ -562,7 +562,7 @@ let session_messages : Mustache.Json.t =
("type", `String "SessionRef")
; ("name", `String "session_id")
; ("name_internal", `String "sessionID")
; ("func_partial_type", `String "SessionRef")
; ("func_name_suffix", `String "SessionRef")
; ("param_ignore", `Bool true)
; ("session_class", `Bool true)
; ("session", `Bool true)
Expand All @@ -572,7 +572,7 @@ let session_messages : Mustache.Json.t =
("type", `String "string")
; ("name", `String "test_param")
; ("name_internal", `String "testParam")
; ("func_partial_type", `String "String")
; ("func_name_suffix", `String "String")
; ("first", `Bool true)
; ("session_class", `Bool true)
; ("session", `Bool true)
Expand Down Expand Up @@ -612,7 +612,7 @@ let messages : Mustache.Json.t =
("type", `String "SessionRef")
; ("name", `String "session_id")
; ("name_internal", `String "sessionID")
; ("func_partial_type", `String "SessionRef")
; ("func_name_suffix", `String "SessionRef")
; ("session", `Bool true)
; ("session_class", `Bool false)
; ("first", `Bool true)
Expand All @@ -622,7 +622,7 @@ let messages : Mustache.Json.t =
("type", `String "HostRef")
; ("name", `String "host")
; ("name_internal", `String "host")
; ("func_partial_type", `String "HostRef")
; ("func_name_suffix", `String "HostRef")
; ("session_class", `Bool false)
; ("session", `Bool false)
]
Expand All @@ -632,7 +632,7 @@ let messages : Mustache.Json.t =
, `O
[
("type", `String "string")
; ("func_partial_type", `String "String")
; ("func_name_suffix", `String "String")
]
)
; ("has_error", `Bool false)
Expand Down Expand Up @@ -708,10 +708,131 @@ module TestGeneratedJson = struct
]
end

module FuncNameSuffixTest = Generic.MakeStateless (struct
open Datamodel_types

module Io = struct
type input_t = ty

type output_t = string

let string_of_input_t = Json.func_name_suffix

let string_of_output_t = Test_printers.string
end

let transform = Json.func_name_suffix

let tests =
`QuickAndAutoDocumented
[
(SecretString, "String")
; (String, "String")
; (Int, "Int")
; (Float, "Float")
; (Bool, "Bool")
; (Enum ("update_sync", [("a", "b"); ("c", "d")]), "EnumUpdateSync")
; (Set String, "StringSet")
; (Map (Int, String), "IntToStringMap")
; (Ref "pool", "PoolRef")
; (Record "pool", "PoolRecord")
; (Option String, "String")
]
end)

module StringOfTyWithEnumsTest = struct
open Datamodel_types
module StringMap = Json.StringMap

let verify description verify_func actual =
Alcotest.(check bool) description true (verify_func actual)

let verify_string (ty, enums) = ty = "string" && enums = StringMap.empty

let test_string () =
let ty, enums = Json.string_of_ty_with_enums String in
verify "String" verify_string (ty, enums)

let test_secret_string () =
let ty, enums = Json.string_of_ty_with_enums SecretString in
verify "SecretString" verify_string (ty, enums)

let verify_float (ty, enums) = ty = "float64" && enums = StringMap.empty

let test_float () =
let ty, enums = Json.string_of_ty_with_enums Float in
verify "Float" verify_float (ty, enums)

let verify_bool (ty, enums) = ty = "bool" && enums = StringMap.empty

let test_bool () =
let ty, enums = Json.string_of_ty_with_enums Bool in
verify "bool" verify_bool (ty, enums)

let verify_datetime (ty, enums) = ty = "time.Time" && enums = StringMap.empty

let test_datetime () =
let ty, enums = Json.string_of_ty_with_enums DateTime in
verify "datetime" verify_datetime (ty, enums)

let enum_lst = [("a", "b"); ("c", "d")]

let verify_enum (ty, enums) =
ty = "UpdateSync" && enums = StringMap.singleton "UpdateSync" enum_lst

let test_enum () =
let ty, enums =
Json.string_of_ty_with_enums (Enum ("update_sync", enum_lst))
in
verify "enum" verify_enum (ty, enums)

let verify_ref (ty, enums) = ty = "PoolRef" && enums = StringMap.empty

let test_ref () =
let ty, enums = Json.string_of_ty_with_enums (Ref "pool") in
verify "ref" verify_ref (ty, enums)

let verify_record (ty, enums) = ty = "PoolRecord" && enums = StringMap.empty

let test_record () =
let ty, enums = Json.string_of_ty_with_enums (Record "pool") in
verify "datetime" verify_record (ty, enums)

let test_option () =
let ty, enums = Json.string_of_ty_with_enums (Option String) in
verify "datetime" verify_string (ty, enums)

let verify_map (ty, enums) =
ty = "map[int]UpdateSync"
&& enums = StringMap.singleton "UpdateSync" enum_lst

let test_map () =
let ty, enums =
Json.string_of_ty_with_enums (Map (Int, Enum ("update_sync", enum_lst)))
in
verify "map" verify_map (ty, enums)

let tests =
[
("String", `Quick, test_string)
; ("SecretString", `Quick, test_secret_string)
; ("Float", `Quick, test_float)
; ("Bool", `Quick, test_bool)
; ("DateTime", `Quick, test_datetime)
; ("Enum", `Quick, test_enum)
; ("Ref", `Quick, test_ref)
; ("Record", `Quick, test_record)
; ("Option", `Quick, test_option)
; ("Map", `Quick, test_map)
]
end

let tests =
make_suite "gen_go_binding_"
[
("snake_to_camel", SnakeToCamelTest.tests)
; ("get_fp_type", FuncNameSuffixTest.tests)
; ("string_of_ty_with_enums", StringOfTyWithEnumsTest.tests)
; ("templates", TemplatesTest.tests)
; ("generated_mustache_jsons", TestGeneratedJson.tests)
]
Expand Down

0 comments on commit 7470964

Please sign in to comment.