summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2020-03-10 23:05:57 +0100
committerDavid Peter <sharkdp@users.noreply.github.com>2020-03-11 10:08:11 +0100
commit1bc62cd7b5181d68f4bbc8acdb04d464d4a5f552 (patch)
treeada0bc3e9183fc90b204383a9f7cb0723df01bb4 /tests
parentb1183f72a5d31777a1865f6411b33e73c7f7e1cc (diff)
Add syntax detection unit tests
Diffstat (limited to 'tests')
-rw-r--r--tests/syntax_detection.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/syntax_detection.rs b/tests/syntax_detection.rs
new file mode 100644
index 00000000..23a52de9
--- /dev/null
+++ b/tests/syntax_detection.rs
@@ -0,0 +1,78 @@
+use std::ffi::OsStr;
+use std::fs::File;
+use std::io;
+use std::io::Write;
+
+use tempdir::TempDir;
+
+use bat::assets::HighlightingAssets;
+use bat::inputfile::InputFile;
+use bat::syntax_mapping::SyntaxMapping;
+
+struct SyntaxDetectionTest {
+ assets: HighlightingAssets,
+ pub syntax_mapping: SyntaxMapping,
+ temp_dir: TempDir,
+}
+
+impl SyntaxDetectionTest {
+ fn new() -> Self {
+ SyntaxDetectionTest {
+ assets: HighlightingAssets::new(),
+ syntax_mapping: SyntaxMapping::new(),
+ temp_dir: TempDir::new("bat_syntax_detection_tests")
+ .expect("creation of temporary directory"),
+ }
+ }
+
+ fn syntax_name_with_content(&self, file_name: &str, first_line: &str) -> String {
+ let file_path = self.temp_dir.path().join(file_name);
+ {
+ let mut temp_file = File::create(&file_path).unwrap();
+ writeln!(temp_file, "{}", first_line).unwrap();
+ }
+
+ let input_file = InputFile::Ordinary(OsStr::new(&file_path));
+ let syntax = self.assets.get_syntax(
+ None,
+ input_file,
+ &mut input_file.get_reader(&io::stdin()).unwrap(),
+ &self.syntax_mapping,
+ );
+
+ syntax.name.clone()
+ }
+
+ fn syntax_name(&self, file_name: &str) -> String {
+ self.syntax_name_with_content(file_name, "")
+ }
+}
+
+#[test]
+fn syntax_detection_basic() {
+ let test = SyntaxDetectionTest::new();
+
+ assert_eq!(test.syntax_name("test.rs"), "Rust");
+ assert_eq!(test.syntax_name("test.cpp"), "C++");
+ assert_eq!(test.syntax_name("PKGBUILD"), "Bourne Again Shell (bash)");
+}
+
+#[test]
+fn syntax_detection_first_line() {
+ let test = SyntaxDetectionTest::new();
+
+ assert_eq!(
+ test.syntax_name_with_content("my_script", "#!/bin/bash"),
+ "Bourne Again Shell (bash)"
+ );
+ assert_eq!(test.syntax_name_with_content("my_script", "<?php"), "PHP");
+}
+
+#[test]
+fn syntax_detection_with_custom_mapping() {
+ let mut test = SyntaxDetectionTest::new();
+
+ assert_ne!(test.syntax_name("test.h"), "C++");
+ test.syntax_mapping.insert("h", "cpp");
+ assert_eq!(test.syntax_name("test.h"), "C++");
+}