Skip to content

Commit

Permalink
refactor: transform some tests into snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Oct 23, 2023
1 parent 3c206de commit 0f69ed5
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 367 deletions.
367 changes: 0 additions & 367 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,370 +374,3 @@ fn compile_literal(literal: &Literal) -> TokenStream {
Literal::Bytes(b) => proc_macro2::Literal::byte_string(b).into_token_stream(),
}
}

#[cfg(test)]
mod tests {
#![allow(clippy::too_many_lines)]

use indoc::indoc;
use pretty_assertions::assert_eq;

use super::*;

fn parse(input: &str, expect: &str) {
let parsed = Schema::parse(input).unwrap();
println!("==========\n{parsed}");

let compiled = compile_schema(&parsed);
println!("----------\n{compiled}");

let pretty = prettyplease::unparse(&syn::parse2(compiled.clone()).unwrap());
println!("----------\n{pretty}==========");

assert_eq!(expect, pretty);
}

#[test]
fn basic_module() {
let input = indoc! {r#"
/// Hello world!
mod sample {}
"#};
let expect = indoc! {r#"
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
/// Hello world!
pub mod sample {
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
}
"#};

parse(input, expect);
}

#[test]
fn basic_struct() {
let input = indoc! {r#"
/// Hello world!
struct Sample {
field1: u32 @1,
field2: bytes @2,
field3: (bool, [i16; 4]) @3,
}
"#};
let expect = indoc! {r#"
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
/// Hello world!
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub struct Sample {
pub field1: u32,
pub field2: Vec<u8>,
pub field3: (bool, [i16; 4]),
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
1,
|w| {
::stef::buf::encode_u32(w, self.field1);
},
);
::stef::buf::encode_field(
w,
2,
|w| {
::stef::buf::encode_bytes(w, &self.field2);
},
);
::stef::buf::encode_field(
w,
3,
|w| {
::stef::buf::encode_bool(w, self.field3.0);
::stef::buf::encode_array(
w,
&&(self.field3.1),
|w, v| {
::stef::buf::encode_i16(w, *v);
},
);
},
);
::stef::buf::encode_u32(w, ::stef::buf::END_MARKER);
}
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::type_complexity, clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
let mut field1: Option<u32> = None;
let mut field2: Option<Vec<u8>> = None;
let mut field3: Option<(bool, [i16; 4])> = None;
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
1 => field1 = Some(::stef::buf::decode_u32(r)?),
2 => field2 = Some(::stef::buf::decode_bytes(r)?),
3 => {
field3 = Some(
{
Ok::<
_,
::stef::buf::Error,
>((
::stef::buf::decode_bool(r)?,
::stef::buf::decode_array(
r,
|r| { ::stef::buf::decode_i16(r) },
)?,
))
}?,
);
}
_ => continue,
}
}
Ok(Self {
field1: field1
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("field1"),
})?,
field2: field2
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: Some("field2"),
})?,
field3: field3
.ok_or(::stef::buf::Error::MissingField {
id: 3,
name: Some("field3"),
})?,
})
}
}
"#};

parse(input, expect);
}

