Skip to content

Commit

Permalink
test: always enable syn support in test
Browse files Browse the repository at this point in the history
  • Loading branch information
drahnr committed Feb 24, 2022
1 parent a11ac06 commit cd79865
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Expander {
self
}

#[cfg(feature = "syndicate")]
#[cfg(any(feature = "syndicate", test))]
/// Create a file with `filename` under `env!("OUT_DIR")` if it's not an `Err(_)`.
pub fn maybe_write_to_out_dir(
self,
Expand All @@ -119,7 +119,7 @@ impl Expander {
self.write_to(tokens, out.as_path())
}

#[cfg(feature = "syndicate")]
#[cfg(any(feature = "syndicate", test))]
/// Create a file with `filename` at `dest` if it's not an `Err(_)`.
pub fn maybe_write_to(
self,
Expand Down
67 changes: 31 additions & 36 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use proc_macro2::Span;

#[test]
fn dry() -> Result<(), std::io::Error> {
Expand Down Expand Up @@ -40,43 +41,37 @@ fn basic() -> Result<(), std::io::Error> {
Ok(())
}

#[cfg(feature = "syndicate")]
mod syndicate {
use super::*;
use proc_macro2::Span;

#[test]
fn ok_is_written_to_external_file() -> Result<(), std::io::Error> {
let ts = Ok(quote! {
pub struct X {
x: [u8;32],
}
});
let modified = Expander::new("bar")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
// .dry(false)
.maybe_write_to_out_dir(ts.clone())?;
#[test]
fn syn_ok_is_written_to_external_file() -> Result<(), std::io::Error> {
let ts = Ok(quote! {
pub struct X {
x: [u8;32],
}
});
let modified = Expander::new("bar")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
// .dry(false)
.maybe_write_to_out_dir(ts.clone())?;

let s = modified.to_string();
assert_ne!(s, ts.unwrap().to_string());
assert!(s.contains("include ! ("));
Ok(())
}
let s = modified.to_string();
assert_ne!(s, ts.unwrap().to_string());
assert!(s.contains("include ! ("));
Ok(())
}

#[test]
fn errors_are_not_written_to_external_file() -> Result<(), std::io::Error> {
let ts = Err(syn::Error::new(Span::call_site(), "Hajajajaiii!"));
let modified = Expander::new("")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
// .dry(false)
.maybe_write_to_out_dir(ts.clone())?;
#[test]
fn syn_error_is_not_written_to_external_file() -> Result<(), std::io::Error> {
let ts = Err(syn::Error::new(Span::call_site(), "Hajajajaiii!"));
let modified = Expander::new("")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
// .dry(false)
.maybe_write_to_out_dir(ts.clone())?;

assert_eq!(
ts.unwrap_err().to_compile_error().to_string(),
modified.to_string()
);
Ok(())
}
assert_eq!(
ts.unwrap_err().to_compile_error().to_string(),
modified.to_string()
);
Ok(())
}

0 comments on commit cd79865

Please sign in to comment.