Skip to content

Commit

Permalink
add expect tests for generated code in examples/
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin authored and c-cube committed Jan 14, 2024
1 parent 078d929 commit 1aa73e8
Show file tree
Hide file tree
Showing 15 changed files with 2,127 additions and 0 deletions.
424 changes: 424 additions & 0 deletions src/examples/calculator.ml.expected

Large diffs are not rendered by default.

173 changes: 173 additions & 0 deletions src/examples/calculator.mli.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@

(** Code for calculator.proto *)

(* generated from "calculator.proto", do not edit *)



(** {2 Types} *)

type div_by_zero = unit

type i32 = {
value : int32;
}

type add_req = {
a : int32;
b : int32;
}

type add_all_req = {
ints : int32 list;
}

type empty = unit


(** {2 Basic values} *)

val default_div_by_zero : unit
(** [default_div_by_zero ()] is the default value for type [div_by_zero] *)

val default_i32 :
?value:int32 ->
unit ->
i32
(** [default_i32 ()] is the default value for type [i32] *)

val default_add_req :
?a:int32 ->
?b:int32 ->
unit ->
add_req
(** [default_add_req ()] is the default value for type [add_req] *)

val default_add_all_req :
?ints:int32 list ->
unit ->
add_all_req
(** [default_add_all_req ()] is the default value for type [add_all_req] *)

val default_empty : unit
(** [default_empty ()] is the default value for type [empty] *)


(** {2 Formatters} *)

val pp_div_by_zero : Format.formatter -> div_by_zero -> unit
(** [pp_div_by_zero v] formats v *)

val pp_i32 : Format.formatter -> i32 -> unit
(** [pp_i32 v] formats v *)

val pp_add_req : Format.formatter -> add_req -> unit
(** [pp_add_req v] formats v *)

val pp_add_all_req : Format.formatter -> add_all_req -> unit
(** [pp_add_all_req v] formats v *)

val pp_empty : Format.formatter -> empty -> unit
(** [pp_empty v] formats v *)


(** {2 Protobuf Encoding} *)

val encode_pb_div_by_zero : div_by_zero -> Pbrt.Encoder.t -> unit
(** [encode_pb_div_by_zero v encoder] encodes [v] with the given [encoder] *)

val encode_pb_i32 : i32 -> Pbrt.Encoder.t -> unit
(** [encode_pb_i32 v encoder] encodes [v] with the given [encoder] *)

val encode_pb_add_req : add_req -> Pbrt.Encoder.t -> unit
(** [encode_pb_add_req v encoder] encodes [v] with the given [encoder] *)

val encode_pb_add_all_req : add_all_req -> Pbrt.Encoder.t -> unit
(** [encode_pb_add_all_req v encoder] encodes [v] with the given [encoder] *)

val encode_pb_empty : empty -> Pbrt.Encoder.t -> unit
(** [encode_pb_empty v encoder] encodes [v] with the given [encoder] *)


(** {2 Protobuf Decoding} *)

val decode_pb_div_by_zero : Pbrt.Decoder.t -> div_by_zero
(** [decode_pb_div_by_zero decoder] decodes a [div_by_zero] binary value from [decoder] *)

val decode_pb_i32 : Pbrt.Decoder.t -> i32
(** [decode_pb_i32 decoder] decodes a [i32] binary value from [decoder] *)

val decode_pb_add_req : Pbrt.Decoder.t -> add_req
(** [decode_pb_add_req decoder] decodes a [add_req] binary value from [decoder] *)

val decode_pb_add_all_req : Pbrt.Decoder.t -> add_all_req
(** [decode_pb_add_all_req decoder] decodes a [add_all_req] binary value from [decoder] *)

val decode_pb_empty : Pbrt.Decoder.t -> empty
(** [decode_pb_empty decoder] decodes a [empty] binary value from [decoder] *)


(** {2 Protobuf YoJson Encoding} *)

val encode_json_div_by_zero : div_by_zero -> Yojson.Basic.t
(** [encode_json_div_by_zero v encoder] encodes [v] to to json *)

val encode_json_i32 : i32 -> Yojson.Basic.t
(** [encode_json_i32 v encoder] encodes [v] to to json *)

