From 91593417141c49b6f2a0755f989e7a3fb9bae310 Mon Sep 17 00:00:00 2001 From: eth-p <32112321+eth-p@users.noreply.github.com> Date: Mon, 10 Sep 2018 19:19:43 -0700 Subject: [PATCH] Modified snapshot tests to support tab expansion. This changes how the files are named (to allow for snapshots that aren't directly related to the --style argument) and fixes the generate_snapshots.py script to work with the latest version of bat. Three new tests are also introduced: - tabs_4 - Tab expansion with a width of 4. - tabs_8 - Tab expansion with a width of 8. - tabs_passthrough - No tab expansion. --- tests/snapshots/generate_snapshots.py | 20 +++++++++---- tests/snapshots/sample.modified.rs | 16 ++++++++++- tests/snapshots/sample.rs | 14 +++++++++ tests/tester.rs | 5 ++-- tests/tests.rs | 41 ++++++++++++++------------- 5 files changed, 68 insertions(+), 28 deletions(-) diff --git a/tests/snapshots/generate_snapshots.py b/tests/snapshots/generate_snapshots.py index 6d25f0f1..ea130a4f 100755 --- a/tests/snapshots/generate_snapshots.py +++ b/tests/snapshots/generate_snapshots.py @@ -10,16 +10,24 @@ def generate_snapshots(): for num in range(len(single_styles)): for grouped in itertools.combinations(single_styles, num + 1): - generate_snapshot(",".join(grouped)) + generate_style_snapshot(",".join(grouped)) for style in collective_styles: - generate_snapshot(style) + generate_style_snapshot(style) -def generate_snapshot(option): - command = "../../target/debug/bat --style={0} sample.rs > output/{0}.snapshot.txt".format( - option + generate_snapshot("tabs_passthrough", "--tabs=0 --style=full") + generate_snapshot("tabs_4", "--tabs=4 --style=full") + generate_snapshot("tabs_8", "--tabs=8 --style=full") + +def generate_style_snapshot(style): + generate_snapshot(style.replace(",","_"), "--style={}".format(style)) + +def generate_snapshot(name, arguments): + command = "../../target/debug/bat --decorations=always {1} sample.rs > output/{0}.snapshot.txt".format( + name, + arguments ) - print("generating snapshot for {}".format(option)) + print("generating snapshot for {}".format(name)) subprocess.call(command, shell=True) def build_bat(): diff --git a/tests/snapshots/sample.modified.rs b/tests/snapshots/sample.modified.rs index bb1a7943..30cbdde6 100644 --- a/tests/snapshots/sample.modified.rs +++ b/tests/snapshots/sample.modified.rs @@ -10,7 +10,7 @@ fn main() { "The perimeter of the rectangle is {} pixels.", perimeter(&rect1) ); - println!(r#"This line contains invalid utf8: "øˆ€€€"#; + println!(r#"This line contains invalid utf8: "�����"#; } fn area(rectangle: &Rectangle) -> u32 { @@ -20,3 +20,17 @@ fn area(rectangle: &Rectangle) -> u32 { fn perimeter(rectangle: &Rectangle) -> u32 { (rectangle.width + rectangle.height) * 2 } + +// Tab alignment: +/* + Indent + 1 2 3 4 +1 ? +22 ? +333 ? +4444 ? +55555 ? +666666 ? +7777777 ? +88888888 ? +*/ diff --git a/tests/snapshots/sample.rs b/tests/snapshots/sample.rs index 00ec0eb6..6ee6489b 100644 --- a/tests/snapshots/sample.rs +++ b/tests/snapshots/sample.rs @@ -16,3 +16,17 @@ fn main() { fn area(rectangle: &Rectangle) -> u32 { rectangle.width * rectangle.height } + +// Tab alignment: +/* + Indent + 1 2 3 4 +1 ? +22 ? +333 ? +4444 ? +55555 ? +666666 ? +7777777 ? +88888888 ? +*/ \ No newline at end of file diff --git a/tests/tester.rs b/tests/tester.rs index 9885ff46..c7f46c03 100644 --- a/tests/tester.rs +++ b/tests/tester.rs @@ -38,13 +38,14 @@ impl BatTester { BatTester { temp_dir, exe } } - pub fn test_snapshot(&self, style: &str) { + pub fn test_snapshot(&self, name: &str, style: &str, tab_width: u32) { let output = Command::new(&self.exe) .current_dir(self.temp_dir.path()) .args(&[ "sample.rs", "--decorations=always", "--terminal-width=80", + &format!("--tabs={}", tab_width), &format!("--style={}", style), ]).output() .expect("bat failed"); @@ -55,7 +56,7 @@ impl BatTester { .replace("tests/snapshots/", ""); let mut expected = String::new(); - let mut file = File::open(format!("tests/snapshots/output/{}.snapshot.txt", style)) + let mut file = File::open(format!("tests/snapshots/output/{}.snapshot.txt", name)) .expect("snapshot file missing"); file.read_to_string(&mut expected) .expect("could not read snapshot file"); diff --git a/tests/tests.rs b/tests/tests.rs index d0313304..906bad4a 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -3,33 +3,36 @@ mod tester; use tester::BatTester; macro_rules! snapshot_tests { - ($($test_name: ident: $style: expr,)*) => { + ($($test_name: ident: $style: expr => [tabs: $tabs:expr],)*) => { $( #[test] fn $test_name() { let bat_tester = BatTester::new(); - bat_tester.test_snapshot($style); + bat_tester.test_snapshot(stringify!($test_name), $style, $tabs); } )* }; } snapshot_tests! { - changes: "changes", - grid: "grid", - header: "header", - numbers: "numbers", - changes_grid: "changes,grid", - changes_header: "changes,header", - changes_numbers: "changes,numbers", - grid_header: "grid,header", - grid_numbers: "grid,numbers", - header_numbers: "header,numbers", - changes_grid_header: "changes,grid,header", - changes_grid_numbers: "changes,grid,numbers", - changes_header_numbers: "changes,header,numbers", - grid_header_numbers: "grid,header,numbers", - changes_grid_header_numbers: "changes,grid,header,numbers", - full: "full", - plain: "plain", + changes: "changes" => [tabs: 8], + grid: "grid" => [tabs: 8], + header: "header" => [tabs: 8], + numbers: "numbers" => [tabs: 8], + changes_grid: "changes,grid" => [tabs: 8], + changes_header: "changes,header" => [tabs: 8], + changes_numbers: "changes,numbers" => [tabs: 8], + grid_header: "grid,header" => [tabs: 8], + grid_numbers: "grid,numbers" => [tabs: 8], + header_numbers: "header,numbers" => [tabs: 8], + changes_grid_header: "changes,grid,header" => [tabs: 8], + changes_grid_numbers: "changes,grid,numbers" => [tabs: 8], + changes_header_numbers: "changes,header,numbers" => [tabs: 8], + grid_header_numbers: "grid,header,numbers" => [tabs: 8], + changes_grid_header_numbers: "changes,grid,header,numbers" => [tabs: 8], + full: "full" => [tabs: 8], + plain: "plain" => [tabs: 8], + tabs_passthrough: "full" => [tabs: 0], + tabs_4: "full" => [tabs: 4], + tabs_8: "full" => [tabs: 8], }