1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
pub use crate::avm1::parse_action;
pub use crate::cfg::parse_cfg;
mod avm1;
mod basic_data_types;
mod cfg;
#[cfg(test)]
mod parser_tests {
use super::*;
use ::avm1_types as avm1;
use ::test_generator::test_resources;
use avm1_types::cfg::Cfg;
use std::io::{Read, Write};
use std::path::Path;
#[test_resources("../tests/actions/*.avm1")]
fn test_parse_action(path: &str) {
let json_path: String = path.replace(".avm1", ".json");
let mut input_file = ::std::fs::File::open(path).unwrap();
let mut input: Vec<u8> = Vec::new();
input_file.read_to_end(&mut input).expect("Failed to read AVM1 file");
let (remaining_input, actual_action) = parse_action(&input).unwrap();
assert_eq!(remaining_input, &[] as &[u8]);
let json_file = ::std::fs::File::open(json_path).unwrap();
let reader = ::std::io::BufReader::new(json_file);
let expected_action: avm1::raw::Action = serde_json_v8::from_reader(reader).unwrap();
assert_eq!(actual_action, expected_action);
}
#[test_resources("../tests/avm1/[!.]*/*/")]
fn test_parse_cfg(path: &str) {
use serde::Serialize;
let path: &Path = Path::new(path);
let _name = path
.components()
.last()
.unwrap()
.as_os_str()
.to_str()
.expect("Failed to retrieve sample name");
let avm1_path = path.join("main.avm1");
let avm1_bytes: Vec<u8> = ::std::fs::read(avm1_path).expect("Failed to read input");
let actual_cfg = parse_cfg(&avm1_bytes);
let actual_cfg_path = path.join("local-cfg.rs.json");
let actual_cfg_file = ::std::fs::File::create(actual_cfg_path).expect("Failed to create actual CFG file");
let actual_cfg_writer = ::std::io::BufWriter::new(actual_cfg_file);
let mut ser = serde_json_v8::Serializer::pretty(actual_cfg_writer);
actual_cfg.serialize(&mut ser).expect("Failed to write actual CFG");
ser.into_inner().write_all(b"\n").unwrap();
let expected_cfg_path = path.join("cfg.json");
let expected_cfg_file = ::std::fs::File::open(expected_cfg_path).expect("Failed to open CFG");
let expected_cfg_reader = ::std::io::BufReader::new(expected_cfg_file);
let expected_cfg = serde_json_v8::from_reader::<_, Cfg>(expected_cfg_reader).expect("Failed to read CFG");
assert_eq!(actual_cfg, expected_cfg);
}
}