val encode_json_add_req : add_req -> Yojson.Basic.t
(** [encode_json_add_req v encoder] encodes [v] to to json *)

val encode_json_add_all_req : add_all_req -> Yojson.Basic.t
(** [encode_json_add_all_req v encoder] encodes [v] to to json *)

val encode_json_empty : empty -> Yojson.Basic.t
(** [encode_json_empty v encoder] encodes [v] to to json *)


(** {2 JSON Decoding} *)

val decode_json_div_by_zero : Yojson.Basic.t -> div_by_zero
(** [decode_json_div_by_zero decoder] decodes a [div_by_zero] value from [decoder] *)

val decode_json_i32 : Yojson.Basic.t -> i32
(** [decode_json_i32 decoder] decodes a [i32] value from [decoder] *)

val decode_json_add_req : Yojson.Basic.t -> add_req
(** [decode_json_add_req decoder] decodes a [add_req] value from [decoder] *)

val decode_json_add_all_req : Yojson.Basic.t -> add_all_req
(** [decode_json_add_all_req decoder] decodes a [add_all_req] value from [decoder] *)

val decode_json_empty : Yojson.Basic.t -> empty
(** [decode_json_empty decoder] decodes a [empty] value from [decoder] *)


(** {2 Services} *)

(** Calculator service *)
module Calculator : sig
open Pbrt_services
open Pbrt_services.Value_mode

module Client : sig

val add : (add_req, unary, i32, unary) Client.rpc

val add_all : (add_all_req, unary, i32, unary) Client.rpc

val ping : (empty, unary, empty, unary) Client.rpc

val get_pings : (empty, unary, i32, unary) Client.rpc
end

