on master help sign in
Add syntax detection unit tests
commit1bc62cd7b5181d68f4bbc8acdb04d464d4a5f552 authorsharkdp <davidpeter@web.de> dateTue 10 Mar 2020, 23:05 (7 years ago) parentb1183f7
2 files changed+78−1
Cargo.lock−1 tests/syntax_detection.rs+78
Cargo.lock−1
@@ -124,7 +124,6 @@ dependencies = [
124124 "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
125125 "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
126126 "error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)",
127− "escargot 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
128127 "git2 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
129128 "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
130129 "liquid 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
tests/syntax_detection.rsnew file+78
@@ -0,0 +1,78 @@
1+use std::ffi::OsStr;
2+use std::fs::File;
3+use std::io;
4+use std::io::Write;
5+
6+use tempdir::TempDir;
7+
8+use bat::assets::HighlightingAssets;
9+use bat::inputfile::InputFile;
10+use bat::syntax_mapping::SyntaxMapping;
11+
12+struct SyntaxDetectionTest {
13+ assets: HighlightingAssets,
14+ pub syntax_mapping: SyntaxMapping,
15+ temp_dir: TempDir,
16+}
17+
18+impl SyntaxDetectionTest {
19+ fn new() -> Self {
20+ SyntaxDetectionTest {
21+ assets: HighlightingAssets::new(),
22+ syntax_mapping: SyntaxMapping::new(),
23+ temp_dir: TempDir::new("bat_syntax_detection_tests")
24+ .expect("creation of temporary directory"),
25+ }
26+ }
27+
28+ fn syntax_name_with_content(&self, file_name: &str, first_line: &str) -> String {
29+ let file_path = self.temp_dir.path().join(file_name);
30+ {
31+ let mut temp_file = File::create(&file_path).unwrap();
32+ writeln!(temp_file, "{}", first_line).unwrap();
33+ }
34+
35+ let input_file = InputFile::Ordinary(OsStr::new(&file_path));
36+ let syntax = self.assets.get_syntax(
37+ None,
38+ input_file,
39+ &mut input_file.get_reader(&io::stdin()).unwrap(),
40+ &self.syntax_mapping,
41+ );
42+
43+ syntax.name.clone()
44+ }
45+
46+ fn syntax_name(&self, file_name: &str) -> String {
47+ self.syntax_name_with_content(file_name, "")
48+ }
49+}
50+
51+#[test]
52+fn syntax_detection_basic() {
53+ let test = SyntaxDetectionTest::new();
54+
55+ assert_eq!(test.syntax_name("test.rs"), "Rust");
56+ assert_eq!(test.syntax_name("test.cpp"), "C++");
57+ assert_eq!(test.syntax_name("PKGBUILD"), "Bourne Again Shell (bash)");
58+}
59+
60+#[test]
61+fn syntax_detection_first_line() {
62+ let test = SyntaxDetectionTest::new();
63+
64+ assert_eq!(
65+ test.syntax_name_with_content("my_script", "#!/bin/bash"),
66+ "Bourne Again Shell (bash)"
67+ );
68+ assert_eq!(test.syntax_name_with_content("my_script", "<?php"), "PHP");
69+}
70+
71+#[test]
72+fn syntax_detection_with_custom_mapping() {
73+ let mut test = SyntaxDetectionTest::new();
74+
75+ assert_ne!(test.syntax_name("test.h"), "C++");
76+ test.syntax_mapping.insert("h", "cpp");
77+ assert_eq!(test.syntax_name("test.h"), "C++");
78+}