Skip to main content

unityrs_testkit/ngui/scenes/
failures.rs

1//! The scenes whose inputs fail (`docs/BATCH_SPEC.md` 7.3's `inputs` rows,
2//! `docs/plans/batch.md` 2.6 and 2.7): a member that does not open or is no
3//! bundle, an input that is no bundle, and an input whose top-level row
4//! fails at the reach or at selection's open. The reference records an
5//! input's results, not its failure, so these are listed apart from
6//! [`super::scenes`], in [`failure_scenes`], for the cutter's own tests,
7//! which take their answers from the spec.
8
9use super::super::{BuildError, file, mono_script};
10use super::{AtlasScene, Built, CutOptions, by_name, one, plain, rgba, scene};
11use crate::env::bundle;
12
13/// A bundle of `cab`, cut to its first 48 bytes: its signature and the head
14/// of its header, so a peek of 21 bytes takes it for a bundle and its open
15/// fails.
16fn broken_bundle() -> Vec<u8> {
17    let mut b = bundle(&[("CAB-n", vec![0; 64])]);
18    b.truncate(48);
19    b
20}
21
22fn cab(id: &str) -> Result<Vec<u8>, BuildError> {
23    file(
24        &[],
25        &[
26            mono_script(1, "NGUIAtlas"),
27            rgba(2, id, 0xF0, 1, 1),
28            by_name(3, id, vec![plain("s", (0, 0, 1, 1))]),
29        ],
30        true,
31    )
32}
33
34/// `x.bundle`, depending on `d.bundle`, whose bytes do not open:
35/// `dependency_failed(d.bundle, container_error)`.
36pub fn member_fails() -> Built {
37    Ok(scene(
38        vec![
39            one("x.bundle", &[("CAB-x", cab("x")?)], &["d.bundle"]),
40            super::SceneBundle {
41                id: "d.bundle".to_owned(),
42                data: broken_bundle(),
43                deps: Vec::new(),
44            },
45        ],
46        &["x.bundle"],
47        CutOptions::default(),
48    ))
49}
50
51/// `x.bundle`, depending on `d.bundle`, a SerializedFile and no bundle:
52/// `dependency_failed(d.bundle, not_a_bundle)`.
53pub fn member_not_a_bundle() -> Built {
54    Ok(scene(
55        vec![
56            one("x.bundle", &[("CAB-x", cab("x")?)], &["d.bundle"]),
57            super::SceneBundle {
58                id: "d.bundle".to_owned(),
59                data: cab("d")?,
60                deps: Vec::new(),
61            },
62        ],
63        &["x.bundle"],
64        CutOptions::default(),
65    ))
66}
67
68/// `x.bundle`, a SerializedFile and no bundle: `not_a_bundle`.
69pub fn input_not_a_bundle() -> Built {
70    Ok(scene(
71        vec![super::SceneBundle {
72            id: "x.bundle".to_owned(),
73            data: cab("x")?,
74            deps: Vec::new(),
75        }],
76        &["x.bundle"],
77        CutOptions::default(),
78    ))
79}
80
81/// `x.bundle` with a top-level row `CAB-n` that peeks as a bundle and does
82/// not open: the reach of the input's containers fails, its `entry_error`.
83pub fn reach_fails() -> Built {
84    Ok(scene(
85        vec![one(
86            "x.bundle",
87            &[("CAB-x", cab("x")?), ("CAB-n", broken_bundle())],
88            &[],
89        )],
90        &["x.bundle"],
91        CutOptions::default(),
92    ))
93}
94
95/// `x.bundle` with two top-level rows `CAB-n`, the first a bundle that does
96/// not open and the second raw bytes: the reach takes the later row, which
97/// is no bundle, and selection's open of every row fails at the first, the
98/// input's `entry_error`.
99pub fn selection_fails() -> Built {
100    Ok(scene(
101        vec![one(
102            "x.bundle",
103            &[
104                ("CAB-x", cab("x")?),
105                ("CAB-n", broken_bundle()),
106                ("CAB-n", b"raw bytes".to_vec()),
107            ],
108            &[],
109        )],
110        &["x.bundle"],
111        CutOptions::default(),
112    ))
113}
114
115/// Every failure scene, by name.
116pub fn failure_scenes() -> Result<Vec<(&'static str, AtlasScene)>, BuildError> {
117    let all: [(&'static str, super::Make); 5] = [
118        ("member_fails", member_fails),
119        ("member_not_a_bundle", member_not_a_bundle),
120        ("input_not_a_bundle", input_not_a_bundle),
121        ("reach_fails", reach_fails),
122        ("selection_fails", selection_fails),
123    ];
124    all.into_iter().map(|(name, f)| Ok((name, f()?))).collect()
125}