Skip to content

Commit

Permalink
refs #1720 added test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnich committed Jan 1, 2018
1 parent 0781423 commit ef1d4db
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 15 deletions.
1 change: 1 addition & 0 deletions doxygen/lang/900_release_notes.dox.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@subsection qore_08132_bug_fixes Bug Fixes in Qore
- <a href="../../modules/WebSocketHandler/html/index.html">WebSocketHandler</a>: fixed a bug where unsolicited \c PONG messages caused the connection to be prematurely closed (<a href="https://github.com/qorelanguage/qore/issues/2566">issue 2566</a>)
- <a href="../../modules/WebSocketClient/html/index.html">WebSocketClient</a>: added \c WebSocketClient::pong() to allow unsolicited \c PONG messages to be sent (<a href="https://github.com/qorelanguage/qore/issues/2566">issue 2566</a>)
- <a href="../../modules/QUnit/html/index.html">WebSocQUnittClient</a>: improved error location reporting by providing all stack location information up until the QUnit call to cover the case when multiple code layers are used such as one or more test modules (<a href="https://github.com/qorelanguage/qore/issues/1720">issue 1720</a>)

@section qore_08131 Qore 0.8.13.1

Expand Down
68 changes: 68 additions & 0 deletions examples/test/qlib/QUnit/exception.qtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/Util.qm
%requires ../../../../qlib/QUnit.qm

%exec-class ExceptionTest

public class ExceptionTest inherits Test {
constructor() : Test("ExceptionTest", "1.0") {
addTestCase("exception test", \exceptionTest());

set_return_value(main());
}

exceptionTest() {
hash<ExceptionInfo> ex((
"err": "X",
"desc": "X",
"type": "User",
"file": "QUnit.qm",
"line": 1,
"endline": 1,
"source": "",
"offset": 0,
"callstack": (
new hash<CallStackInfo>((
"function": "QUnit::assertEq",
"line": 2,
"endline": 2,
"file": "MyTest2.qm",
"source": "",
"offset": 0,
"typecode": 0,
"type": "user",
)),
new hash<CallStackInfo>((
"function": "MyTest2::assertEq",
"line": 1,
"endline": 1,
"file": "MyTest1.qm",
"source": "",
"offset": 0,
"typecode": 0,
"type": "user",
)),
new hash<CallStackInfo>((
"function": "t",
"line": 1,
"endline": 1,
"file": "test",
"source": "",
"offset": 0,
"typecode": 0,
"type": "user",
)),
),
));
string str = TestCase::getPos(ex);
#printf("str: %y\n", str);
assertRegex("test.*->.*MyTest1.*->.*MyTest2", str);
}
}
46 changes: 31 additions & 15 deletions qlib/QUnit.qm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
%requires Util

module QUnit {
version = "0.3.1";
version = "0.3.2";
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,9 @@ public class MyTestClass inherits QUnit::DependencyInjectedTest {

@section unittest_relnotes Release Notes

@subsection qunit_v0_3_2 Version 0.3.2
- improved error location reporting by providing all stack location information up until the QUnit call to cover the case when multiple code layers are used such as one or more test modules (<a href="https://github.com/qorelanguage/qore/issues/1720">issue 1720</a>)

@subsection qunit_v0_3_1 Version 0.3.1
- added the following methods:
- @ref QUnit::Test::assertRegex()
Expand Down Expand Up @@ -448,23 +451,31 @@ public class QUnit::TestCase {
}
}

static *string getStackPos(list stack) {
static list<string> getStackList(list stack) {
list<string> l();
foreach hash callSite in (stack) {
if (callSite.file !~ /\.qm/) {
string fn = regex_subst (callSite.file, "<run-time-loaded: (.*)>", "$1");
return sprintf("%s:%d", fn, callSite.line + callSite.offset);
if (callSite.file !~ /QUnit\.qm/) {
string fn = callSite.file;
fn =~ s/<run-time-loaded: (.*)>/$1/;
# try to add function/method call name
if (*string f = stack[$# + 1].function)
fn = sprintf("%s() at %s", f, fn);
else
fn = "<start> " + fn;
l += sprintf("%s:%d", fn, callSite.line + callSite.offset);
}
}
return l;
}

static string getPos(hash ex) {
string pos = get_ex_pos(ex);
if (pos =~ /\.qm/ && ex.callstack) {
*string np = TestCase::getStackPos(ex.callstack);
if (np)
pos = np;
list<string> l = TestCase::getStackList(ex.callstack);
{
string pos = get_ex_pos(ex);
if (pos !~ /QUnit\.qm/)
unshift l, pos;
}
return pos;
return (foldr $1 + " -> " + $2, l) ?? "<unknown>";
}

#! handles exceptions raised while running the TestCase
Expand Down Expand Up @@ -728,7 +739,7 @@ public class QUnit::TestReporter {
print("\n");
if (testcase.result != TEST_SUCCESS) {
string out = "";
printf("%s at %s\n", testcase.error, testcase.pos);
printf("%s in %s\n", testcase.error, testcase.pos);
ListIterator errorDescription(testcase.detail.split("\n"));
while (errorDescription.next()) {
out += sprintf("\t>> %s\n", errorDescription.getValue());
Expand Down Expand Up @@ -756,7 +767,7 @@ public class QUnit::TestReporter {
hash testcase;

testcase."^attributes^".name = it.getValue().testcase.getName();
string errmsg = sprintf("%s at %s:\n%s", it.getValue().error, it.getValue().pos, it.getValue().detail);
string errmsg = sprintf("%s in %s:\n%s", it.getValue().error, it.getValue().pos, it.getValue().detail);

int status = it.getValue().result;
testcase."^attributes^"."status" = RESULT_TYPE_DESCRIPTION{status}.desc;
Expand Down Expand Up @@ -1903,8 +1914,13 @@ fail("Unexpected code executed");
static string getAssertionName(*string name) {
if (exists name)
return sprintf("%y", name);
*string np = TestCase::getStackPos(get_all_thread_call_stacks(){gettid()});
return np ? sprintf("at %s", np) : "<no name>";
*string np;
{
list<string> l = TestCase::getStackList(get_thread_call_stack());
if (l)
np = foldr $1 + " -> " + $2, l;
}
return np ? sprintf("in %s", np) : "<no name>";
}
}

Expand Down

0 comments on commit ef1d4db

Please sign in to comment.