-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_tests.rs
113 lines (98 loc) · 3.38 KB
/
integration_tests.rs
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
use dotenv::dotenv;
use spriterator::Spriterator;
use std::env;
use std::error::Error;
use std::fs;
use std::path::Path;
/// Test the Spriterator struct
///
/// INPUT_DIR = /path/to/input/images
/// OUTPUT_DIR = /path/to/output/sprites
/// FILE_EXTENSION = png
#[test]
#[ignore] // Ignored due to CI requirements (a directory with images is necessary for it to function).
fn spriterator_test() -> Result<(), Box<dyn Error>> {
dotenv().ok();
let input_dir = env::var("INPUT_DIR").expect("INPUT_DIR must be set");
let output_dir = env::var("OUTPUT_DIR").expect("OUTPUT_DIR must be set");
let ext = env::var("FILE_EXTENSION").expect("FILE_EXTENSION must be set");
let large_image_size: u32 = env::var("LARGE_IMAGE_SIZE")
.expect("LARGE_IMAGE_SIZE must be set")
.parse()
.unwrap();
let medium_image_size: u32 = env::var("MEDIUM_IMAGE_SIZE")
.expect("MEDIUM_IMAGE_SIZE must be set")
.parse()
.unwrap();
let small_image_size: u32 = env::var("SMALL_IMAGE_SIZE")
.expect("SMALL_IMAGE_SIZE must be set")
.parse()
.unwrap();
let output_dir = format!("{}/{}", output_dir, ext);
let dir_path = Path::new(&output_dir);
if dir_path.exists() {
fs::remove_dir_all(dir_path)?;
}
fs::create_dir_all(dir_path)?;
let spriterator = Spriterator::new(
format!("{}/{}", input_dir, ext).as_str(),
2048,
2048,
None,
None,
);
let sprites = spriterator.generate();
let sprites = sprites.map_err(|e| {
eprintln!("Failed to generate sprites: {}", e);
e
})?;
assert!(!sprites.is_empty(), "No sprites were generated");
for (index, sprite) in sprites.iter().enumerate() {
let frames = sprite.get_frames();
assert!(!frames.is_empty(), "Sprite {} has no frames", index);
let expected_frames = vec![
(0, 0, medium_image_size, medium_image_size),
(medium_image_size, 0, small_image_size, small_image_size),
(
medium_image_size + small_image_size,
0,
large_image_size,
large_image_size,
),
];
for (i, frame) in frames.iter().enumerate() {
let (expected_x, expected_y, expected_width, expected_height) =
expected_frames.get(i).expect("Unexpected frame count");
assert_eq!(frame.get_x(), *expected_x, "Frame {} X mismatch", i);
assert_eq!(frame.get_y(), *expected_y, "Frame {} Y mismatch", i);
assert_eq!(
frame.get_width(),
*expected_width,
"Frame {} width mismatch",
i
);
assert_eq!(
frame.get_height(),
*expected_height,
"Frame {} height mismatch",
i
);
}
let sprite_path = format!("{}/{}.{}", output_dir, index + 1, ext);
sprite.save(&sprite_path)?;
assert!(
Path::new(&sprite_path).exists(),
"Sprite file was not created: {}",
sprite_path
);
}
let saved_files: Vec<_> = fs::read_dir(dir_path)?
.filter_map(|entry| entry.ok())
.collect();
assert_eq!(
saved_files.len(),
sprites.len(),
"Mismatch between number of sprites and saved files"
);
Ok(())
}