This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,15 @@
This tests error cases for `ppx_cstruct`.
It only deals with the errors raised by the ppx itself (such as when the type is
not supported), not errors in the generated code.
To add a test case:
- create a file in this directory named `something.ml` with the error case
- create an empty file named `something.ml.expected`
- don't forget to add these files to git
- run `dune runtest`: it displays a diff on `dune.inc`
- run `dune promote`: it updates `dune.inc` with the generated test case
- run `dune runtest`: it runs the test and displays a diff against the empty
expected output
- run `dune promote`: it updates `something.ml.expected`

View file

@ -0,0 +1,4 @@
[%%cenum
type t = A [@id ""]
[@@uint8_t]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: invalid id

View file

@ -0,0 +1,4 @@
[%%cenum
type t = A of int
[@@uint8_t]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: invalid cenum variant

View file

@ -0,0 +1,4 @@
[%%cenum
type t =
| A
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: invalid cenum attributes

View file

@ -0,0 +1,5 @@
[%%cenum
type t =
{ x : uint8_t
}
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: expected variant type

View file

@ -0,0 +1,5 @@
[%%cenum
type t =
| A
[@@uint9_t]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: enum: unknown width specifier uint9_t

View file

@ -0,0 +1,6 @@
[%%cstruct
type t =
{ a : uint8_t
}
[@@little_endian ""]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: no attribute payload expected

View file

@ -0,0 +1,7 @@
[%%cstruct
type dup = {
x : uint8_t;
y : uint8_t;
x : uint8_t;
} [@@little_endian]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: field x is present several times in this type

View file

@ -0,0 +1,6 @@
[%%cstruct
type t = {
a: uint8_t [@len 8l]
}
[@@little_endian]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: [@len] argument should be an integer

View file

@ -0,0 +1,6 @@
[%%cstruct
type t = {
a: uint8_t [@len ""]
}
[@@little_endian]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: [@len] argument should be an integer

View file

@ -0,0 +1,6 @@
[%%cstruct
type t = {
a: uint8_t [@len 0]
}
[@@little_endian]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: [@len] argument should be > 0

View file

@ -0,0 +1,6 @@
[%%cstruct
type t = {
a : (uint8_t [@len 2]) [@len 3]
}
[@@little_endian]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: multiple field length attribute

View file

@ -0,0 +1,3 @@
[%%cstruct
type t = int
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: record type declaration expected

View file

@ -0,0 +1,6 @@
[%%cstruct
type t = {
a : uint8_t list
}
[@@little_endian]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: type identifier expected

View file

@ -0,0 +1,7 @@
[%%cstruct
type t =
{ a : uint8_t
}
[@@little_endian]
[@@other_attr]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: too many attributes

View file

@ -0,0 +1,6 @@
[%%cstruct
type t =
{ a : uint8_t
}
[@@unknown_endian]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: unknown endian unknown_endian, should be little_endian, big_endian, host_endian or bi_endian

View file

@ -0,0 +1,6 @@
[%%cstruct
type t =
{ a : uint9_t
}
[@@little_endian]
]

View file

@ -0,0 +1 @@
Error: ppx_cstruct: Unknown type uint9_t

View file

@ -0,0 +1,27 @@
(executable
(name pp)
(modules pp)
(preprocess
(action
(run %{bin:cppo} -V OCAML:%{ocaml_version} %{input-file})))
(libraries ppx_cstruct ocaml-migrate-parsetree))
(executable
(name gen_tests)
(modules gen_tests))
(include dune.inc)
(rule
(targets dune.inc.gen)
(deps
(source_tree .))
(action
(with-stdout-to
%{targets}
(run ./gen_tests.exe))))
(rule
(alias runtest)
(action
(diff dune.inc dune.inc.gen)))

View file

@ -0,0 +1,224 @@
(rule
(deps pp.exe (:input cenum_id_payload.ml))
(targets cenum_id_payload.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cenum_id_payload.ml.expected cenum_id_payload.ml.errors)))
(rule
(deps pp.exe (:input cenum_invalid_type.ml))
(targets cenum_invalid_type.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cenum_invalid_type.ml.expected cenum_invalid_type.ml.errors)))
(rule
(deps pp.exe (:input cenum_no_attribute.ml))
(targets cenum_no_attribute.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cenum_no_attribute.ml.expected cenum_no_attribute.ml.errors)))
(rule
(deps pp.exe (:input cenum_not_a_variant.ml))
(targets cenum_not_a_variant.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cenum_not_a_variant.ml.expected cenum_not_a_variant.ml.errors)))
(rule
(deps pp.exe (:input cenum_unknown_attribute.ml))
(targets cenum_unknown_attribute.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cenum_unknown_attribute.ml.expected cenum_unknown_attribute.ml.errors)))
(rule
(deps pp.exe (:input cstruct_attribute_payload.ml))
(targets cstruct_attribute_payload.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_attribute_payload.ml.expected cstruct_attribute_payload.ml.errors)))
(rule
(deps pp.exe (:input cstruct_duplicate_field.ml))
(targets cstruct_duplicate_field.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_duplicate_field.ml.expected cstruct_duplicate_field.ml.errors)))
(rule
(deps pp.exe (:input cstruct_len_int32.ml))
(targets cstruct_len_int32.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_len_int32.ml.expected cstruct_len_int32.ml.errors)))
(rule
(deps pp.exe (:input cstruct_len_not_int.ml))
(targets cstruct_len_not_int.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_len_not_int.ml.expected cstruct_len_not_int.ml.errors)))
(rule
(deps pp.exe (:input cstruct_len_zero.ml))
(targets cstruct_len_zero.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_len_zero.ml.expected cstruct_len_zero.ml.errors)))
(rule
(deps pp.exe (:input cstruct_multiple_len.ml))
(targets cstruct_multiple_len.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_multiple_len.ml.expected cstruct_multiple_len.ml.errors)))
(rule
(deps pp.exe (:input cstruct_not_a_record.ml))
(targets cstruct_not_a_record.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_not_a_record.ml.expected cstruct_not_a_record.ml.errors)))
(rule
(deps pp.exe (:input cstruct_not_an_identifier.ml))
(targets cstruct_not_an_identifier.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_not_an_identifier.ml.expected cstruct_not_an_identifier.ml.errors)))
(rule
(deps pp.exe (:input cstruct_several_attributes.ml))
(targets cstruct_several_attributes.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_several_attributes.ml.expected cstruct_several_attributes.ml.errors)))
(rule
(deps pp.exe (:input cstruct_unknown_endian.ml))
(targets cstruct_unknown_endian.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_unknown_endian.ml.expected cstruct_unknown_endian.ml.errors)))
(rule
(deps pp.exe (:input cstruct_unknown_type.ml))
(targets cstruct_unknown_type.ml.errors)
(action
(progn
(with-stderr-to %{targets}
(run ./pp.exe -no-color --impl %{input}))
(bash "sed -i.bak '1d' %{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff cstruct_unknown_type.ml.expected cstruct_unknown_type.ml.errors)))

View file

@ -0,0 +1,31 @@
let output_stanzas name =
Printf.printf
{|
(rule
(deps pp.exe (:input %s))
(targets %s.errors)
(action
(progn
(with-stderr-to %%{targets}
(run ./pp.exe -no-color --impl %%{input}))
(bash "sed -i.bak '1d' %%{targets}"))))
(rule
(alias runtest)
(package ppx_cstruct)
(action
(diff %s.expected %s.errors)))
|}
name name name name
let is_test = function
| "pp.ml" -> false
| "pp.pp.ml" -> false
| "gen_tests.ml" -> false
| e -> Filename.check_suffix e ".ml"
let () =
Sys.readdir "."
|> Array.to_list
|> List.sort String.compare
|> List.filter is_test
|> List.iter output_stanzas

View file

@ -0,0 +1,11 @@
(* we can just use exit from 4.07 onwards, but 4.06
* and earlier executed at_exit recursively *)
external sys_exit : int -> 'a = "caml_sys_exit"
let () = at_exit (fun () -> sys_exit 0)
#if OCAML_VERSION >= (4,8,0)
let () = Clflags.(error_style := Some Short)
#endif
let () = Clflags.(color := Some Never)
let () = Ppxlib.Driver.standalone ()