This Trac instance is not used for development anymore!

We migrated our development workflow to git and Gitea.
To test the future redirection, replace trac by ariadne in the page URL.

source: ps/trunk/source/simulation2/components/tests/test_scripts.h

Last change on this file was 27965, checked in by Vladislav Belov, 13 months ago

Revert non-ASCII characters from source and configuration files introduced in rP27786.

Fixes #6846

Differential Revision: https://code.wildfiregames.com/D5185

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1/* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "simulation2/system/ComponentTest.h"
19#include "simulation2/serialization/StdDeserializer.h"
20#include "simulation2/serialization/StdSerializer.h"
21
22#include "ps/Filesystem.h"
23
24#include "scriptinterface/FunctionWrapper.h"
25#include "scriptinterface/ScriptContext.h"
26
27class TestComponentScripts : public CxxTest::TestSuite
28{
29public:
30 void setUp()
31 {
32 g_VFS = CreateVfs();
33 g_VFS->Mount(L"", DataDir() / "mods" / "mod" / "", VFS_MOUNT_MUST_EXIST);
34 g_VFS->Mount(L"", DataDir() / "mods" / "public" / "", VFS_MOUNT_MUST_EXIST, 1); // ignore directory-not-found errors
35 CXeromyces::Startup();
36 }
37
38 void tearDown()
39 {
40 CXeromyces::Terminate();
41 g_VFS.reset();
42 }
43
44 static void load_script(const ScriptInterface& scriptInterface, const VfsPath& pathname)
45 {
46 CVFSFile file;
47 TS_ASSERT_EQUALS(file.Load(g_VFS, pathname), PSRETURN_OK);
48 CStr content = file.DecodeUTF8(); // assume it's UTF-8
49 TSM_ASSERT(L"Running script "+pathname.string(), scriptInterface.LoadScript(pathname, content));
50 }
51
52 static void Script_LoadComponentScript(const ScriptInterface& scriptInterface, const VfsPath& pathname)
53 {
54 ScriptRequest rq(scriptInterface);
55 CComponentManager* componentManager = scriptInterface.ObjectFromCBData<CComponentManager>(rq);
56 TS_ASSERT(componentManager->LoadScript(VfsPath(L"simulation/components") / pathname));
57 }
58
59 static void Script_LoadHelperScript(const ScriptInterface& scriptInterface, const VfsPath& pathname)
60 {
61 ScriptRequest rq(scriptInterface);
62 CComponentManager* componentManager = scriptInterface.ObjectFromCBData<CComponentManager>(rq);
63 TS_ASSERT(componentManager->LoadScript(VfsPath(L"simulation/helpers") / pathname));
64 }
65
66 static JS::Value Script_SerializationRoundTrip(const ScriptInterface& scriptInterface, JS::HandleValue value)
67 {
68 ScriptRequest rq(scriptInterface);
69
70 JS::RootedValue val(rq.cx);
71 val = value;
72 std::stringstream stream;
73 CStdSerializer serializer(scriptInterface, stream);
74 serializer.ScriptVal("", &val);
75 CStdDeserializer deserializer(scriptInterface, stream);
76 deserializer.ScriptVal("", &val);
77 return val;
78 }
79
80 void test_global_scripts()
81 {
82 if (!VfsDirectoryExists(L"globalscripts/tests/"))
83 {
84 debug_printf("Skipping globalscripts tests (can't find binaries/data/mods/public/globalscripts/tests/)\n");
85 return;
86 }
87
88 VfsPaths paths;
89 TS_ASSERT_OK(vfs::GetPathnames(g_VFS, L"globalscripts/tests/", L"test_*.js", paths));
90 for (const VfsPath& path : paths)
91 {
92 CSimContext context;
93 CComponentManager componentManager(context, *g_ScriptContext, true);
94 ScriptTestSetup(componentManager.GetScriptInterface());
95
96 ScriptRequest rq(componentManager.GetScriptInterface());
97 ScriptFunction::Register<Script_SerializationRoundTrip>(rq, "SerializationRoundTrip");
98
99 load_script(componentManager.GetScriptInterface(), path);
100 }
101 }
102
103 void test_scripts()
104 {
105 if (!VfsFileExists(L"simulation/components/tests/setup.js"))
106 {
107 debug_printf("Skipping component scripts tests (can't find binaries/data/mods/public/simulation/components/tests/setup.js)\n");
108 return;
109 }
110
111 VfsPaths paths;
112 TS_ASSERT_OK(vfs::GetPathnames(g_VFS, L"simulation/components/tests/", L"test_*.js", paths));
113 TS_ASSERT_OK(vfs::GetPathnames(g_VFS, L"simulation/helpers/tests/", L"test_*.js", paths));
114 paths.push_back(VfsPath(L"simulation/components/tests/setup_test.js"));
115 for (const VfsPath& path : paths)
116 {
117 // Clean up previous scripts.
118 g_ScriptContext->ShrinkingGC();
119 CSimContext context;
120 CComponentManager componentManager(context, *g_ScriptContext, true);
121
122 ScriptTestSetup(componentManager.GetScriptInterface());
123
124 ScriptRequest rq(componentManager.GetScriptInterface());
125 ScriptFunction::Register<Script_LoadComponentScript>(rq, "LoadComponentScript");
126 ScriptFunction::Register<Script_LoadHelperScript>(rq, "LoadHelperScript");
127 ScriptFunction::Register<Script_SerializationRoundTrip>(rq, "SerializationRoundTrip");
128
129 componentManager.LoadComponentTypes();
130
131 load_script(componentManager.GetScriptInterface(), L"simulation/components/tests/setup.js");
132 load_script(componentManager.GetScriptInterface(), path);
133 }
134 }
135};
Note: See TracBrowser for help on using the repository browser.