| 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 |
|
|---|
| 20 | #include "simulation2/components/ICmpCommandQueue.h"
|
|---|
| 21 |
|
|---|
| 22 | class TestCmpCommandQueue : public CxxTest::TestSuite
|
|---|
| 23 | {
|
|---|
| 24 | public:
|
|---|
| 25 | void setUp()
|
|---|
| 26 | {
|
|---|
| 27 | CXeromyces::Startup();
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | void tearDown()
|
|---|
| 31 | {
|
|---|
| 32 | CXeromyces::Terminate();
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | void test_basic()
|
|---|
| 36 | {
|
|---|
| 37 | ComponentTestHelper test(*g_ScriptContext);
|
|---|
| 38 | ScriptRequest rq(test.GetScriptInterface());
|
|---|
| 39 |
|
|---|
| 40 | std::vector<SimulationCommand> empty;
|
|---|
| 41 |
|
|---|
| 42 | ICmpCommandQueue* cmp = test.Add<ICmpCommandQueue>(CID_CommandQueue, "", SYSTEM_ENTITY);
|
|---|
| 43 |
|
|---|
| 44 | TS_ASSERT(test.GetScriptInterface().Eval("var cmds = []; function ProcessCommand(player, cmd) { cmds.push([player, cmd]); }"));
|
|---|
| 45 |
|
|---|
| 46 | JS::RootedValue cmd(rq.cx);
|
|---|
| 47 |
|
|---|
| 48 | TS_ASSERT(test.GetScriptInterface().Eval("([1,2,3])", &cmd));
|
|---|
| 49 | cmp->PushLocalCommand(1, cmd);
|
|---|
| 50 |
|
|---|
| 51 | TS_ASSERT(test.GetScriptInterface().Eval("({\"x\":4})", &cmd));
|
|---|
| 52 | cmp->PushLocalCommand(-1, cmd);
|
|---|
| 53 |
|
|---|
| 54 | test.Roundtrip();
|
|---|
| 55 |
|
|---|
| 56 | // Process the first two commands
|
|---|
| 57 | cmp->FlushTurn(empty);
|
|---|
| 58 |
|
|---|
| 59 | TS_ASSERT(test.GetScriptInterface().Eval("({\"y\":5})", &cmd));
|
|---|
| 60 | cmp->PushLocalCommand(10, cmd);
|
|---|
| 61 |
|
|---|
| 62 | // Process the next command
|
|---|
| 63 | cmp->FlushTurn(empty);
|
|---|
| 64 |
|
|---|
| 65 | // Process no commands
|
|---|
| 66 | cmp->FlushTurn(empty);
|
|---|
| 67 |
|
|---|
| 68 | test.Roundtrip();
|
|---|
| 69 |
|
|---|
| 70 | std::string output;
|
|---|
| 71 | TS_ASSERT(test.GetScriptInterface().Eval("JSON.stringify(cmds)", output));
|
|---|
| 72 | TS_ASSERT_STR_EQUALS(output, "[[1,[1,2,3]],[-1,{\"x\":4}],[10,{\"y\":5}]]");
|
|---|
| 73 | }
|
|---|
| 74 | };
|
|---|