use super::{format_f64, quoted};
use crate::{OwnedArgument, OwnedData, Scene, write_stream};
use nsi_trait::Type;
#[test]
fn doubles_format_the_way_3delight_writes_them() {
for (value, expected) in [
(0.1f64, "0.10000000000000001"),
(1.0 / 3.0, "0.33333333333333331"),
(1e-7, "9.9999999999999995e-08"),
(1e20, "1e+20"),
(0.5, "0.5"),
(1.0, "1"),
(-0.0, "-0"),
(0.0, "0"),
] {
assert_eq!(format_f64(value), expected, "for {value}");
}
}
#[test]
fn the_reserved_handles_are_never_declared() {
let mut scene = Scene::default();
scene
.set_attribute(
crate::GLOBAL,
vec![OwnedArgument {
name: "renderatlowpriority".to_string(),
type_tag: Type::I32,
array_length: 1,
flags: 0,
data: OwnedData::I32(vec![1]),
}],
)
.unwrap();
let mut out = Vec::new();
write_stream(&scene, &mut out).expect("write");
let text = String::from_utf8(out).expect("utf-8");
assert!(
!text.contains("Create"),
"no Create for a reserved handle:\n{text}"
);
assert!(text.contains("SetAttribute \".global\""));
}
#[test]
fn an_empty_slice_still_brackets() {
let mut scene = Scene::default();
scene.create("m", "mesh").unwrap();
scene
.set_attribute(
"m",
vec![OwnedArgument {
name: "empty".to_string(),
type_tag: Type::F32,
array_length: 1,
flags: 0,
data: OwnedData::F32(Vec::new()),
}],
)
.unwrap();
let mut out = Vec::new();
write_stream(&scene, &mut out).expect("write");
let text = String::from_utf8(out).expect("utf-8");
assert!(text.contains("[ ]"), "empty slice brackets:\n{text}");
}
#[test]
fn a_string_cannot_inject_a_statement() {
let injected = "say \"hi\"\nCreate \"evil\" \"mesh\"";
let escaped = String::from_utf8(quoted(injected.as_bytes()))
.expect("ASCII escapes keep it UTF-8");
assert!(!escaped.contains('\n'), "no raw newline: {escaped}");
assert_eq!(
escaped.matches('"').count() - escaped.matches("\\\"").count(),
2,
"only the delimiters are unescaped quotes"
);
}
#[test]
fn a_recorded_scene_with_hostile_strings_stays_one_statement_a_line() {
let mut scene = Scene::default();
scene.create("me\"ss\ny", "mesh").expect("new handle");
scene
.set_attribute(
"me\"ss\ny",
vec![OwnedArgument {
name: "na\"me".to_string(),
type_tag: Type::String,
array_length: 1,
flags: 0,
data: OwnedData::String(vec![
b"Create \"evil\" \"mesh\"".to_vec(),
]),
}],
)
.unwrap();
let mut out = Vec::new();
write_stream(&scene, &mut out).expect("write");
let text = String::from_utf8(out).expect("utf-8");
assert_eq!(
text.lines().filter(|l| l.starts_with("Create ")).count(),
1,
"exactly one Create statement:\n{text}"
);
for line in text.lines() {
let bare = line.replace("\\\\", "").matches('"').count()
- line.replace("\\\\", "").matches("\\\"").count();
assert_eq!(bare % 2, 0, "unbalanced quotes in {line:?}");
assert!(
!line.trim_start().starts_with("evil"),
"a value escaped its literal: {line:?}"
);
}
}
#[test]
fn a_non_utf8_byte_replays_raw() {
let mut scene = Scene::default();
scene.create("d", "outputdriver").unwrap();
scene
.set_attribute(
"d",
vec![OwnedArgument {
name: "imagefilename".to_string(),
type_tag: Type::String,
array_length: 1,
flags: 0,
data: OwnedData::String(vec![b"caf\xE9.exr".to_vec()]),
}],
)
.unwrap();
let mut out = Vec::new();
write_stream(&scene, &mut out).expect("write");
assert!(
out.windows(8).any(|w| w == b"caf\xE9.exr"),
"the byte is written raw, not escaped or replaced",
);
assert!(
!out.windows(3).any(|w| w == [0xEF, 0xBF, 0xBD]),
"no U+FFFD anywhere",
);
}
#[test]
fn control_bytes_escape_but_high_bytes_do_not() {
let escaped = quoted(b"a\x01b\xE9c");
assert_eq!(escaped, b"\"a\\001b\xE9c\"".to_vec());
}