Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
On failed assertion, return a stack trace instead of throwing an exce…
Browse files Browse the repository at this point in the history
…ption
  • Loading branch information
dmcassel committed Oct 25, 2017
1 parent abab120 commit 4b31f64
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 50 deletions.
6 changes: 4 additions & 2 deletions src/test/suites/Unit Test Tests/assert-all-exist.xqy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -13,5 +14,6 @@ declare function local:case2()
test:assert-all-exist(0, ()),
test:assert-all-exist(1, "1"),
test:assert-all-exist(2, ("1", "2")),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-ALL-EXIST-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case2")), "ASSERT-ALL-EXIST-FAILED")

tlib:test-for-failure(local:case1(), "ASSERT-ALL-EXIST-FAILED"),
tlib:test-for-failure(local:case2(), "ASSERT-ALL-EXIST-FAILED")
9 changes: 5 additions & 4 deletions src/test/suites/Unit Test Tests/assert-at-least-one-equal.xqy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -24,7 +25,7 @@ test:assert-at-least-one-equal(0, 0),
test:assert-at-least-one-equal(0, (0, 1, 2)),
test:assert-at-least-one-equal((0, 1, 2), 0),
test:assert-at-least-one-equal((0, 1, 2), (0, 3, 4)),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-AT-LEAST-ONE-EQUAL-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case2")), "ASSERT-AT-LEAST-ONE-EQUAL-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case3")), "ASSERT-AT-LEAST-ONE-EQUAL-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case4")), "ASSERT-AT-LEAST-ONE-EQUAL-FAILED")
tlib:test-for-failure(local:case1(), "ASSERT-AT-LEAST-ONE-EQUAL-FAILED"),
tlib:test-for-failure(local:case2(), "ASSERT-AT-LEAST-ONE-EQUAL-FAILED"),
tlib:test-for-failure(local:case3(), "ASSERT-AT-LEAST-ONE-EQUAL-FAILED"),
tlib:test-for-failure(local:case4(), "ASSERT-AT-LEAST-ONE-EQUAL-FAILED")
18 changes: 13 additions & 5 deletions src/test/suites/Unit Test Tests/assert-equal.xqy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -25,15 +26,22 @@ declare function local:case5()
test:assert-equal((<a><aa/></a>, <b/>, <c/>), (element a { element aaa { } }, element b {}, element c {}))
};

test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-EQUAL-FAILED"),
declare function local:case6()
{
fn:error(xs:QName("TEST-ERROR"), "deliberate attempt to throw an error")
};

test:assert-throws-error(xdmp:function(xs:QName("local:case6")), "TEST-ERROR"),

test:assert-equal(<a class="1"/>, element a { attribute class { "1" } }),

test:assert-throws-error(xdmp:function(xs:QName("local:case2")), "ASSERT-EQUAL-FAILED"),
tlib:test-for-failure(local:case1(), "ASSERT-EQUAL-FAILED"),

tlib:test-for-failure(local:case2(), "ASSERT-EQUAL-FAILED"),

test:assert-throws-error(xdmp:function(xs:QName("local:case3")), "ASSERT-EQUAL-FAILED"),
tlib:test-for-failure(local:case3(), "ASSERT-EQUAL-FAILED"),

test:assert-throws-error(xdmp:function(xs:QName("local:case4")), "ASSERT-EQUAL-FAILED"),
tlib:test-for-failure(local:case4(), "ASSERT-EQUAL-FAILED"),

test:assert-equal((<a/>, <b/>, <c/>), (<a/>, <b/>, <c/>)),

Expand All @@ -47,4 +55,4 @@ test:assert-equal("a", "a"),

test:assert-equal((), ()),

test:assert-throws-error(xdmp:function(xs:QName("local:case5")), "ASSERT-EQUAL-FAILED")
tlib:test-for-failure(local:case5(), "ASSERT-EQUAL-FAILED")
3 changes: 2 additions & 1 deletion src/test/suites/Unit Test Tests/assert-exists.xqy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -8,4 +9,4 @@ declare function local:case1()
test:assert-exists("1"),
test:assert-exists(("1", "2")),
test:assert-exists(<a/>),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-EXISTS-FAILED")
tlib:test-for-failure(local:case1(), "ASSERT-EXISTS-FAILED")
5 changes: 3 additions & 2 deletions src/test/suites/Unit Test Tests/assert-false.xqy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -12,5 +13,5 @@ declare function local:case2()

