diff options
author | Kyle Criddle <criddle.kyle@gmail.com> | 2020-03-24 19:08:43 -0600 |
---|---|---|
committer | Kyle Criddle <criddle.kyle@gmail.com> | 2020-03-24 19:08:43 -0600 |
commit | 38178fedf463cb704509b772d5e274461086d368 (patch) | |
tree | 287e8fc25a94da48f5147d788d313d0e5a644dfd /tests | |
parent | 59f2e2d58d3dc8ca31b74b9bad5899c4521baf0c (diff) | |
parent | 8d3136eb6f94a1a4dffd9cf6a88d083c693d1616 (diff) |
Merge branch 'master' into fix_654_stdin_filename
Diffstat (limited to 'tests')
-rw-r--r-- | tests/no_duplicate_extensions.rs | 35 | ||||
-rw-r--r-- | tests/syntax_detection.rs | 78 |
2 files changed, 35 insertions, 78 deletions
diff --git a/tests/no_duplicate_extensions.rs b/tests/no_duplicate_extensions.rs new file mode 100644 index 00000000..20dfc349 --- /dev/null +++ b/tests/no_duplicate_extensions.rs @@ -0,0 +1,35 @@ +use std::collections::HashSet; + +use bat::HighlightingAssets; + +#[test] +fn no_duplicate_extensions() { + const KNOWN_EXCEPTIONS: &[&'static str] = &[ + // The '.h' extension currently appears in multiple syntaxes: C, C++, Objective C, + // Objective C++ + "h", + // In addition to the standard Haskell syntax in 'Packages', we also ship the 'Cabal' + // syntax which comes with a "Haskell (improved)" syntax. + "hs", + // In addition to the standard JavaScript syntax in 'Packages', we also ship the + // 'Javascript (Babel)' syntax. + "js", + // The "Ruby Haml" syntax also comes with a '.sass' extension. However, we make sure + // that 'sass' is mapped to the 'Sass' syntax. + "sass", + ]; + + let assets = HighlightingAssets::from_binary(); + + let mut extensions = HashSet::new(); + + for syntax in assets.syntaxes() { + for extension in &syntax.file_extensions { + assert!( + KNOWN_EXCEPTIONS.contains(&extension.as_str()) || extensions.insert(extension), + "File extension / pattern \"{}\" appears twice in the syntax set", + extension + ); + } + } +} diff --git a/tests/syntax_detection.rs b/tests/syntax_detection.rs deleted file mode 100644 index 23a52de9..00000000 --- a/tests/syntax_detection.rs +++ /dev/null @@ -1,78 +0,0 @@ -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++"); -} |