module Server : sig
(** Produce a server implementation from handlers *)
val make :
add:((add_req, unary, i32, unary) Server.rpc -> 'handler) ->
add_all:((add_all_req, unary, i32, unary) Server.rpc -> 'handler) ->
ping:((empty, unary, empty, unary) Server.rpc -> 'handler) ->
get_pings:((empty, unary, i32, unary) Server.rpc -> 'handler) ->
unit -> 'handler Pbrt_services.Server.t
end
end
70 changes: 70 additions & 0 deletions src/examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
(action
(run ocaml-protoc --binary --pp --ml_out ./ %{deps})))

(rule
(alias runtest)
(action
(diff example01.ml.expected example01.ml)))

(rule
(alias runtest)
(action
(diff example01.mli.expected example01.mli)))

(test
(name example01)
(modules t_example01 example01)
Expand All @@ -16,6 +26,16 @@
(action
(run ocaml-protoc --pp --ml_out ./ %{deps})))

(rule
(alias runtest)
(action
(diff example03.ml.expected example03.ml)))

(rule
(alias runtest)
(action
(diff example03.mli.expected example03.mli)))

(test
(name example03)
(modules t_example03 example03)
Expand All @@ -28,6 +48,16 @@
(action
(run ocaml-protoc --pp --ml_out ./ %{deps})))

(rule
(alias runtest)
(action
(diff example04.ml.expected example04.ml)))

(rule
(alias runtest)
(action
(diff example04.mli.expected example04.mli)))

(test
(name example04)
(modules t_example04 example04)
Expand All @@ -40,6 +70,16 @@
(action
(run ocaml-protoc --binary --pp --ml_out ./ %{deps})))

(rule
(alias runtest)
(action
(diff example05.ml.expected example05.ml)))

(rule
(alias runtest)
(action
(diff example05.mli.expected example05.mli)))

(test
(name example05)
(modules t_example05 example05)
Expand All @@ -52,6 +92,16 @@
(action
(run ocaml-protoc --binary --pp --yojson --services --ml_out ./ %{deps})))

(rule
(alias runtest)
(action
(diff calculator.ml.expected calculator.ml)))

(rule
(alias runtest)
(action
(diff calculator.mli.expected calculator.mli)))

(test
(name calculator)
(modules t_calculator calculator)
Expand All @@ -64,6 +114,16 @@
(action
(run ocaml-protoc --binary --pp --yojson --services --ml_out ./ %{deps})))

(rule
(alias runtest)
(action
(diff file_server.ml.expected file_server.ml)))

(rule
(alias runtest)
(action
(diff file_server.mli.expected file_server.mli)))

(test
(name file_server)
(modules file_server) ; just check that it compiles
Expand All @@ -76,6 +136,16 @@
(action
(run ocaml-protoc --pp --binary --ml_out ./ %{deps})))

(rule
(alias runtest)
(action
(diff orgchart.ml.expected orgchart.ml)))

(rule
(alias runtest)
(action
(diff orgchart.mli.expected orgchart.mli)))

(test
(name orgchart_ml)
(modules orgchart orgchart_ml)
Expand Down
105 changes: 105 additions & 0 deletions src/examples/example01.ml.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
[@@@ocaml.warning "-27-30-39"]

type person = {
name : string;
id : int32;
email : string;
phone : string list;
}

let rec default_person
?name:((name:string) = "")
?id:((id:int32) = 0l)
?email:((email:string) = "")
?phone:((phone:string list) = [])
() : person = {
name;
id;
email;
phone;
}

type person_mutable = {
mutable name : string;
mutable id : int32;
mutable email : string;
mutable phone : string list;
}

let default_person_mutable () : person_mutable = {
name = "";
id = 0l;
email = "";
phone = [];
}

[@@@ocaml.warning "-27-30-39"]

(** {2 Formatters} *)

let rec pp_person fmt (v:person) =
let pp_i fmt () =
Pbrt.Pp.pp_record_field ~first:true "name" Pbrt.Pp.pp_string fmt v.name;
Pbrt.Pp.pp_record_field ~first:false "id" Pbrt.Pp.pp_int32 fmt v.id;
Pbrt.Pp.pp_record_field ~first:false "email" Pbrt.Pp.pp_string fmt v.email;
Pbrt.Pp.pp_record_field ~first:false "phone" (Pbrt.Pp.pp_list Pbrt.Pp.pp_string) fmt v.phone;
in
Pbrt.Pp.pp_brk pp_i fmt ()

[@@@ocaml.warning "-27-30-39"]

(** {2 Protobuf Encoding} *)

let rec encode_pb_person (v:person) encoder =
Pbrt.Encoder.string v.name encoder;
Pbrt.Encoder.key 1 Pbrt.Bytes encoder;
Pbrt.Encoder.int32_as_varint v.id encoder;
Pbrt.Encoder.key 2 Pbrt.Varint encoder;
Pbrt.Encoder.string v.email encoder;
Pbrt.Encoder.key 3 Pbrt.Bytes encoder;
Pbrt.List_util.rev_iter_with (fun x encoder ->
Pbrt.Encoder.string x encoder;
Pbrt.Encoder.key 4 Pbrt.Bytes encoder;
) v.phone encoder;
()

[@@@ocaml.warning "-27-30-39"]

(** {2 Protobuf Decoding} *)

let rec decode_pb_person d =
let v = default_person_mutable () in
let continue__= ref true in
while !continue__ do
match Pbrt.Decoder.key d with
| None -> (
v.phone <- List.rev v.phone;
); continue__ := false
| Some (1, Pbrt.Bytes) -> begin
v.name <- Pbrt.Decoder.string d;
end
| Some (1, pk) ->
Pbrt.Decoder.unexpected_payload "Message(person), field(1)" pk
| Some (2, Pbrt.Varint) -> begin
v.id <- Pbrt.Decoder.int32_as_varint d;
end
| Some (2, pk) ->
Pbrt.Decoder.unexpected_payload "Message(person), field(2)" pk
| Some (3, Pbrt.Bytes) -> begin
v.email <- Pbrt.Decoder.string d;
end
| Some (3, pk) ->
Pbrt.Decoder.unexpected_payload "Message(person), field(3)" pk
| Some (4, Pbrt.Bytes) -> begin
v.phone <- (Pbrt.Decoder.string d) :: v.phone;
end
| Some (4, pk) ->
Pbrt.Decoder.unexpected_payload "Message(person), field(4)" pk
| Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind
done;
({
name = v.name;
id = v.id;
email = v.email;
phone = v.phone;
} : person)
Loading

0 comments on commit 1aa73e8

Please sign in to comment.