-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_sample.py
More file actions
73 lines (54 loc) · 2.1 KB
/
test_sample.py
File metadata and controls
73 lines (54 loc) · 2.1 KB
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
from unittest import TestCase
import io, contextlib, os, shutil, json, random
import numpy as np
from PIL import Image
from reference import REFERENCES, REFERENCE_SIZES, run_sample, reference_to_nparray
from parameterized import parameterized
from RGBMatrixEmulator.emulation.options import RGBMatrixEmulatorConfig
from test.context import (
REFERENCE_RANDOM_SEED,
TEST_CONFIG_PATH,
SAMPLE_CONFIG_PATH,
TestConfigContext,
)
random.seed(REFERENCE_RANDOM_SEED)
class TestSampleRunMatchesReference(TestCase):
TESTS = []
for reference in REFERENCES:
for refsize in REFERENCE_SIZES:
TESTS.append(
(f"{reference.name}-w{refsize[0]}h{refsize[1]}", reference, refsize)
)
@classmethod
def setUpClass(cls):
cls.temp_context = TestConfigContext(TEST_CONFIG_PATH, SAMPLE_CONFIG_PATH)
cls.temp_path = cls.temp_context.__enter__()
@classmethod
def tearDownClass(cls):
cls.temp_context.__exit__(None, None, None)
@parameterized.expand(TESTS)
def test_sample(self, name, sample, size):
expected = reference_to_nparray(sample, size)
if expected is None:
print(f"\n{sample.file_name} {size} has no reference image. Skipping...")
return
# Suppress "Press CTRL-C to stop sample" messages
with io.StringIO() as buf, contextlib.redirect_stdout(buf):
actual = run_sample(sample, size)
if not np.array_equal(expected, actual):
image = Image.fromarray(np.array(actual, dtype="uint8"), "RGB")
image.save(
os.path.abspath(
os.path.join(
__file__,
"..",
"result",
f"{sample.file_name}-w{size[0]}h{size[1]}.png",
)
)
)
self.assertTrue(
False,
f"Actual results do not match reference screenshot. See test/result/{sample.file_name}-w{size[0]}h{size[1]}.png to compare",
)
self.assertTrue(True)