test:assert-false(fn:false()),
test:assert-false((fn:false(), fn:false())),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-FALSE-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case2")), "ASSERT-FALSE-FAILED")
tlib:test-for-failure(local:case1(), "ASSERT-FALSE-FAILED"),
tlib:test-for-failure(local:case2(), "ASSERT-FALSE-FAILED")
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -12,5 +13,5 @@ declare function local:case2()

test:assert-meets-maximum-threshold(6, 6),
test:assert-meets-maximum-threshold(6, (3, 4, 5, 6)),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-MEETS-MAXIMUM-THRESHOLD-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case2")), "ASSERT-MEETS-MAXIMUM-THRESHOLD-FAILED")
tlib:test-for-failure(local:case1(), "ASSERT-MEETS-MAXIMUM-THRESHOLD-FAILED"),
tlib:test-for-failure(local:case2(), "ASSERT-MEETS-MAXIMUM-THRESHOLD-FAILED")
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -12,5 +13,5 @@ declare function local:case2()

test:assert-meets-minimum-threshold(2, 2),
test:assert-meets-minimum-threshold(2, (3, 4, 5, 6)),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-MEETS-MINIMUM-THRESHOLD-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case2")), "ASSERT-MEETS-MINIMUM-THRESHOLD-FAILED")
tlib:test-for-failure(local:case1(), "ASSERT-MEETS-MINIMUM-THRESHOLD-FAILED"),
tlib:test-for-failure(local:case2(), "ASSERT-MEETS-MINIMUM-THRESHOLD-FAILED")
5 changes: 3 additions & 2 deletions src/test/suites/Unit Test Tests/assert-not-equal.xqy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -15,5 +16,5 @@ test:assert-not-equal((0, 1, 2), (0, 2, 1)),
test:assert-not-equal((0, 1, 2), ()),
test:assert-not-equal(<a/>, <g/>),
test:assert-not-equal(<a><aa/></a>, <g/>),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-NOT-EQUAL-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case2")), "ASSERT-NOT-EQUAL-FAILED")
tlib:test-for-failure(local:case1(), "ASSERT-NOT-EQUAL-FAILED"),
tlib:test-for-failure(local:case2(), "ASSERT-NOT-EQUAL-FAILED")
3 changes: 2 additions & 1 deletion src/test/suites/Unit Test Tests/assert-not-exists.xqy
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
test:assert-not-exists("a")
};

test:assert-not-exists(()),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-NOT-EXISTS-FAILED")
tlib:test-for-failure(local:case1(), "ASSERT-NOT-EXISTS-FAILED")
9 changes: 5 additions & 4 deletions src/test/suites/Unit Test Tests/assert-true.xqy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
Expand All @@ -22,10 +23,10 @@ declare function local:case4()

test:assert-true(fn:true()),
test:assert-true((fn:true(), fn:true())),
test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "ASSERT-TRUE-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case2")), "ASSERT-TRUE-FAILED"),
tlib:test-for-failure(local:case1(), "ASSERT-TRUE-FAILED"),
tlib:test-for-failure(local:case2(), "ASSERT-TRUE-FAILED"),

test:assert-true(fn:true(), "test"),
test:assert-true((fn:true(), fn:true()), "test"),
test:assert-throws-error(xdmp:function(xs:QName("local:case3")), "ASSERT-TRUE-FAILED"),
test:assert-throws-error(xdmp:function(xs:QName("local:case4")), "ASSERT-TRUE-FAILED")
tlib:test-for-failure(local:case3(), "ASSERT-TRUE-FAILED"),
tlib:test-for-failure(local:case4(), "ASSERT-TRUE-FAILED")
3 changes: 2 additions & 1 deletion src/test/suites/Unit Test Tests/fail.xqy
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";
import module namespace tlib = "http://marklogic.com/roxy/unit-test-tests" at "lib/testing-lib.xqy";

