summaryrefslogtreecommitdiff
path: root/tests/rust_usage_test
diff options
context:
space:
mode:
authorCasper <casperneo@uchicago.edu>2020-07-23 16:30:27 -0700
committerGitHub <noreply@github.com>2020-07-23 16:30:27 -0700
commit043b52bd4a81fea3b8128d813852636989ff1716 (patch)
tree50796aa7f073cc191ce7a559445178d996a031d9 /tests/rust_usage_test
parentc8fa0afdfc907452edbf2f5f3110f2f673fc4a70 (diff)
downloadflatbuffers-043b52bd4a81fea3b8128d813852636989ff1716.tar.gz
flatbuffers-043b52bd4a81fea3b8128d813852636989ff1716.tar.bz2
flatbuffers-043b52bd4a81fea3b8128d813852636989ff1716.zip
Optional Scalars support for Rust (#6034)
* First draft of rust optionals * Code cleanup around ftBool and ftVectorOfBool * Tests for Rust optional scalars * test bools too Co-authored-by: Casper Neo <cneo@google.com>
Diffstat (limited to 'tests/rust_usage_test')
-rw-r--r--tests/rust_usage_test/Cargo.toml4
-rw-r--r--tests/rust_usage_test/tests/integration_test.rs5
-rw-r--r--tests/rust_usage_test/tests/optional_scalars_test.rs52
3 files changed, 60 insertions, 1 deletions
diff --git a/tests/rust_usage_test/Cargo.toml b/tests/rust_usage_test/Cargo.toml
index e6a5c1df..6fa98a9a 100644
--- a/tests/rust_usage_test/Cargo.toml
+++ b/tests/rust_usage_test/Cargo.toml
@@ -1,7 +1,9 @@
[package]
name = "rust_usage_test"
version = "0.1.0"
-authors = ["Robert Winslow <hello@rwinslow.com>", "FlatBuffers Maintainers"]
+authors = ["Robert Winslow <hello@rwinslow.com>",
+ "Casper Neo <cneo@google.com>",
+ "FlatBuffers Maintainers"]
[dependencies]
flatbuffers = { path = "../../rust/flatbuffers" }
diff --git a/tests/rust_usage_test/tests/integration_test.rs b/tests/rust_usage_test/tests/integration_test.rs
index 1aea51c6..b861cf03 100644
--- a/tests/rust_usage_test/tests/integration_test.rs
+++ b/tests/rust_usage_test/tests/integration_test.rs
@@ -27,6 +27,7 @@ extern crate serde_derive;
extern crate quickcheck_derive;
mod flexbuffers_tests;
+mod optional_scalars_test;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/include_test1_generated.rs"]
@@ -41,6 +42,10 @@ pub mod include_test2_generated;
mod monster_test_generated;
pub use monster_test_generated::my_game;
+#[allow(dead_code, unused_imports)]
+#[path = "../../optional_scalars_generated.rs"]
+mod optional_scalars_generated;
+
#[rustfmt::skip] // TODO: Use standard rust formatting and remove dead code.
#[allow(dead_code)]
mod flatbuffers_tests {
diff --git a/tests/rust_usage_test/tests/optional_scalars_test.rs b/tests/rust_usage_test/tests/optional_scalars_test.rs
new file mode 100644
index 00000000..4b63de2e
--- /dev/null
+++ b/tests/rust_usage_test/tests/optional_scalars_test.rs
@@ -0,0 +1,52 @@
+#[allow(dead_code, unused_imports)]
+#[path = "../../optional_scalars_generated.rs"]
+mod optional_scalars_generated;
+use crate::optional_scalars_generated::optional_scalars::*;
+
+// There are 3 variants of scalars in tables - those specified with default=42,
+// optional scalars, and those with nothing specified (implicitly default=0).
+// This tests that you can read what you write.
+macro_rules! make_test {
+ (
+ $test_name: ident,
+ $just: ident, $default: ident, $maybe: ident,
+ $five: expr, $zero: expr, $fortytwo: expr
+ ) => {
+ #[test]
+ fn $test_name() {
+ let mut builder = flatbuffers::FlatBufferBuilder::new();
+ // Test five makes sense when specified.
+ let ss = ScalarStuff::create(&mut builder, &ScalarStuffArgs {
+ $just: $five,
+ $default: $five,
+ $maybe: Some($five),
+ ..Default::default()
+ });
+ builder.finish(ss, None);
+
+ let s = flatbuffers::get_root::<ScalarStuff>(builder.finished_data());
+ assert_eq!(s.$just(), $five);
+ assert_eq!(s.$default(), $five);
+ assert_eq!(s.$maybe(), Some($five));
+
+ // Test defaults are used when not specified.
+ let s = flatbuffers::get_root::<ScalarStuff>(&[0; 8]);
+ assert_eq!(s.$just(), $zero);
+ assert_eq!(s.$default(), $fortytwo);
+ assert_eq!(s.$maybe(), None);
+ }
+
+ };
+}
+
+make_test!(optional_i8, just_i8, default_i8, maybe_i8, 5, 0, 42);
+make_test!(optional_u8, just_u8, default_u8, maybe_u8, 5, 0, 42);
+make_test!(optional_i16, just_i16, default_i16, maybe_i16, 5, 0, 42);
+make_test!(optional_u16, just_u16, default_u16, maybe_u16, 5, 0, 42);
+make_test!(optional_i32, just_i32, default_i32, maybe_i32, 5, 0, 42);
+make_test!(optional_u32, just_u32, default_u32, maybe_u32, 5, 0, 42);
+make_test!(optional_i64, just_i64, default_i64, maybe_i64, 5, 0, 42);
+make_test!(optional_u64, just_u64, default_u64, maybe_u64, 5, 0, 42);
+make_test!(optional_f32, just_f32, default_f32, maybe_f32, 5.0, 0.0, 42.0);
+make_test!(optional_f64, just_f64, default_f64, maybe_f64, 5.0, 0.0, 42.0);
+make_test!(optional_bool, just_bool, default_bool, maybe_bool, true, false, true);