Skip to content

Commit

Permalink
test: Add a proper test and add it to cI
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsergio committed Apr 4, 2024
1 parent e37359f commit f1ed5fc
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: 18.x

- name: Install dependencies
run: npm install

- name: tsc
run: tsc

- name: Build Qt test
run: |
cmake --preset=dev -S test/qt_test/
cmake --build test/qt_test/build-dev/
- name: Run test
run: node out/test.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/test/qt_test/.cache
*.tgz
*.log
/out/
5 changes: 5 additions & 0 deletions src/qttest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export class QtTest {
return path.basename(this.filename);
}

public relativeFilename() {
let current_dir = process.cwd();
return this.filename.replace(current_dir + "/", "");
}

/**
* Calls "./yourqttest -functions" and stores the results in the slots property.
*/
Expand Down
95 changes: 95 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// Author: Sergio Martins <[email protected]>
// SPDX-License-Identifier: MIT

import { QtTest, QtTests } from "./qttest";

// Be sure to build the Qt tests with CMake first
// See .github/workflows/ci.yml

async function runTests(buildDirPath: string)
{
let qt = new QtTests();
await qt.discoverViaCMake(buildDirPath);

let expected_executables = [
"test/qt_test/build-dev/test1",
"test/qt_test/build-dev/test2",
"test/qt_test/build-dev/test3"];

if (qt.qtTestExecutables.length != expected_executables.length) {
console.error("Expected 3 executables, got " + qt.qtTestExecutables.length);
process.exit(1);
}

// 1. Test that the executable test names are correct:
var i = 0;
for (var executable of qt.qtTestExecutables) {
let expected = expected_executables[i];
if (executable.relativeFilename() != expected) {
console.error("Expected executable " + expected + ", got " + executable.relativeFilename());
process.exit(1);
}
i++;
}

// 2. Test that the disovered slots are correct:
await qt.dumpTestSlots();

interface ExpectedSlots {
[key: string]: string[];
}
let expected_slots: ExpectedSlots = {
"test/qt_test/build-dev/test1": ["testA", "testB", "testC"],
"test/qt_test/build-dev/test2": ["testD", "testE", "testF"],
"test/qt_test/build-dev/test3": ["testAbortsEverythig", "testH", "testI"],
};

for (var executable of qt.qtTestExecutables) {
var i = 0;

for (let slot of executable.slots!) {

let expected_slot = expected_slots[executable.relativeFilename()][i];
if (slot.name != expected_slot) {
console.error("Expected slot " + expected_slot + ", got " + slot.name);
process.exit(1);
}
i++;
}
}

// 3. Run the tests:
let expected_success = [true, false, false];
var i = 0;
for (var executable of qt.qtTestExecutables) {
await executable.runTest();
let wasSuccess = executable.lastExitCode === 0;
if (wasSuccess && !expected_success[i]) {
console.error("Expected test to fail: " + executable.filename);
process.exit(1);
} else if (!wasSuccess && expected_success[i]) {
console.error("Expected test to pass: " + executable.filename);
process.exit(1);
}

i++;
}

// 4. Run individual slots:
let slot = qt.qtTestExecutables[0].slots![0];
await slot.runTest();
if (slot.lastTestFailure) {
console.error("Expected test to pass: " + slot.name);
process.exit(1);
}

let slot2 = qt.qtTestExecutables[1].slots![2];
await slot2.runTest();
if (!slot2.lastTestFailure) {
console.error("Expected test to fail: " + slot2.name);
process.exit(1);
}
}

runTests("test/qt_test/build-dev/");

0 comments on commit f1ed5fc

Please sign in to comment.