declare function local:case1()
{
test:fail('i failed')
};

test:assert-throws-error(xdmp:function(xs:QName("local:case1")), "USER-FAIL")
tlib:test-for-failure(local:case1(), "USER-FAIL")
20 changes: 20 additions & 0 deletions src/test/suites/Unit Test Tests/lib/testing-lib.xqy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
xquery version "1.0-ml";

module namespace tlib = "http://marklogic.com/roxy/unit-test-tests";

import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/test-helper.xqy";

declare option xdmp:mapping "false";

(:
: Call a function and verify that a function generates the failure XML.
:)
declare function tlib:test-for-failure($actual, $error-name as xs:string?)
{
test:assert-equal("{http://marklogic.com/roxy/test}result", xdmp:key-from-QName(fn:node-name($actual))),
test:assert-equal("fail", $actual/@type/fn:string()),
if ($error-name) then
test:assert-equal($error-name, $actual/error:error/error:name/fn:string())
else ()
};

52 changes: 28 additions & 24 deletions src/test/test-helper.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -184,36 +184,37 @@ declare function helper:fail($message as item()*) {
typeswitch($message)
case element(error:error) return $message
default return
fn:error(xs:QName("USER-FAIL"), $message)
(: Get the stack trace :)
try { fn:error(xs:QName("USER-FAIL"), $message) } catch ($e) { $e }
}
};

declare function helper:assert-all-exist($count as xs:unsignedInt, $item as item()*) {
if ($count eq fn:count($item)) then
helper:success()
else
fn:error(xs:QName("ASSERT-ALL-EXIST-FAILED"), "Assert All Exist failed", $item)
helper:stack-trace("ASSERT-ALL-EXIST-FAILED", "Assert All Exist failed", $item)
};

declare function helper:assert-exists($item as item()*) {
if (fn:exists($item)) then
helper:success()
else
fn:error(xs:QName("ASSERT-EXISTS-FAILED"), "Assert Exists failed", $item)
helper:stack-trace("ASSERT-EXISTS-FAILED", "Assert Exists failed", $item)
};

declare function helper:assert-not-exists($item as item()*) {
if (fn:not(fn:exists($item))) then
helper:success()
else
fn:error(xs:QName("ASSERT-NOT-EXISTS-FAILED"), "Assert Not Exists failed", $item)
helper:stack-trace("ASSERT-NOT-EXISTS-FAILED", "Assert Not Exists failed", $item)
};

declare function helper:assert-at-least-one-equal($expected as item()*, $actual as item()*) {
if ($expected = $actual) then
helper:success()
else
fn:error(xs:QName("ASSERT-AT-LEAST-ONE-EQUAL-FAILED"), "Assert At Least one Equal failed", ())
helper:stack-trace("ASSERT-AT-LEAST-ONE-EQUAL-FAILED", "Assert At Least one Equal failed", ())
};

