Skip to content

Commit 972882d

Browse files
committed
Tests for types::fmt module
1 parent b9354a7 commit 972882d

3 files changed

Lines changed: 178 additions & 13 deletions

File tree

impl/src/types/fmt/input.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,95 @@ impl ToTokens for EnumVariantFormatInput {
121121
});
122122
}
123123
}
124+
125+
#[cfg(test)]
126+
#[allow(clippy::expect_used)]
127+
mod tests {
128+
use super::*;
129+
130+
use quote::quote;
131+
132+
#[test]
133+
fn struct_format_input_requires_initial_lit_str() {
134+
let empty_stream_res = syn::parse2::<StructFormatInput>(quote! {});
135+
let err = empty_stream_res.expect_err(
136+
"empty stream was parsed successfully as StructFormatInput",
137+
);
138+
assert_eq!(
139+
err.to_string(),
140+
"unexpected end of input, expected string literal"
141+
);
142+
}
143+
144+
#[test]
145+
fn struct_format_input_requires_initial_arg_to_be_lit_str() {
146+
let empty_stream_res =
147+
syn::parse2::<StructFormatInput>(quote! { true });
148+
let err = empty_stream_res.expect_err(
149+
"stream `true` was parsed successfully as StructFormatInput",
150+
);
151+
assert_eq!(err.to_string(), "expected string literal");
152+
}
153+
154+
#[test]
155+
fn struct_format_input_rejects_unexpected_token_after_lit_str() {
156+
let empty_stream_res =
157+
syn::parse2::<StructFormatInput>(quote! { "format string" 5 });
158+
let err = empty_stream_res.expect_err(
159+
"stream `\"format string\" 5` was parsed successfully as StructFormatInput",
160+
);
161+
assert_eq!(err.to_string(), "unexpected token after string literal");
162+
}
163+
164+
#[test]
165+
fn struct_format_input_parses_lit_str_with_trailing_comma() {
166+
let empty_stream_res =
167+
syn::parse2::<StructFormatInput>(quote! { "format string", });
168+
let format_input = empty_stream_res.expect(
169+
"stream `\"format string\",` could not be parsed as StructFormatInput",
170+
);
171+
assert_eq!(format_input.lit_str.value(), "format string");
172+
}
173+
174+
#[test]
175+
fn enum_variant_format_input_requires_initial_lit_str() {
176+
let empty_stream_res = syn::parse2::<EnumVariantFormatInput>(quote! {});
177+
let err = empty_stream_res.expect_err(
178+
"empty stream was parsed successfully as EnumVariantFormatInput",
179+
);
180+
assert_eq!(
181+
err.to_string(),
182+
"unexpected end of input, expected string literal"
183+
);
184+
}
185+
186+
#[test]
187+
fn enum_variant_format_input_requires_initial_arg_to_be_lit_str() {
188+
let empty_stream_res =
189+
syn::parse2::<EnumVariantFormatInput>(quote! { true });
190+
let err = empty_stream_res.expect_err(
191+
"stream `true` was parsed successfully as EnumVariantFormatInput",
192+
);
193+
assert_eq!(err.to_string(), "expected string literal");
194+
}
195+
196+
#[test]
197+
fn enum_variant_format_input_rejects_unexpected_token_after_lit_str() {
198+
let empty_stream_res =
199+
syn::parse2::<EnumVariantFormatInput>(quote! { "format string" 5 });
200+
let err = empty_stream_res.expect_err(
201+
"stream `\"format string\" 5` was parsed successfully as EnumVariantFormatInput",
202+
);
203+
assert_eq!(err.to_string(), "unexpected token after string literal");
204+
}
205+
206+
#[test]
207+
fn enum_variant_format_input_parses_lit_str_with_trailing_comma() {
208+
let empty_stream_res =
209+
syn::parse2::<EnumVariantFormatInput>(quote! { "format string", });
210+
let format_input = empty_stream_res.expect(
211+
"stream `\"format string\",` could not be parsed as EnumVariantFormatInput",
212+
);
213+
assert_eq!(format_input.lit_str.value(), "format string");
214+
}
215+
}

impl/src/types/fmt/mod.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,89 @@ impl ToTokens for FormatData {
202202
}
203203
}
204204
}
205+
206+
#[cfg(test)]
207+
#[allow(clippy::expect_used)]
208+
mod tests {
209+
use super::*;
210+
211+
use quote::quote;
212+
213+
#[test]
214+
fn struct_format_data_requires_display_attr() {
215+
let derive_input =
216+
syn::parse2::<DeriveInput>(quote! { struct CustomType; })
217+
.expect("malformed test stream");
218+
let err = FormatData::new(&derive_input).expect_err(
219+
"stream without display attr was parsed successfully as FormatData",
220+
);
221+
assert_eq!(
222+
err.to_string(),
223+
"missing `display` attribute for struct with `#[derive(Error)]`"
224+
);
225+
}
226+
227+
#[test]
228+
fn struct_format_data_requires_list_form_for_display_attr() {
229+
let derive_input = syn::parse2::<DeriveInput>(
230+
quote! { #[display] struct CustomType; },
231+
)
232+
.expect("malformed test stream");
233+
let err = FormatData::new(&derive_input).expect_err(
234+
"stream with path display attr was parsed successfully as FormatData",
235+
);
236+
assert_eq!(
237+
err.to_string(),
238+
"expected `display` to be a list attribute: `#[display(\"template...\")]`"
239+
);
240+
}
241+
242+
#[test]
243+
fn enum_format_data_requires_display_attr() {
244+
let derive_input =
245+
syn::parse2::<DeriveInput>(quote! { enum CustomType { One, Two } })
246+
.expect("malformed test stream");
247+
let err = FormatData::new(&derive_input).expect_err(
248+
"stream without display attr was parsed successfully as FormatData",
249+
);
250+
assert_eq!(
251+
err.to_string(),
252+
"missing `display` attribute for enum with `#[derive(Error)]`\nadd a `display` attribute to at least the whole enum or to all of its variants"
253+
);
254+
}
255+
256+
#[test]
257+
fn enum_format_data_requires_list_form_for_display_attr() {
258+
let derive_input = syn::parse2::<DeriveInput>(
259+
quote! { #[display] enum CustomType { One, Two } },
260+
)
261+
.expect("malformed test stream");
262+
let err = FormatData::new(&derive_input).expect_err(
263+
"stream with path display attr was parsed successfully as FormatData",
264+
);
265+
assert_eq!(
266+
err.to_string(),
267+
"expected `display` to be a list attribute: `#[display(\"template...\")]`"
268+
);
269+
}
270+
271+
#[test]
272+
fn enum_format_data_requires_list_form_for_display_attr_on_every_variant() {
273+
let derive_input = syn::parse2::<DeriveInput>(quote! {
274+
enum CustomType {
275+
#[display]
276+
One,
277+
#[display]
278+
Two
279+
}
280+
})
281+
.expect("malformed test stream");
282+
let err = FormatData::new(&derive_input).expect_err(
283+
"stream with path display attr was parsed successfully as FormatData",
284+
);
285+
assert_eq!(
286+
err.to_string(),
287+
"expected `display` to be a list attribute: `#[display(\"template...\")]`"
288+
);
289+
}
290+
}

tests/src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
pub fn add(left: u64, right: u64) -> u64 {
2-
left + right
3-
}
41

5-
#[cfg(test)]
6-
mod tests {
7-
use super::*;
8-
9-
#[test]
10-
fn it_works() {
11-
let result = add(2, 2);
12-
assert_eq!(result, 4);
13-
}
14-
}

0 commit comments

Comments
 (0)