2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-18 15:26:16 +00:00

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.
This commit is contained in:
eth-p 2018-09-10 19:19:43 -07:00
parent b23ff24ebc
commit 9159341714
No known key found for this signature in database
GPG Key ID: 1F8DF8091CD46FBC
5 changed files with 68 additions and 28 deletions

View File

@ -10,16 +10,24 @@ def generate_snapshots():
for num in range(len(single_styles)): for num in range(len(single_styles)):
for grouped in itertools.combinations(single_styles, num + 1): for grouped in itertools.combinations(single_styles, num + 1):
generate_snapshot(",".join(grouped)) generate_style_snapshot(",".join(grouped))
for style in collective_styles: for style in collective_styles:
generate_snapshot(style) generate_style_snapshot(style)
def generate_snapshot(option): generate_snapshot("tabs_passthrough", "--tabs=0 --style=full")
command = "../../target/debug/bat --style={0} sample.rs > output/{0}.snapshot.txt".format( generate_snapshot("tabs_4", "--tabs=4 --style=full")
option 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) subprocess.call(command, shell=True)
def build_bat(): def build_bat():

View File

@ -10,7 +10,7 @@ fn main() {
"The perimeter of the rectangle is {} pixels.", "The perimeter of the rectangle is {} pixels.",
perimeter(&rect1) perimeter(&rect1)
); );
println!(r#"This line contains invalid utf8: "øˆ€€€"#; println!(r#"This line contains invalid utf8: "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"#;
} }
fn area(rectangle: &Rectangle) -> u32 { fn area(rectangle: &Rectangle) -> u32 {
@ -20,3 +20,17 @@ fn area(rectangle: &Rectangle) -> u32 {
fn perimeter(rectangle: &Rectangle) -> u32 { fn perimeter(rectangle: &Rectangle) -> u32 {
(rectangle.width + rectangle.height) * 2 (rectangle.width + rectangle.height) * 2
} }
// Tab alignment:
/*
Indent
1 2 3 4
1 ?
22 ?
333 ?
4444 ?
55555 ?
666666 ?
7777777 ?
88888888 ?
*/

View File

@ -16,3 +16,17 @@ fn main() {
fn area(rectangle: &Rectangle) -> u32 { fn area(rectangle: &Rectangle) -> u32 {
rectangle.width * rectangle.height rectangle.width * rectangle.height
} }
// Tab alignment:
/*
Indent
1 2 3 4
1 ?
22 ?
333 ?
4444 ?
55555 ?
666666 ?
7777777 ?
88888888 ?
*/

View File

@ -38,13 +38,14 @@ impl BatTester {
BatTester { temp_dir, exe } 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) let output = Command::new(&self.exe)
.current_dir(self.temp_dir.path()) .current_dir(self.temp_dir.path())
.args(&[ .args(&[
"sample.rs", "sample.rs",
"--decorations=always", "--decorations=always",
"--terminal-width=80", "--terminal-width=80",
&format!("--tabs={}", tab_width),
&format!("--style={}", style), &format!("--style={}", style),
]).output() ]).output()
.expect("bat failed"); .expect("bat failed");
@ -55,7 +56,7 @@ impl BatTester {
.replace("tests/snapshots/", ""); .replace("tests/snapshots/", "");
let mut expected = String::new(); 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"); .expect("snapshot file missing");
file.read_to_string(&mut expected) file.read_to_string(&mut expected)
.expect("could not read snapshot file"); .expect("could not read snapshot file");

View File

@ -3,33 +3,36 @@ mod tester;
use tester::BatTester; use tester::BatTester;
macro_rules! snapshot_tests { macro_rules! snapshot_tests {
($($test_name: ident: $style: expr,)*) => { ($($test_name: ident: $style: expr => [tabs: $tabs:expr],)*) => {
$( $(
#[test] #[test]
fn $test_name() { fn $test_name() {
let bat_tester = BatTester::new(); let bat_tester = BatTester::new();
bat_tester.test_snapshot($style); bat_tester.test_snapshot(stringify!($test_name), $style, $tabs);
} }
)* )*
}; };
} }
snapshot_tests! { snapshot_tests! {
changes: "changes", changes: "changes" => [tabs: 8],
grid: "grid", grid: "grid" => [tabs: 8],
header: "header", header: "header" => [tabs: 8],
numbers: "numbers", numbers: "numbers" => [tabs: 8],
changes_grid: "changes,grid", changes_grid: "changes,grid" => [tabs: 8],
changes_header: "changes,header", changes_header: "changes,header" => [tabs: 8],
changes_numbers: "changes,numbers", changes_numbers: "changes,numbers" => [tabs: 8],
grid_header: "grid,header", grid_header: "grid,header" => [tabs: 8],
grid_numbers: "grid,numbers", grid_numbers: "grid,numbers" => [tabs: 8],
header_numbers: "header,numbers", header_numbers: "header,numbers" => [tabs: 8],
changes_grid_header: "changes,grid,header", changes_grid_header: "changes,grid,header" => [tabs: 8],
changes_grid_numbers: "changes,grid,numbers", changes_grid_numbers: "changes,grid,numbers" => [tabs: 8],
changes_header_numbers: "changes,header,numbers", changes_header_numbers: "changes,header,numbers" => [tabs: 8],
grid_header_numbers: "grid,header,numbers", grid_header_numbers: "grid,header,numbers" => [tabs: 8],
changes_grid_header_numbers: "changes,grid,header,numbers", changes_grid_header_numbers: "changes,grid,header,numbers" => [tabs: 8],
full: "full", full: "full" => [tabs: 8],
plain: "plain", plain: "plain" => [tabs: 8],
tabs_passthrough: "full" => [tabs: 0],
tabs_4: "full" => [tabs: 4],
tabs_8: "full" => [tabs: 8],
} }