declare private function helper:are-these-equal($expected as item()*, $actual as item()*) {
Expand Down Expand Up @@ -244,24 +245,21 @@ declare function helper:assert-equal($expected as item()*, $actual as item()*) {
if (helper:are-these-equal($expected, $actual)) then
helper:success()
else
fn:error(xs:QName("ASSERT-EQUAL-FAILED"), "Assert Equal failed", ($expected, $actual))
helper:stack-trace("ASSERT-EQUAL-FAILED", "Assert Equal failed", ($expected, $actual))
};

declare function helper:assert-equal($expected as item()*, $actual as item()*, $error-object as item()*) {
if (helper:are-these-equal($expected, $actual)) then
helper:success()
else
fn:error(xs:QName("ASSERT-EQUAL-FAILED"), "Assert Equal failed", ($expected, $actual, " : ", $error-object))
helper:stack-trace("ASSERT-EQUAL-FAILED", "Assert Equal failed", ($expected, $actual, " : ", $error-object))
};

declare function helper:assert-not-equal($expected as item()*, $actual as item()*) {
if (fn:not(helper:are-these-equal($expected, $actual))) then
helper:success()
else
fn:error(
xs:QName("ASSERT-NOT-EQUAL-FAILED"),
fn:concat("test name", ": Assert Not Equal failed"),
($expected, $actual))
helper:stack-trace("ASSERT-NOT-EQUAL-FAILED", "test name: Assert Not Equal failed", ($expected, $actual))
};

declare function helper:assert-equal-xml($expected, $actual) {
Expand Down Expand Up @@ -346,7 +344,7 @@ declare function helper:assert-equal-json($expected, $actual) {
default return
helper:assert-true(fn:false(), ("type mismatch ($expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual), ")"))
default return
fn:error(xs:QName("INVALID-ARG"), "Unsupported JSON type.")
helper:stack-trace("INVALID-ARG", "Unsupported JSON type.", ())
};

declare function helper:assert-true($supposed-truths as xs:boolean*) {
Expand All @@ -355,14 +353,14 @@ declare function helper:assert-true($supposed-truths as xs:boolean*) {

declare function helper:assert-true($supposed-truths as xs:boolean*, $msg as item()*) {
if (fn:false() = $supposed-truths) then
fn:error(xs:QName("ASSERT-TRUE-FAILED"), "Assert True failed", $msg)
helper:stack-trace("ASSERT-TRUE-FAILED", "Assert True failed", $msg)
else
helper:success()
};

declare function helper:assert-false($supposed-falsehoods as xs:boolean*) {
if (fn:true() = $supposed-falsehoods) then
fn:error(xs:QName("ASSERT-FALSE-FAILED"), "Assert False failed", $supposed-falsehoods)
helper:stack-trace("ASSERT-FALSE-FAILED", "Assert False failed", $supposed-falsehoods)
else
helper:success()
};
Expand All @@ -372,20 +370,14 @@ declare function helper:assert-meets-minimum-threshold($expected as xs:decimal,
if (every $i in 1 to fn:count($actual) satisfies $actual[$i] ge $expected) then
helper:success()
else
fn:error(
xs:QName("ASSERT-MEETS-MINIMUM-THRESHOLD-FAILED"),
fn:concat("test name", ": Assert Meets Minimum Threshold failed"),
($expected, $actual))
helper:stack-trace("ASSERT-MEETS-MINIMUM-THRESHOLD-FAILED", "test name: Assert Meets Minimum Threshold failed", ($expected, $actual))
};

declare function helper:assert-meets-maximum-threshold($expected as xs:decimal, $actual as xs:decimal+) {
if (every $i in 1 to fn:count($actual) satisfies $actual[$i] le $expected) then
helper:success()
else
fn:error(
xs:QName("ASSERT-MEETS-MAXIMUM-THRESHOLD-FAILED"),
fn:concat("test name", ": Assert Meets Maximum Threshold failed"),
($expected, $actual))
helper:stack-trace("ASSERT-MEETS-MAXIMUM-THRESHOLD-FAILED", "test name: Assert Meets Maximum Threshold failed", ($expected, $actual))
};

declare function helper:assert-throws-error($function as xdmp:function)
Expand Down Expand Up @@ -549,8 +541,7 @@ declare function helper:sleep($msec as xs:unsignedInt) as empty-sequence() {

declare function helper:log($items as item()*)
{
let $_ := fn:trace($items, "UNIT-TEST")
return ()
fn:trace($items, "UNIT-TEST")
};

declare function helper:list-from-database(
Expand Down Expand Up @@ -606,3 +597,16 @@ declare function helper:remove-modules-directories($dirs as xs:string*)
</options>)
else ()
};

(:
: Throw an exception, capture the stack trace.
:)
declare private function helper:stack-trace($error as xs:string, $description as xs:string, $data as item()*)
{
try {
fn:error(xs:QName($error), $description, $data)
}
catch ($e) {
helper:fail($e)
}
};

0 comments on commit 4b31f64

Please sign in to comment.