#![allow(dead_code)] use difference::Changeset; use std::{fmt, io}; use std::fs::File; use std::path::{Path, PathBuf}; use std::io::prelude::*; /// Loads the file at the given location and returns its contents as string. fn load_file>(path: P) -> io::Result { let mut file = File::open(path)?; let mut buffer = String::new(); file.read_to_string(&mut buffer)?; Ok(buffer) } /// Saves the given string into the specified file path. fn save_file, S: AsRef>(path: P, contents: S) -> io::Result<()> { let mut file = File::create(path)?; file.write_all(contents.as_ref().as_bytes())?; Ok(()) } /// Resolves the full path to a fixture file. pub fn fixture_path>(file_name: S) -> PathBuf { Path::new("tests").join("fixtures").join(file_name.as_ref()) } /// Assets that the given object matches the snapshot saved in the snapshot /// file. The object is serialized using the Debug trait. /// /// If the value differs from the snapshot, the assertion fails and prints /// a colored diff output. pub fn assert_snapshot, T: fmt::Debug>(snapshot_name: S, val: &T) { assert_snapshot_plain(snapshot_name, &format!("{:#?}", val)); } /// Assets that the given string matches the snapshot saved in the snapshot /// file. The given string will be used as plain output and directly compared /// with the stored snapshot. /// /// If the value differs from the snapshot, the assertion fails and prints /// a colored diff output. pub fn assert_snapshot_plain>(snapshot_name: S, output: &str) { let name = snapshot_name.as_ref(); let output_path = Path::new("tests").join("outputs").join(name); save_file(output_path, &output).unwrap_or_default(); let snapshot_path = Path::new("tests").join("snapshots").join(name); let snapshot = load_file(snapshot_path).unwrap_or("".into()); assert!( snapshot == output, "Value does not match stored snapshot {}:\n\n{}", name, Changeset::new(&snapshot, &output, "\n") ); } /// Loads the fixture file with the given name and returns its contents /// as String pub fn load_fixture>(file_name: S) -> io::Result { load_file(fixture_path(file_name)) }