trevors_utilities/tests/poc/data_to_text.rs
Trevor Merritt e3b8939082 config.toml: remove excluded files for coverage report
data_to_text.rs: change from . as unrecognized character to ' '
demo.rs: improves POC display of headers
tests/primitive_formatting.rs: initial tests of bool formatting
2026-04-14 07:43:20 -04:00

74 lines
2.2 KiB
Rust

use trevors_utilities::data_to_text::DataToText;
fn read_bin(source: &str) -> Vec<u8> {
let full_path = format!(
"{}/resources/data_to_text/{}.bin",
std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string(),
source
);
// println!("FULL PATH BIN: [{}]", full_path);
std::fs::read(full_path).unwrap()
}
fn read_display(source: &str) -> String {
let full_path = format!(
"{}/resources/data_to_text/{}.display",
std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string(),
source
);
std::fs::read_to_string(full_path).unwrap()
}
#[test]
fn data_to_text() {
let data_to_display: &[u8] = &[
0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
0x20, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x20,
];
let expected_data: &str = "0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x20 fghijklmnopqrst \n0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x20 vwxyz{|}~ \n";
let actual_data = DataToText::data_to_text(data_to_display);
assert_eq!(actual_data, expected_data);
}
#[test]
fn quickbrownfoxtest() {
let test_params = vec!["quickbrownfox", "q"];
for stub in test_params {
// load the content from quickbrownfox.bin
let bin_data = read_bin(stub);
// format it for display
let formatted = DataToText::data_to_text(&bin_data);
// load the content from quickbrownfox.display
let expected = read_display(stub);
assert_eq!(formatted, expected);
}
}
#[test]
fn data_to_text_window_all() {
let bin_data = read_bin("quickbrownfox");
let formatted = DataToText::data_to_text_window(&bin_data, 0, bin_data.len());
let expected = read_display("quickbrownfox");
assert_eq!(formatted, expected);
}
#[test]
fn test_window_past_end_of_data() {
let bin_data = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06];
let formatted = DataToText::data_to_text_window(&bin_data, 10, 10);
assert_eq!(formatted, "");
}