From 90cb3fcaec1c71e0b6d13897e612f4111ed56d86 Mon Sep 17 00:00:00 2001 From: Martin DeMello Date: Sat, 4 Feb 2023 18:50:35 -0800 Subject: [PATCH] add some basic tests --- .github/workflows/ci.yml | 4 +-- tests/dune | 4 +++ tests/get_set.ml | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/dune create mode 100644 tests/get_set.ml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c9f0937..f1a8e50 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: run: | echo '' | bash -c "sh <(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)" opam init - opam install -y dune dune-configurator + opam install -y dune dune-configurator alcotest - name: Build ocaml-gtk run: | eval `opam env` @@ -38,7 +38,7 @@ jobs: run: | echo '' | bash -c "sh <(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)" opam init - opam install -y dune dune-configurator + opam install -y dune dune-configurator alcotest - name: Build ocaml-gtk run: | eval `opam env` diff --git a/tests/dune b/tests/dune new file mode 100644 index 0000000..89ac4c9 --- /dev/null +++ b/tests/dune @@ -0,0 +1,4 @@ +(tests + (names get_set) + (modes exe) + (libraries alcotest ocamlgtk)) diff --git a/tests/get_set.ml b/tests/get_set.ml new file mode 100644 index 0000000..21dc0b6 --- /dev/null +++ b/tests/get_set.ml @@ -0,0 +1,58 @@ +let e = epsilon_float + +(* For tests that need to be run within an initialised Gtk.application *) +let run_app f = + let app = Gtk.application "org.gtk.example" Gio.ApplicationFlags.flags_none in + ignore @@ app#signal_connect_activate f; + ignore @@ app#run [||] + +let int () = + run_app begin fun () -> + let p = Gtk.picture () in + let _ = p#set_margin_start 1 in + let v = p#get_margin_start in + Alcotest.(check int) "Set and retrieved gint" v 1 + end + +let double () = + run_app begin fun () -> + let ps = Gtk.page_setup () in + let _ = ps#set_left_margin 1.0 1 in + let margin = ps#get_left_margin 1 in + Alcotest.(check @@ float e) "Set and retrieved gdouble" margin 1.0 + end + +let float () = + run_app begin fun () -> + let a = Gtk.aspect_frame 1.0 1.0 1.0 true in + a#set_xalign 0.5; + let v = a#get_xalign in + Alcotest.(check @@ float e) "Set and retrieved gfloat" v 0.5 + end + +let string () = + run_app begin fun () -> + let label = Gtk.label "hello" in + let v = label#get_label in + Alcotest.(check string) "Set and retrieved string" v "hello" + end + +let string_option () = + run_app begin fun () -> + let button = Gtk.Button.new_with_label "hello" in + let v = button#get_label in + let v = match v with (Some s) -> s | None -> "none" in + Alcotest.(check string) "Set and retrieved string option" v "hello" + end + +let test_set = [ + ("int", `Quick, int); + ("double", `Quick, double); + ("float", `Quick, float); + ("string", `Quick, string); + ("string_option", `Quick, string_option) +] + +let () = + Alcotest.run "Get and set tests" + [ ("Tests", test_set) ]