diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-12-05 09:40:43 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-12-21 08:39:32 +0000 |
commit | 91b29f9bd7e34434824789d33014c916e7e13243 (patch) | |
tree | 10b1ac472742d7343c9c4cd26cd3879585b6f436 /subplotlib | |
parent | 384f1fa8a20f29f7a72f5591c36a2b41caf9822c (diff) | |
download | subplot-91b29f9bd7e34434824789d33014c916e7e13243.tar.gz |
subplotlib: Add tests so that we can test subplotlib without subplot built
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'subplotlib')
-rw-r--r-- | subplotlib/tests/subplotlib.rs | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/subplotlib/tests/subplotlib.rs b/subplotlib/tests/subplotlib.rs new file mode 100644 index 0000000..ab74670 --- /dev/null +++ b/subplotlib/tests/subplotlib.rs @@ -0,0 +1,153 @@ +use subplotlib::prelude::*; + +// -------------------------------- +// This came from helpers/subplotlib_context.rs + +use std::collections::HashMap; + +struct Context { + counter: usize, + files: HashMap<String, SubplotDataFile>, + this_file: Option<SubplotDataFile>, +} + +impl Default for Context { + fn default() -> Self { + Self { + counter: 0, + files: HashMap::new(), + this_file: None, + } + } +} + +impl Context { + fn remember_file(&mut self, name: &str, content: SubplotDataFile) { + self.files.insert(name.to_string(), content); + } +} + +// -------------------------------- +// This came from helpers/subplotlib_impl.rs + +#[step] +fn a_trivial_setup(context: &mut Context) { + context.counter = 0; +} + +#[step] +fn a_trivial_cleanup(context: &mut Context) {} + +#[step] +fn increment_counter(context: &mut Context) { + context.counter += 1; +} + +#[step] +fn check_counter(context: &mut Context, num: usize) { + if context.counter != num { + throw!(format!( + "Counter was wrong, it was {} but {} was expected", + context.counter, num + )); + } +} + +#[step] +fn acquire_file_content(context: &mut Context, somename: &str, file: SubplotDataFile) { + context.remember_file(somename, file); +} + +#[step] +fn remember_target(context: &mut Context, somename: &str) { + if let Some(file) = context.files.get(somename) { + context.this_file = Some(file.clone()); + } else { + throw!(format!("Unknown file {}", somename)); + } +} + +#[step] +fn check_contents(context: &mut Context, text: &str) { + if let Some(file) = context.this_file.as_ref() { + let body_as_text = String::from_utf8_lossy(file.data()); + if !body_as_text.as_ref().contains(text) { + throw!(format!( + "Failed to find {} when looking at {}", + text, + file.name().display() + )); + } + } else { + throw!("Not looking at a file"); + } +} + +// -------------------------------- + +lazy_static! { + static ref SUBPLOT_EMBEDDED_FILES: Vec<SubplotDataFile> = vec![SubplotDataFile::new( + "ZXhhbXBsZS50eHQ=", + "VGhpcyBkYXRhIGZpbGUgd2lsbCBiZSBlbWJlZGRlZCBpbnRvIHRoZSB0ZXN0IHN1aXRlCg==" + ),]; +} + +// --------------------------------- + +// Fundamentals +#[test] +fn scenario_1() { + let mut scenario = Scenario::new(&base64_decode("RnVuZGFtZW50YWxz")); + + let step = BUILDER_a_trivial_setup::default().build(); + let cleanup = BUILDER_a_trivial_cleanup::default().build(); + scenario.add_step(step, Some(cleanup)); + + let step = BUILDER_increment_counter::default().build(); + scenario.add_step(step, None); + + let step = BUILDER_check_counter::default().num(1).build(); + scenario.add_step(step, None); + + let step = BUILDER_increment_counter::default().build(); + scenario.add_step(step, None); + + let step = BUILDER_check_counter::default().num(2).build(); + scenario.add_step(step, None); + + scenario.run().unwrap(); +} + +// --------------------------------- + +// Embedded files +#[test] +fn scenario_2() { + let mut scenario = Scenario::new(&base64_decode("RW1iZWRkZWQgZmlsZXM=")); + + let step = BUILDER_acquire_file_content::default() + .file({ + use std::path::PathBuf; + let target_name: PathBuf = base64_decode("ZXhhbXBsZS50eHQ=").into(); + SUBPLOT_EMBEDDED_FILES + .iter() + .find(|df| df.name() == target_name) + .expect("Unable to find file at runtime") + .clone() + }) + .somename(&base64_decode("RVhBTVBMRQ==")) + .build(); + scenario.add_step(step, None); + + let step = BUILDER_remember_target::default() + .somename(&base64_decode("RVhBTVBMRQ==")) + .build(); + scenario.add_step(step, None); + + let step = BUILDER_check_contents::default() + .text(&base64_decode("d2lsbCBiZSBlbWJlZGRlZA==")) + .build(); + scenario.add_step(step, None); + + scenario.run().unwrap(); +} |