-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtestScripts.py
More file actions
55 lines (46 loc) · 2.15 KB
/
testScripts.py
File metadata and controls
55 lines (46 loc) · 2.15 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
## Copyright (c) 2020-2023 The PyUnity Team
## This file is licensed under the MIT License.
## See https://docs.pyunity.x10.bz/en/latest/license.html
from pyunity import Logger, PyUnityException, Scripts
from . import TestCase
from pathlib import Path
import sys
currentdir = Path(__file__).absolute().parent
class TestScripts(TestCase):
def testCheckScript(self):
with open(currentdir / "file1.py") as f:
assert Scripts.CheckScript(f.read().split("\n"))
with open(currentdir / "file2.py") as f:
assert not Scripts.CheckScript(f.read().split("\n"))
def testLoadScript(self):
sys.modules["PyUnityScripts"] = 0
with Logger.TempRedirect(silent=True) as r:
Scripts.GenerateModule()
assert r.get() == "Warning: PyUnityScripts is already a package\n"
sys.modules.pop("PyUnityScripts")
module = Scripts.GenerateModule()
assert module is Scripts.GenerateModule()
assert hasattr(module, "__pyunity__")
assert module.__pyunity__
assert __import__("PyUnityScripts") is module
file = currentdir / "TestBehaviour1.py"
behaviour = Scripts.LoadScript(file)
assert str(file) in module._lookup
assert module._lookup[str(file)] is behaviour
assert "TestBehaviour1" in Scripts.var
assert Scripts.var["TestBehaviour1"] is behaviour
assert "TestBehaviour1" in module.__all__
assert hasattr(module, "TestBehaviour1")
assert getattr(module, "TestBehaviour1") is behaviour
def testLoadScriptFails(self):
with self.assertRaises(PyUnityException) as exc:
Scripts.LoadScript("DoesNotExist.py")
assert exc.value == "The specified file does not exist: 'DoesNotExist.py'"
file = currentdir / "file1.py"
with self.assertRaises(PyUnityException) as exc:
Scripts.LoadScript(file)
assert exc.value == f"Cannot find class 'file1' in {str(file)!r}"
file = currentdir / "file2.py"
with Logger.TempRedirect(silent=True) as r:
Scripts.LoadScript(file)
assert r.get() == f"Warning: {str(file)!r} is not a valid PyUnity script\n"