[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 09/12] Rust bindings: Add bindtests



---
 generator/bindtests.ml | 65 +++++++++++++++++++++++++++++++++++++++++-
 generator/main.ml      |  2 +-
 generator/rust.mli     |  5 +++-
 rust/src/bin/.gitkeep  |  0
 4 files changed, 69 insertions(+), 3 deletions(-)
 create mode 100644 rust/src/bin/.gitkeep

diff --git a/generator/bindtests.ml b/generator/bindtests.ml
index 8a5682d5e..e88e71c8a 100644
--- a/generator/bindtests.ml
+++ b/generator/bindtests.ml
@@ -984,7 +984,70 @@ and generate_php_bindtests () =
   dump "bindtests"
 
 and generate_rust_bindtests () =
-  generate_header CStyle GPLv2plus
+  generate_header CStyle GPLv2plus;
+
+  pr "extern crate guestfs;\n";
+  pr "use guestfs::*;\n";
+  pr "use std::default::Default;\n";
+  pr "\n";
+  pr "fn main() {\n";
+  pr "    let g = match Handle::create() {\n";
+  pr "        Ok(g) => g,\n";
+  pr "        Err(e) => panic!(format!(\" could not create handle {}\", e)),\n";
+  pr "    };\n";
+  generate_lang_bindtests (
+    fun f args optargs ->
+      pr "    g.%s(" f;
+      let needs_comma = ref false in
+      List.iter (
+        fun arg ->
+          if !needs_comma then pr ", ";
+          needs_comma := true;
+          match arg with
+          | CallString s -> pr "\"%s\"" s
+          | CallOptString None -> pr "None"
+          | CallOptString (Some s) -> pr "Some(\"%s\")" s
+          | CallStringList xs ->
+            pr "&vec![%s]"
+              (String.concat ", " (List.map (sprintf "\"%s\"") xs))
+          | CallInt i -> pr "%d" i
+          | CallInt64 i -> pr "%Ldi64" i
+          | CallBool b -> pr "%b" b
+          | CallBuffer s ->
+            let f = fun x -> sprintf "%d" (Char.code x) in
+            pr "&[%s]"
+              (String.concat ", " (List.map f (String.explode s)))
+      ) args;
+      if !needs_comma then pr ", ";
+      (match optargs with
+       | None -> pr "Default::default()"
+       | Some optargs ->
+         pr "%sOptArgs{" (Rust.snake2caml f);
+         needs_comma := false;
+         List.iter (
+           fun optarg ->
+             if !needs_comma then pr ", ";
+             needs_comma := true;
+             match optarg with
+             | CallOBool (n, v) ->
+               pr "%s: Some(%b)" n v
+             | CallOInt (n, v) ->
+               pr "%s: Some(%d)" n v
+             | CallOInt64 (n, v) ->
+               pr "%s: Some(%Ldi64)" n v
+             | CallOString (n, v) ->
+               pr "%s: Some(\"%s\")" n v
+             | CallOStringList (n, xs) ->
+               pr "%s: Some(&[%s])"
+                 n (String.concat ", " (List.map (sprintf "\"%s\"") xs))
+         ) optargs;
+         if !needs_comma then pr ", ";
+         pr ".. Default::default()}";
+      );
+      pr ").expect(\"failed to run\");\n";
+  );
+  pr "    println!(\"EOF\");\n";
+  pr "}\n";
 
 (* Language-independent bindings tests - we do it this way to
  * ensure there is parity in testing bindings across all languages.
diff --git a/generator/main.ml b/generator/main.ml
index 5de89138c..ffe1bb95c 100644
--- a/generator/main.ml
+++ b/generator/main.ml
@@ -374,7 +374,7 @@ Run it from the top source directory using the command
 
   output_to "rust/src/lib.rs"
             Rust.generate_rust;
-  output_to "rust/tests/bind_test.rs"
+  output_to "rust/src/bin/bindtests.rs"
             Bindtests.generate_rust_bindtests;
 
   (* Generate the list of files generated -- last. *)
diff --git a/generator/rust.mli b/generator/rust.mli
index 4fef55d4e..5410286c8 100644
--- a/generator/rust.mli
+++ b/generator/rust.mli
@@ -16,4 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *)
 
-val generate_rust: unit -> unit
\ No newline at end of file
+val generate_rust: unit -> unit
+
+(* for bindtests.ml *)
+val snake2caml: string -> string
diff --git a/rust/src/bin/.gitkeep b/rust/src/bin/.gitkeep
new file mode 100644
index 000000000..e69de29bb
-- 
2.20.1 (Apple Git-117)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]