mod common;
use std::collections::BTreeMap;
use std::path::Path;
use tabnas_multisource::{FileResolver, MapResolver, MultiSourceOptions};
use common::{make_with, to_json};
fn ts_test_dir() -> String {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("rs/ has a parent")
.join("ts")
.join("test")
.to_string_lossy()
.into_owned()
}
#[test]
fn a_js_source_is_raw_text_here() {
let parser = make_with(MultiSourceOptions::new(FileResolver::new()).with_path(ts_test_dir()));
let value = parser
.parse(r#"d:@"k02.js""#)
.expect("the file is read, just not executed");
let json = to_json(&value);
assert_eq!(
json["d"].as_str().map(str::trim),
Some("module.exports = {\n e: 3\n}"),
"the module text is the value, not the module's exports"
);
}
#[test]
fn a_js_file_is_not_found_by_an_extensionless_reference() {
let parser = make_with(MultiSourceOptions::new(FileResolver::new()).with_path(ts_test_dir()));
let error = parser
.parse(r#"d:@"k02""#)
.expect_err("nothing but a .js file is there");
assert_eq!(error.code, "multisource_not_found");
assert!(
!error.hint.lines().any(|line| line.ends_with(".js")),
"the search does not consider .js: {}",
error.hint
);
}
#[test]
fn a_chain_deeper_than_the_cap_is_an_error() {
let depth = 4_000;
let mut files: BTreeMap<String, String> = BTreeMap::new();
for level in 0..depth {
files.insert(
format!("s{level}.jsonic"),
format!(r#"@"s{}.jsonic""#, level + 1),
);
}
files.insert(format!("s{depth}.jsonic"), "end:1".to_string());
let parser = make_with(MultiSourceOptions::new(MapResolver::from_map(files)));
let error = parser
.parse(r#"@"s0.jsonic""#)
.expect_err("the chain is capped");
assert_eq!(error.code, "multisource_depth");
}
#[test]
fn a_chain_inside_the_cap_still_resolves() {
let depth = 32;
let mut files: BTreeMap<String, String> = BTreeMap::new();
for level in 0..depth {
files.insert(
format!("s{level}.jsonic"),
format!(r#"@"s{}.jsonic""#, level + 1),
);
}
files.insert(format!("s{depth}.jsonic"), "end:1".to_string());
let parser = make_with(MultiSourceOptions::new(MapResolver::from_map(files)));
let value = parser
.parse(r#"@"s0.jsonic""#)
.expect("a chain inside the cap resolves");
assert_eq!(to_json(&value), serde_json::json!({ "end": 1.0 }));
}
#[test]
fn a_processor_alias_matches_typescript_where_go_cannot() {
let parser = make_with(
MultiSourceOptions::new(FileResolver::new())
.with_path(ts_test_dir())
.with_processor_alias("foo", "jsonic"),
);
let value = parser.parse(r#"@"t04.foo""#).expect("the alias resolves");
assert_eq!(to_json(&value), serde_json::json!({ "a": 1.0 }));
}