build.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use std::env;
  2. use std::fs::File;
  3. use std::io::Write;
  4. use std::path::Path;
  5. const TEST_PREFIX: &str = "
  6. #[cfg(test)]
  7. use pretty_assertions::assert_eq;
  8. use crate::{grammar,lexer};
  9. use std::io::Write;
  10. // to let us use pretty_assertions with strings, we write a newtype
  11. // that delegates Debug to Display
  12. #[derive(PartialEq, Eq)]
  13. struct StringWrapper<'a> {
  14. wrapped: &'a str,
  15. }
  16. impl<'a> core::fmt::Debug for StringWrapper<'a> {
  17. fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
  18. core::fmt::Display::fmt(self.wrapped, f)
  19. }
  20. }
  21. fn assert_eq(x: &str, y: &str) {
  22. assert_eq!(StringWrapper {wrapped: x}, StringWrapper {wrapped: y});
  23. }
  24. ";
  25. const TEST_TEMPLATE: &str = "
  26. #[test]
  27. fn test_%PREFIX%() {
  28. let state = crate::interp::State::new();
  29. let source = include_str!(\"%ROOT%/tests/%PREFIX%.matzo\");
  30. let lexer = lexer::tokens(source);
  31. let stmts = grammar::StmtsParser::new().parse(&mut state.get_ast().borrow_mut(), lexer);
  32. assert!(stmts.is_ok());
  33. let stmts = stmts.unwrap();
  34. if let Ok(expected) = std::fs::read_to_string(\"%ROOT%/tests/%PREFIX%.parsed\") {
  35. let mut buf = Vec::new();
  36. for s in stmts.iter() {
  37. writeln!(buf, \"{:?}\", s.show(&state.get_ast().borrow())).unwrap();
  38. }
  39. assert_eq(
  40. std::str::from_utf8(&buf).unwrap().trim(),
  41. expected.trim(),
  42. );
  43. }
  44. if let Ok(expected) = std::fs::read_to_string(\"%ROOT%/tests/%PREFIX%.output\") {
  45. let possibilities = expected.lines().collect::<Vec<&str>>();
  46. let mut buf = Vec::new();
  47. for stmt in stmts {
  48. state.execute(&stmt, &mut buf).unwrap();
  49. }
  50. let out = std::str::from_utf8(&buf).unwrap();
  51. if !possibilities.contains(&out.trim()) {
  52. panic!(\"Got output\\n `{}`\\nbut expected one of the following:\\n{}\\n\",
  53. &out.trim(),
  54. possibilities.iter().map(|x| format!(\" `{}`\\n\", x)).collect::<Vec<String>>().join(\", \"),
  55. );
  56. }
  57. }
  58. }
  59. ";
  60. fn main() -> Result<(), Box<dyn std::error::Error>> {
  61. lalrpop::process_root()?;
  62. for file in ["build.rs", "src/grammar.lalrpop"] {
  63. println!("cargo:rerun-if-changed={}", file);
  64. }
  65. vergen::vergen(vergen::Config::default())?;
  66. let out_dir = env::var("OUT_DIR")?;
  67. let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
  68. let dest = Path::new(&out_dir).join("exp_tests.rs");
  69. let mut test_file = File::create(&dest)?;
  70. writeln!(test_file, "{}", TEST_PREFIX)?;
  71. for exp in std::fs::read_dir("tests")? {
  72. let exp = exp?.path().canonicalize()?;
  73. let fname = exp.file_name().ok_or("bad file name")?.to_string_lossy();
  74. if let Some(prefix) = fname.strip_suffix(".matzo") {
  75. let test = TEST_TEMPLATE
  76. .replace("%FILE%", &fname)
  77. .replace("%PREFIX%", prefix)
  78. .replace("%ROOT%", &manifest_dir);
  79. writeln!(test_file, "{}", test)?;
  80. }
  81. }
  82. Ok(())
  83. }