Skip to content

Commit

Permalink
Merge pull request #3307 from qorelanguage/bugfix/3306_nested_tests
Browse files Browse the repository at this point in the history
refs #3306 fixed a bug where tests could not be nested
  • Loading branch information
pvanek committed Feb 5, 2019
2 parents 9fb3605 + 21a4882 commit 2727dcd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
3 changes: 3 additions & 0 deletions doxygen/lang/900_release_notes.dox.tmpl
Expand Up @@ -12,6 +12,9 @@
- <a href="../../modules/Util/html/index.html">Util</a> module fixes:
- fixed a bug in \c get_exception_string() with Java exceptions
(<a href="https://github.com/qorelanguage/qore/issues/3304">issue 3304</a>)
- <a href="../../modules/QUnit/html/index.html">QUnit</a> module fixes:
- fixed a bug where tests could not be nested
(<a href="https://github.com/qorelanguage/qore/issues/3306">issue 3306</a>)

@section qore_09 Qore 0.9

Expand Down
15 changes: 15 additions & 0 deletions examples/test/qlib/QUnit/tests.qtest
Expand Up @@ -38,6 +38,7 @@ public class EmptyTest inherits QUnit::Test {

public class QUnitTest inherits QUnit::Test {
constructor() : Test("QUnitTest", "1.0") {
addTestCase("nested test", \nestedTest());
addTestCase("issue 3172", \issue3172());
addTestCase("Dependency injection tests", \testInjectedClass());
addTestCase("Test empty test", \testEmptyTest());
Expand All @@ -51,6 +52,20 @@ public class QUnitTest inherits QUnit::Test {
return True;
}

nestedTest() {
{
Test test("NestedTest", "1.0");

code test_code = sub () {
test.assertTrue(True);
};
test.addTestCase("my test", test_code);
test.main();
}

assertTrue(True);
}

issue3172() {
assertThrows("TEST-SKIPPED-EXCEPTION", "fmt: 1", \testSkip(), ("fmt: %d", 1));
}
Expand Down
37 changes: 20 additions & 17 deletions qlib/QUnit.qm
@@ -1,7 +1,7 @@
# -*- mode: qore; indent-tabs-mode: nil -*-
#! @file QUnit.qm Qore user module for automatic testing

%requires qore >= 0.8.13
%requires qore >= 0.9
%new-style

%try-module xml >= 1.3
Expand All @@ -14,7 +14,7 @@
%requires Util

module QUnit {
version = "0.4";
version = "0.4.1";
desc = "User module for unit testing with dependency injection support";
author = "Zdenek Behan <zdenek.behan@qoretechnologies.com>";
url = "http://qore.org";
Expand Down Expand Up @@ -130,6 +130,10 @@ public class MyTestClass inherits QUnit::DependencyInjectedTest {

@section unittest_relnotes Release Notes

@subsection qunit_v0_4_1 Version 0.4.1
- allow tests to be nested
(<a href="https://github.com/qorelanguage/qore/issues/3306">issue 3306</a>)

@subsection qunit_v0_4 Version 0.4
- updated @ref QUnit::Test::testSkip() "Test::testSkip()" to use the reason argument a format string with
@ref Qore::vsprintf() "vsprintf()"
Expand Down Expand Up @@ -434,6 +438,9 @@ public class QUnit::TestCase {

#! number of skipped assertions in current test case
int num_asserts_skip = 0;

#! any saved test case
auto saved_tc;
}

#! creates the TestCase object from the given arguments
Expand All @@ -457,8 +464,7 @@ public class QUnit::TestCase {
call_function_args(m_code, m_args);
# Test success
test.addTestResult(self, TestReporter::TEST_SUCCESS);
}
catch (hash<ExceptionInfo> e) {
} catch (hash<ExceptionInfo> e) {
checkException(test, e);
}
}
Expand Down Expand Up @@ -486,7 +492,7 @@ public class QUnit::TestCase {
return l;
}

static string getPos(hash ex) {
static string getPos(hash<auto> ex) {
string pos = get_ex_pos(ex);
bool qunit = (pos =~ /QUnit\.qm/);
list<string> l = TestCase::getStackList(ex.callstack, qunit);
Expand All @@ -496,7 +502,7 @@ public class QUnit::TestCase {
}

#! handles exceptions raised while running the TestCase
checkException(QUnit::Test test, hash e) {
checkException(QUnit::Test test, hash<auto> e) {
if (e.err =~ /TEST-.*EXCEPTION/) {
*string assertion_name = e.arg.name;
*string pos = e.arg.pos;
Expand Down Expand Up @@ -547,11 +553,12 @@ public class QUnit::TestCase {
}

setupThread() {
saved_tc = remove_thread_data("tc").tc;
save_thread_data("tc", self);
}

restoreThread() {
remove_thread_data("tc");
save_thread_data("tc", saved_tc);
}

#! renames the test case
Expand Down Expand Up @@ -976,8 +983,7 @@ addTestCase(obj);
expected = sprintf("(string %y) %N", exp.encoding(), exp);
actual = neg ? "<identical>" : sprintf("(string %y) %N", act.encoding(), act);
}
}
catch (hash<ExceptionInfo> ex) {
} catch (hash<ExceptionInfo> ex) {
}
}
if (!done) {
Expand Down Expand Up @@ -1829,7 +1835,7 @@ fail("Unexpected code executed");

@return the result of the \a condition call, if the immediate value has any further use
*/
public auto testAssertion(string name, code condition, *softlist<auto> args, hash expectedResultValue) {
public auto testAssertion(string name, code condition, *softlist<auto> args, hash<auto> expectedResultValue) {
return testAssertion(name, condition, args, new TestResultValue(expectedResultValue));
}

Expand All @@ -1842,7 +1848,7 @@ fail("Unexpected code executed");

@return the result of the \a condition call, if the immediate value has any further use
*/
public auto testAssertion(string name, code condition, *softlist<auto> args, list expectedResultValue) {
public auto testAssertion(string name, code condition, *softlist<auto> args, list<auto> expectedResultValue) {
return testAssertion(name, condition, args, new TestResultValue(expectedResultValue));
}

Expand Down Expand Up @@ -1874,8 +1880,7 @@ fail("Unexpected code executed");
} else {
result = new QUnit::TestResultValue(ret);
}
}
catch (e) {
} catch (hash<ExceptionInfo> e) {
if (e.err == "TEST-FAILED-EXCEPTION") {
# Since boolean can contain no detail, we abuse Exceptions this way to annotate a simple failure.
result = new QUnit::TestResultFailure(e.desc);
Expand Down Expand Up @@ -1995,8 +2000,7 @@ fail("Unexpected code executed");

try {
globalSetUp();
}
catch (hash<ExceptionInfo> ex) {
} catch (hash<ExceptionInfo> ex) {
gtc.checkException(self, ex);
}

Expand All @@ -2011,8 +2015,7 @@ fail("Unexpected code executed");
try {
gtc.rename("globalTearDown");
globalTearDown();
}
catch (hash<ExceptionInfo> ex) {
} catch (hash<ExceptionInfo> ex) {
gtc.checkException(self, ex);
}

Expand Down

0 comments on commit 2727dcd

Please sign in to comment.