#[test]
fn basic_enum() {
let input = indoc! {r#"
/// Hello world!
enum Sample {
Variant1 @1,
Variant2(u32 @1, u8 @2) @2,
Variant3 {
field1: string @1,
field2: vec<bool> @2,
} @3,
}
"#};
let expect = indoc! {r#"
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
/// Hello world!
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub enum Sample {
Variant1,
Variant2(u32, u8),
Variant3 { field1: String, field2: Vec<bool> },
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(
clippy::borrow_deref_ref,
clippy::semicolon_if_nothing_returned,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
match self {
Self::Variant1 => {
::stef::buf::encode_id(w, 1);
}
Self::Variant2(n0, n1) => {
::stef::buf::encode_id(w, 2);
::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, *n0) });
::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u8(w, *n1) });
::stef::buf::encode_u32(w, ::stef::buf::END_MARKER);
}
Self::Variant3 { field1, field2 } => {
::stef::buf::encode_id(w, 3);
::stef::buf::encode_field(
w,
1,
|w| { ::stef::buf::encode_string(w, &*field1) },
);
::stef::buf::encode_field(
w,
2,
|w| {
::stef::buf::encode_vec(
w,
&*field2,
|w, v| {
::stef::buf::encode_bool(w, *v);
},
)
},
);
::stef::buf::encode_u32(w, ::stef::buf::END_MARKER);
}
}
}
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
match ::stef::buf::decode_id(r)? {
1 => Ok(Self::Variant1),
2 => {
let mut n0: Option<u32> = None;
let mut n1: Option<u8> = None;
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
1 => n0 = Some(::stef::buf::decode_u32(r)?),
2 => n1 = Some(::stef::buf::decode_u8(r)?),
_ => continue,
}
}
Ok(
Self::Variant2(
n0
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: None,
})?,
n1
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: None,
})?,
),
)
}
3 => {
let mut field1: Option<String> = None;
let mut field2: Option<Vec<bool>> = None;
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
1 => field1 = Some(::stef::buf::decode_string(r)?),
2 => {
field2 = Some(
::stef::buf::decode_vec(
r,
|r| { ::stef::buf::decode_bool(r) },
)?,
);
}
_ => continue,
}
}
Ok(Self::Variant3 {
field1: field1
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("field1"),
})?,
field2: field2
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: Some("field2"),
})?,
})
}
id => Err(::stef::buf::Error::UnknownVariant(id)),
}
}
}
"#};

parse(input, expect);
}

#[test]
fn basic_alias() {
let input = indoc! {r#"
/// Hello world!
type Sample = String;
"#};
let expect = indoc! {r#"
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
/// Hello world!
#[allow(dead_code, clippy::module_name_repetitions, clippy::option_option)]
pub type Sample = String;
"#};

parse(input, expect);
}

#[test]
fn basic_const() {
let input = indoc! {r#"
/// A bool.
const BOOL: bool = true;
/// An integer.
const INT: u32 = 100;
/// A float.
const FLOAT: f64 = 5.0;
/// A string.
const STRING: string = "hello";
/// Some bytes.
const BYTES: bytes = [1, 2, 3];
"#};
let expect = indoc! {r#"
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
/// A bool.
#[allow(dead_code)]
pub const BOOL: bool = true;
/// An integer.
#[allow(dead_code)]
pub const INT: u32 = 100;
/// A float.
#[allow(dead_code)]
pub const FLOAT: f64 = 5.0;
/// A string.
#[allow(dead_code)]
pub const STRING: &str = "hello";
/// Some bytes.
#[allow(dead_code)]
pub const BYTES: &[u8] = b"\x01\x02\x03";
"#};

parse(input, expect);
}

#[test]
fn basic_import() {
let input = indoc! {r#"
use other::module;
use other::module::Type;
"#};
let expect = indoc! {r#"
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
use other::module;
use other::module::Type;
"#};

parse(input, expect);
}
}
12 changes: 12 additions & 0 deletions crates/stef-build/tests/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ fn compile_schema() {
assert_snapshot!("compile", format!("{value}"), input.trim());
});
}

#[test]
fn compile_schema_extra() {
glob!("inputs-extra/*.stef", |path| {
let input = fs::read_to_string(path).unwrap();
let value = Schema::parse(input.as_str()).unwrap();
let value = stef_build::compile_schema(&value);
let value = prettyplease::unparse(&syn::parse2(value.clone()).unwrap());

assert_snapshot!("compile-extra", format!("{value}"), input.trim());
});
}
2 changes: 2 additions & 0 deletions crates/stef-build/tests/inputs-extra/alias.stef
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Hello world!
type Sample = String;
Loading

0 comments on commit 0f69ed5

Please sign in to comment.