Skip to content

Commit

Permalink
Tets (#1)
Browse files Browse the repository at this point in the history
* g

* g

* g

* g
  • Loading branch information
Jasperav authored May 19, 2022
1 parent 2fe8489 commit ca9e427
Show file tree
Hide file tree
Showing 22 changed files with 966 additions and 110 deletions.
80 changes: 41 additions & 39 deletions .github/workflows/continuous-integration-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
default: true
profile: minimal
components: rustfmt
- name: build b_tests
run: cargo build --package b_tests
- name: rustfmt
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -82,45 +84,45 @@ jobs:
command: test
args: --no-default-features

no-std:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
with:
submodules: recursive
- name: install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
default: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- name: install cargo-no-std-check
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-no-std-check
- name: prost cargo-no-std-check
uses: actions-rs/cargo@v1
with:
command: no-std-check
args: --manifest-path Cargo.toml --no-default-features
- name: prost-types cargo-no-std-check
uses: actions-rs/cargo@v1
with:
command: no-std-check
args: --manifest-path prost-types/Cargo.toml --no-default-features
# prost-build depends on prost with --no-default-features, but when
# prost-build is built through the workspace, prost typically has default
# features enabled due to vagaries in Cargo workspace feature resolution.
# This additional check ensures that prost-build does not rely on any of
# prost's default features to compile.
- name: prost-build check
uses: actions-rs/cargo@v1
with:
command: check
args: --manifest-path prost-build/Cargo.toml
# no-std:
# runs-on: ubuntu-latest
# steps:
# - name: checkout
# uses: actions/checkout@v2
# with:
# submodules: recursive
# - name: install toolchain
# uses: actions-rs/toolchain@v1
# with:
# toolchain: nightly
# default: true
# profile: minimal
# - uses: Swatinem/rust-cache@v1
# - name: install cargo-no-std-check
# uses: actions-rs/cargo@v1
# with:
# command: install
# args: cargo-no-std-check
# - name: prost cargo-no-std-check
# uses: actions-rs/cargo@v1
# with:
# command: no-std-check
# args: --manifest-path Cargo.toml --no-default-features
# - name: prost-types cargo-no-std-check
# uses: actions-rs/cargo@v1
# with:
# command: no-std-check
# args: --manifest-path prost-types/Cargo.toml --no-default-features
# # prost-build depends on prost with --no-default-features, but when
# # prost-build is built through the workspace, prost typically has default
# # features enabled due to vagaries in Cargo workspace feature resolution.
# # This additional check ensures that prost-build does not rely on any of
# # prost's default features to compile.
# - name: prost-build check
# uses: actions-rs/cargo@v1
# with:
# command: check
# args: --manifest-path prost-build/Cargo.toml

vendored:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ std = []
[dependencies]
bytes = { version = "1", default-features = false }
prost-derive = { version = "0.10.0", path = "prost-derive", optional = true }
uuid = { version = "1", features = ["v4"] }

[dev-dependencies]
criterion = "0.3"
Expand Down
3 changes: 3 additions & 0 deletions b_tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/src/b_generated
/src/generated
/src/protos
6 changes: 6 additions & 0 deletions b_tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
[package]
name = "b_tests"
version = "0.1.0"
authors = ["Jasper Visser <jasperav@hotmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
prost-build = { path = "../prost-build" }
protobuf_strict = { git = "https://github.com/Jasperav/protobuf_strict.git" }

[dependencies]
uuid = { version = "1", features = ["v4"] }
prost = { path = "../" }
66 changes: 66 additions & 0 deletions b_tests/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use prost_build::{Config, CustomType};
use std::io::Write;

fn main() {
let src = std::env::current_dir().unwrap().join("src");

protobuf_strict::write_protos(&src);

macro_rules! initialize_dir {
($name: expr) => {{
// Maybe the dir doesn't exists yet
let _ = std::fs::remove_dir_all(src.join($name));
std::fs::create_dir(src.join($name)).unwrap();
std::fs::File::create(src.join($name).join("mod.rs")).unwrap()
}};
}

let mut b_generated = initialize_dir!("b_generated");
let mut generated = initialize_dir!("generated");
let mut protos = vec![];
let paths = std::fs::read_dir("./src/protos").unwrap();

for path in paths {
let path = path.unwrap();
let file_name = path.file_name();
let s = file_name.to_str().unwrap();

protos.push("src/protos/".to_string() + s);
}

macro_rules! go_generate {
($name: expr, $file: expr, $config: expr) => {
std::env::set_var("OUT_DIR", src.join($name).to_str().unwrap());

$config
.compile_protos(protos.as_slice(), &["src/protos/".to_string()])
.unwrap();

let paths = std::fs::read_dir("./src/".to_string() + $name).unwrap();

for path in paths {
let path = path.unwrap();
let file_name = path.file_name();
let s = file_name.to_str().unwrap();

if s == "mod.rs" {
continue;
}

let m = s.strip_suffix(".rs").unwrap();

writeln!($file, "#[rustfmt::skip]\nmod {};\npub use {}::*;", m, m).unwrap();
}
};
}

let mut config = Config::new();

config
.add_types_mapping(protobuf_strict::get_uuids().to_vec(), CustomType::Uuid)
.strict_messages()
.inline_enums();

go_generate!("b_generated", b_generated, config);
go_generate!("generated", generated, Config::new());
}
191 changes: 187 additions & 4 deletions b_tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,191 @@
mod b_generated;
mod generated;

#[cfg(test)]
mod tests {
mod test {
use super::*;
use prost::Message;
use std::str::FromStr;
use uuid::Uuid;

const UUID: &'static str = "cd663747-6cb1-4ddc-bdfe-3dc76db62724";

fn get_uuid() -> Uuid {
uuid::Uuid::from_str(UUID).unwrap()
}

fn get_no_uuid() -> String {
"no_uuid".to_string()
}

fn get_custom() -> String {
"custom".to_string()
}

fn get_amount() -> i32 {
1
}

fn b_generated_order() -> b_generated::Order {
b_generated::Order {
gender: b_generated::Gender::Female,
genders: vec![b_generated::Gender::Female, b_generated::Gender::Other],
currency: Some(b_generated::Currency {
c: Some(b_generated::currency::C::Amount(get_amount())),
}),
o_currency: None,
currencies: vec![
b_generated::Currency {
c: Some(b_generated::currency::C::Custom(get_custom())),
},
b_generated::Currency {
c: Some(b_generated::currency::C::Amount(get_amount())),
},
],
uuid: get_uuid(),
no_uuid: get_no_uuid(),
repeated_uuids: vec![get_uuid(), get_uuid()],
no_uuids: vec![get_custom()],
order_inner: b_generated::order::OrderInner::InnerAnother,
order_inners: vec![
b_generated::order::OrderInner::InnerAnother,
b_generated::order::OrderInner::InnerAnother2,
],
something: Some(b_generated::order::Something::AlsoUuid(get_uuid())),
}
}

fn generated_order() -> generated::Order {
generated::Order {
gender: generated::Gender::Female as i32,
genders: vec![
generated::Gender::Female as i32,
generated::Gender::Other as i32,
],
currency: Some(generated::Currency {
c: Some(generated::currency::C::Amount(get_amount())),
}),
o_currency: None,
currencies: vec![
generated::Currency {
c: Some(generated::currency::C::Custom(get_custom())),
},
generated::Currency {
c: Some(generated::currency::C::Amount(get_amount())),
},
],
uuid: get_uuid().to_string(),
no_uuid: get_no_uuid(),
repeated_uuids: vec![get_uuid().to_string(), get_uuid().to_string()],
no_uuids: vec![get_custom()],
order_inner: generated::order::OrderInner::InnerAnother as i32,
order_inners: vec![
generated::order::OrderInner::InnerAnother as i32,
generated::order::OrderInner::InnerAnother2 as i32,
],
something: Some(generated::order::Something::AlsoUuid(
get_uuid().to_string(),
)),
}
}

#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
fn equal() {
let g = b_generated_order();
let b = generated_order();
let g = g.encode_buffer().unwrap();
let b = b.encode_buffer().unwrap();

assert_eq!(g, b);
assert_eq!(g.encoded_len(), b.encoded_len());

// Check if encoding works
b_generated::Order::decode(b.as_slice()).unwrap();
generated::Order::decode(g.as_slice()).unwrap();
}

fn check_order(order: generated::Order) {
let b = order.encode_buffer().unwrap();

b_generated::Order::decode(b.as_slice()).unwrap();
}

macro_rules! write_invalid_test {
($method_name: ident, $order: ident, $change: tt) => {
#[test]
#[should_panic]
fn $method_name() {
let mut $order = generated_order();

$change;

check_order($order);
}
};
}

write_invalid_test!(
invalid_gender_zero,
order,
({
order.gender = 0;
})
);
write_invalid_test!(
invalid_gender_over_max,
order,
({
order.gender = 999;
})
);
write_invalid_test!(
invalid_uuids,
order,
({
order.repeated_uuids = vec![get_uuid().to_string(), get_no_uuid().to_string()];
})
);
write_invalid_test!(
empty_currency,
order,
({
order.currency = None;
})
);
write_invalid_test!(
invalid_uuid,
order,
({
order.uuid = order.uuid[1..].to_string();
})
);

write_invalid_test!(
invalid_inner_zero,
order,
({
order.order_inner = 0;
})
);
write_invalid_test!(
invalid_inner_over_max,
order,
({
order.order_inner = 999;
})
);
write_invalid_test!(
empty_something,
order,
({
order.something = None;
})
);
write_invalid_test!(
something_invalid_uuid,
order,
({
order.something = Some(generated::order::Something::AlsoUuid(get_no_uuid()));
})
);
}
Loading

0 comments on commit ca9e427

Please sign in to comment.