Skip to content

Commit

Permalink
test: enable more snapshot tests and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Sep 13, 2023
1 parent a6d3d4b commit 85938a4
Show file tree
Hide file tree
Showing 24 changed files with 1,075 additions and 16 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ All notable changes to this project will be documented in this file.

- Implement encoding in the Rust codegen ([2787b51](https://github.com/dnaka91/wazzup/commit/2787b51c04803311bf5ca3160b37e7db31d5a8ea))
- Simplify encoding logic ([e8205bc](https://github.com/dnaka91/wazzup/commit/e8205bcb6749fce6dd1f56ede38128076820bffd))
- Implement basic decoding logic ([f67c572](https://github.com/dnaka91/wazzup/commit/f67c57220ac2c57961bf54f7f47bca467d3fb20b))

### <!-- 1 -->🐛 Bug Fixes

- Don't double wrap optional types in decode ([a6d3d4b](https://github.com/dnaka91/wazzup/commit/a6d3d4bde28d28acb0afba123949ed7e5cbfeb98))

### <!-- 2 -->📚 Documentation

Expand All @@ -21,6 +26,11 @@ All notable changes to this project will be documented in this file.

- Generate definitions and impls together ([b32bcfd](https://github.com/dnaka91/wazzup/commit/b32bcfd8630bc445421ce32b784de6601659aade))

### <!-- 6 -->🧪 Testing

- Add snapshot tests to stef-build ([1313fe9](https://github.com/dnaka91/wazzup/commit/1313fe9f99cceee8a883791c99e318768e27f801))
- Enable more snapshot tests and fix errors ([8ac6f73](https://github.com/dnaka91/wazzup/commit/8ac6f7369a3aa95b0708e9ad02b2e5ff3f496280))

### <!-- 7 -->⚙️ Miscellaneous Tasks

- Initial commit ([5eb2f2b](https://github.com/dnaka91/wazzup/commit/5eb2f2b9687146363974ea645de22a8441e890a1))
Expand Down
26 changes: 21 additions & 5 deletions crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use stef_parser::{DataType, Enum, Fields, NamedField, Struct, UnnamedField, Variant};
use stef_parser::{DataType, Enum, Fields, Generics, NamedField, Struct, UnnamedField, Variant};

pub fn compile_struct(
Struct {
comment: _,
attributes: _,
name,
generics: _,
generics,
fields,
}: &Struct<'_>,
) -> TokenStream {
let name = Ident::new(name, Span::call_site());
let (generics, generics_where) = compile_generics(generics);
let field_vars = compile_field_vars(fields);
let field_matches = compile_field_matches(fields);
let field_assigns = compile_field_assigns(fields);

quote! {
impl ::stef::Decode for #name {
impl #generics ::stef::Decode for #name #generics #generics_where {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
#field_vars

Expand All @@ -40,15 +41,16 @@ pub fn compile_enum(
comment: _,
attributes: _,
name,
generics: _,
generics,
variants,
}: &Enum<'_>,
) -> TokenStream {
let name = Ident::new(name, Span::call_site());
let (generics, generics_where) = compile_generics(generics);
let variants = variants.iter().map(compile_variant);

quote! {
impl ::stef::Decode for #name {
impl #generics ::stef::Decode for #name #generics #generics_where {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
match ::stef::buf::decode_id(r)? {
#(#variants,)*
Expand Down Expand Up @@ -195,6 +197,20 @@ fn compile_field_assigns(fields: &Fields<'_>) -> TokenStream {
}
}

fn compile_generics(Generics(types): &Generics<'_>) -> (TokenStream, TokenStream) {
(!types.is_empty())
.then(|| {
let types = types.iter().map(|ty| Ident::new(ty, Span::call_site()));
let types2 = types.clone();

(
quote! { <#(#types,)*> },
quote! { where #(#types2: ::stef::buf::Decode,)* },
)
})
.unwrap_or_default()
}

#[allow(clippy::needless_pass_by_value)]
fn compile_data_type(ty: &DataType<'_>) -> TokenStream {
match ty {
Expand Down
17 changes: 13 additions & 4 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,13 @@ fn compile_comment(Comment(lines): &Comment<'_>) -> TokenStream {
}

fn compile_generics(Generics(types): &Generics<'_>) -> Option<TokenStream> {
(!types.is_empty()).then(|| quote! { <#(#types,)*> })
(!types.is_empty()).then(|| {
let types = types.iter().map(|ty| Ident::new(ty, Span::call_site()));
quote! { <#(#types,)*> }
})
}

fn compile_fields(fields: &Fields<'_>, public: bool) -> TokenStream {
fn compile_fields(fields: &Fields<'_>, for_struct: bool) -> TokenStream {
match fields {
Fields::Named(named) => {
let fields = named.iter().map(
Expand All @@ -200,7 +203,7 @@ fn compile_fields(fields: &Fields<'_>, public: bool) -> TokenStream {
id: _,
}| {
let comment = compile_comment(comment);
let public = public.then(|| quote! { pub });
let public = for_struct.then(|| quote! { pub });
let name = Ident::new(name, Span::call_site());
let ty = compile_data_type(ty);
quote! {
Expand All @@ -222,7 +225,13 @@ fn compile_fields(fields: &Fields<'_>, public: bool) -> TokenStream {

quote! { (#(#fields,)*) }
}
Fields::Unit => quote! {},
Fields::Unit => {
if for_struct {
quote! { ; }
} else {
quote! {}
}
}
}
}

Expand Down
26 changes: 21 additions & 5 deletions crates/stef-build/src/encode.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};
use stef_parser::{DataType, Enum, Fields, NamedField, Struct, UnnamedField, Variant};
use stef_parser::{DataType, Enum, Fields, Generics, NamedField, Struct, UnnamedField, Variant};

pub fn compile_struct(
Struct {
comment: _,
attributes: _,
name,
generics: _,
generics,
fields,
}: &Struct<'_>,
) -> TokenStream {
let name = Ident::new(name, Span::call_site());
let (generics, generics_where) = compile_generics(generics);
let fields = compile_struct_fields(fields);

quote! {
impl ::stef::Encode for #name {
impl #generics ::stef::Encode for #name #generics #generics_where {
fn encode(&self, w: &mut impl ::stef::BufMut) {
#fields
}
Expand Down Expand Up @@ -66,15 +67,16 @@ pub fn compile_enum(
comment: _,
attributes: _,
name,
generics: _,
generics,
variants,
}: &Enum<'_>,
) -> TokenStream {
let name = Ident::new(name, Span::call_site());
let (generics, generics_where) = compile_generics(generics);
let variants = variants.iter().map(compile_variant);

quote! {
impl ::stef::Encode for #name {
impl #generics ::stef::Encode for #name #generics #generics_where {
fn encode(&self, w: &mut impl ::stef::BufMut) {
match self {
#(#variants,)*
Expand Down Expand Up @@ -169,6 +171,20 @@ fn compile_variant_fields(fields: &Fields<'_>) -> TokenStream {
}
}

fn compile_generics(Generics(types): &Generics<'_>) -> (TokenStream, TokenStream) {
(!types.is_empty())
.then(|| {
let types = types.iter().map(|ty| Ident::new(ty, Span::call_site()));
let types2 = types.clone();

(
quote! { <#(#types,)*> },
quote! { where #(#types2: ::stef::buf::Encode,)* },
)
})
.unwrap_or_default()
}

#[allow(clippy::needless_pass_by_value)]
fn compile_data_type(ty: &DataType<'_>, name: TokenStream) -> TokenStream {
match ty {
Expand Down
4 changes: 2 additions & 2 deletions crates/stef-build/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use stef_parser::Schema;

#[test]
fn compile_schema() {
glob!("inputs/types-*.stef", |path| {
glob!("inputs/*.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).unwrap());
let value = prettyplease::unparse(&syn::parse2(value.clone()).unwrap());

assert_snapshot!("compile", format!("{value}"), input.trim());
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/stef-build/tests/parser.rs
expression: "/// Sample type alias.\ntype Sample = u32;"
input_file: crates/stef-parser/tests/inputs/alias-basic.stef
---
/// Sample type alias.
pub type Sample = u32;

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/stef-build/tests/parser.rs
expression: "#[validate(min = 1, max = 100)]\nstruct Sample"
input_file: crates/stef-parser/tests/inputs/attribute-multi.stef
---
pub struct Sample;
impl ::stef::Encode for Sample {
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/stef-build/tests/parser.rs
expression: "#[deprecated = \"don't use\"]\nstruct Sample"
input_file: crates/stef-parser/tests/inputs/attribute-single.stef
---
pub struct Sample;
impl ::stef::Encode for Sample {
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/stef-build/tests/parser.rs
expression: "#[deprecated]\nstruct Sample"
input_file: crates/stef-parser/tests/inputs/attribute-unit.stef
---
pub struct Sample;
impl ::stef::Encode for Sample {
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/stef-build/tests/parser.rs
expression: "#[deprecated=\"don't use\",compress]\n#[validate(in_range(min=100,max=200),non_empty)]\nstruct Sample"
input_file: crates/stef-parser/tests/inputs/attributes-min-ws.stef
---
pub struct Sample;
impl ::stef::Encode for Sample {
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/stef-build/tests/parser.rs
expression: "#[deprecated = \"don't use\", compress]\n#[validate(\n in_range(min = 100, max = 200),\n non_empty,\n)]\nstruct Sample"
input_file: crates/stef-parser/tests/inputs/attributes.stef
---
pub struct Sample;
impl ::stef::Encode for Sample {
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/stef-build/tests/parser.rs
expression: "const BOOL_TRUE: bool = true;\nconst BOOL_FALSE: bool = false;\nconst INT: u32 = 100;\nconst FLOAT: f64 = 5.5;\nconst STRING: string = \"value\";\nconst BYTES: bytes = [1, 2, 3];"
input_file: crates/stef-parser/tests/inputs/const-basic.stef
---
const BOOL_TRUE: bool = true;
const BOOL_FALSE: bool = false;
const INT: u32 = 100;
const FLOAT: f64 = 5.5;
const STRING: &str = "value";
const BYTES: &[u8] = b"\x01\x02\x03";

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: crates/stef-build/tests/parser.rs
expression: "const SIMPLE: string = \"value\";\n\nconst NEWLINE_ESCAPE: string = \"one \\\n two \\\n three\\\n\";\n\nconst ESCAPES: string = \"escape basics \\r\\n \\t \\b \\f \\\\ \\\"\\\n hello\\\" \\n\\\n unicode \\u{2764} \\\n emoji ❤ \\\n\";\n\nconst MULTILINE: string = \"a\n b\n c\n\";"
input_file: crates/stef-parser/tests/inputs/const-string.stef
---
const SIMPLE: &str = "value";
const NEWLINE_ESCAPE: &str = "one two three";
const ESCAPES: &str = "escape basics \r\n \t \u{8} \u{c} \\ \"hello\" \nunicode ❤ emoji ❤ ";
const MULTILINE: &str = "a\n b\n c\n";

Loading

0 comments on commit 85938a4

Please sign in to comment.