Issues (10)

tests/tests.py (1 issue)

1
import sys
2
import os
3
from unittest import main, TestCase
4
from subprocess import call, DEVNULL
5
from pypen.utils.math import *
6
from pypen import settings
7
8
9
class TestExamples(TestCase):
10
11
    def test_util_functions(self):
12
        cool_print("Testing util functions...")
13
        # Constants ok
14
        self.assertAlmostEqual(PI, 3.141, 2)
15
        self.assertAlmostEqual(TAU, 6.283, 2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TAU does not seem to be defined.
Loading history...
16
        self.assertAlmostEqual(E, 2.718, 2)
17
        print("constants OK")
18
19
        # clamp ok
20
        self.assertEqual(clamp(110, 0, 100), 100)
21
        self.assertEqual(clamp(-110, 0, 100), 0)
22
        print("clamp OK")
23
24
        # lerp ok
25
        self.assertEqual(lerp(0, 100, 1), 100)
26
        self.assertEqual(lerp(0, 100, 1.5), 100)
27
        self.assertEqual(lerp(100, 200, 1.5), 200)
28
        self.assertEqual(lerp(100, 200, 0.1), 110)
29
        self.assertNotEqual(lerp(0, 100, 1.5), 150)
30
        print("lerp OK")
31
32
        # lerp_unclamped ok
33
        self.assertEqual(lerp_unclamped(0, 100, 1.5), 150)
34
        self.assertEqual(lerp_unclamped(100, 200, 1.1), 210)
35
        print("lerp_unclamped OK")
36
37
    def test_all_examples(self):
38
        cool_print("Trying to run pypen command with timeout on all sketches in examples/")
39
        examples_dir_path = os.path.join(os.path.split(
40
            os.path.realpath(__file__))[0], "..", "examples")
41
42
        for example_filename in os.listdir(examples_dir_path):
43
            if os.path.splitext(example_filename)[1] == ".py":
44
                print()
45
                print("Running {}".format(example_filename))
46
                return_code = run_pypen_sketch(
47
                    os.path.join(examples_dir_path, example_filename))
48
                self.assertEqual(return_code, 0)
49
                print("OK")
50
51
    def test_all_test_scenes(self):
52
        cool_print("Trying to run pypen command with timeout on all sketches in tests/test_scenes/")
53
        test_scenes_dir_path = os.path.join(os.path.split(
54
            os.path.realpath(__file__))[0], "test_scenes")
55
56
        filename_to_return_code = {
57
            "test_empty_file.py": 1,
58
            "test_colors.py": 0,
59
            "test_no_start_or_update.py": 1
60
        }
61
62
        for test_filename in os.listdir(test_scenes_dir_path):
63
            if os.path.splitext(test_filename)[1] == ".py":
64
                print()
65
                print("Running {}".format(test_filename))
66
                return_code = run_pypen_sketch(
67
                    os.path.join(test_scenes_dir_path, test_filename))
68
                self.assertEqual(return_code, filename_to_return_code[test_filename] if test_filename in filename_to_return_code else 0)
69
                print("OK")
70
71
    def test_init(self):
72
        cool_print("Trying to run pypen --init")
73
        arguments = ["pypen", "--init", os.path.join(os.path.split(os.path.realpath(__file__))[0], "test_scenes", "test_init.py")]
74
        return_code = call(arguments, timeout=4000, stdout=DEVNULL, stderr=sys.stderr)
75
        print(' '.join(arguments))
76
        self.assertEqual(return_code, 0)
77
        print("OK")
78
79
80
def run_pypen_sketch(example_path, custom_timeout=2000, use_stdout=False):
81
    timeout_time = custom_timeout
82
    arguments = ["pypen", example_path, "--timeout", str(timeout_time)]
83
84
    print(' '.join(arguments))
85
    return_code = call(arguments, timeout=timeout_time*4, stdout=DEVNULL if not use_stdout else sys.stdout, stderr=sys.stderr)
86
    return return_code
87
88
89
def cool_print(message):
90
    print()
91
    print()
92
    print()
93
    print("==== {} ====".format(message))
94
    print()
95
96
97
if __name__ == "__main__":
98
    cool_print("STARTING PYPEN TESTS")
99
    main()
100