diff options
author | Ethan P <eth-p+git@hidden.email> | 2020-05-11 20:10:04 -0700 |
---|---|---|
committer | Ethan P <eth-p+git@hidden.email> | 2020-05-13 00:38:56 -0700 |
commit | 1fb669ae1abfff0712dc370133ae92af56aa439c (patch) | |
tree | f2f18fdee4cef673a742d769554ec1424bbd6602 /src/assets.rs | |
parent | 7d07aa395a90f33bb939625de8576c0fa667aef7 (diff) |
Test that OrdinaryFile consistent with CustomReader
Diffstat (limited to 'src/assets.rs')
-rw-r--r-- | src/assets.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/assets.rs b/src/assets.rs index 6cf2e2c4..364e17bc 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -280,6 +280,8 @@ mod tests { use std::ffi::OsStr; + use std::fs::File; + use std::io::Write; use tempdir::TempDir; use crate::input::Input; @@ -300,6 +302,27 @@ mod tests { } } + fn syntax_for_real_file_with_content_os( + &self, + file_name: &OsStr, + 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 = Input::ordinary_file(file_path.as_os_str()); + let dummy_stdin: &[u8] = &[]; + let mut opened_input = input.open(dummy_stdin).unwrap(); + let syntax = self + .assets + .get_syntax(None, &mut opened_input, &self.syntax_mapping); + + syntax.name.clone() + } + fn syntax_for_file_with_content_os(&self, file_name: &OsStr, first_line: &str) -> String { let file_path = self.temp_dir.path().join(file_name); let input = Input::from_reader(Box::new(BufReader::new(first_line.as_bytes()))) @@ -334,6 +357,12 @@ mod tests { .get_syntax(None, &mut opened_input, &self.syntax_mapping); syntax.name.clone() } + + fn syntax_is_same_for_file_and_reader(&self, file_name: &str, content: &str) -> bool { + let real = self.syntax_for_real_file_with_content_os(file_name.as_ref(), content); + let fake = self.syntax_for_file_with_content_os(file_name.as_ref(), content); + return real == fake; + } } #[test] @@ -365,6 +394,27 @@ mod tests { } #[test] + fn syntax_detection_same_for_file_and_string() { + let mut test = SyntaxDetectionTest::new(); + + test.syntax_mapping + .insert("*.myext", MappingTarget::MapTo("C")) + .ok(); + test.syntax_mapping + .insert("MY_FILE", MappingTarget::MapTo("Markdown")) + .ok(); + + assert!(test.syntax_is_same_for_file_and_reader("Test.md", "")); + assert!(test.syntax_is_same_for_file_and_reader("Test.txt", "#!/bin/bash")); + assert!(test.syntax_is_same_for_file_and_reader(".bashrc", "")); + assert!(test.syntax_is_same_for_file_and_reader("test.h", "")); + assert!(test.syntax_is_same_for_file_and_reader("test.js", "#!/bin/bash")); + assert!(test.syntax_is_same_for_file_and_reader("test.myext", "")); + assert!(test.syntax_is_same_for_file_and_reader("MY_FILE", "")); + assert!(test.syntax_is_same_for_file_and_reader("MY_FILE", "<?php")); + } + + #[test] fn syntax_detection_well_defined_mapping_for_duplicate_extensions() { let test = SyntaxDetectionTest::new(); |