Skip to content

Commit 0e5b4ef

Browse files
committed
Add no_std tests
1 parent a1ab713 commit 0e5b4ef

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

tests/RustTest.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ else
4141
fi
4242

4343
cargo bench $TARGET_FLAG
44+
45+
# make sure that the no_std variant builds
46+
rustup install nightly
47+
48+
cd ../../rust/flatbuffers
49+
cargo +nightly build --no-default-features
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#![no_std]
2+
3+
extern crate flatbuffers;
4+
5+
#[allow(dead_code, unused_imports)]
6+
#[path = "../../monster_test_generated.rs"]
7+
mod monster_test_generated;
8+
pub use monster_test_generated::my_game;
9+
10+
static MONSTER_DATA: &[u8] = include_bytes!("../../monsterdata_test.mon");
11+
12+
// Note: Copied from `bin/monster_example.rs`
13+
#[test]
14+
fn no_std_example_data() {
15+
let monster = my_game::example::get_root_as_monster(MONSTER_DATA);
16+
17+
assert_eq!(monster.hp(), 80);
18+
assert_eq!(monster.mana(), 150);
19+
assert_eq!(monster.name(), "MyMonster");
20+
}
21+
22+
// Note: the rest was copied from the `roundtrip_generated_code` module in
23+
// `tests/integration_test.rs`. Really just making sure that everything compiles
24+
// here, the behavior has already been tested.
25+
26+
fn build_mon<'a, 'b>(
27+
builder: &'a mut flatbuffers::FlatBufferBuilder,
28+
args: &'b my_game::example::MonsterArgs,
29+
) -> my_game::example::Monster<'a> {
30+
let mon = my_game::example::Monster::create(builder, &args);
31+
my_game::example::finish_monster_buffer(builder, mon);
32+
my_game::example::get_root_as_monster(builder.finished_data())
33+
}
34+
35+
#[test]
36+
fn vector_of_struct_store_with_type_inference() {
37+
let mut b = flatbuffers::FlatBufferBuilder::new();
38+
let v = b.create_vector(&[
39+
my_game::example::Test::new(127, -128),
40+
my_game::example::Test::new(3, 123),
41+
my_game::example::Test::new(100, 101),
42+
]);
43+
let name = b.create_string("foo");
44+
let m = build_mon(
45+
&mut b,
46+
&my_game::example::MonsterArgs {
47+
name: Some(name),
48+
test4: Some(v),
49+
..Default::default()
50+
},
51+
);
52+
assert_eq!(
53+
m.test4().unwrap(),
54+
&[
55+
my_game::example::Test::new(127, -128),
56+
my_game::example::Test::new(3, 123),
57+
my_game::example::Test::new(100, 101)
58+
][..]
59+
);
60+
}

0 commit comments

Comments
 (0)