Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing JavaScript functions to assert-throws-error #123

Open
dmcassel opened this issue Mar 10, 2020 · 1 comment
Open

Support passing JavaScript functions to assert-throws-error #123

dmcassel opened this issue Mar 10, 2020 · 1 comment

Comments

@dmcassel
Copy link
Collaborator

See Stack Overflow question.

test:assert-throws-error requires an xdmp:function, which isn't what you get from JavaScript.

@dmcassel
Copy link
Collaborator Author

Implementation notes: if we got rid of the typed parameter for test:assert-throws-error, we could check for XQuery functions like this:

$function instance of xdmp:function

Not sure how to check whether the parameter is a JS function. I think the most straightforward solution, in combination with #111, is to just do something like:

declare function test:assert-throws-error($function, $error-code as xs:string?)
{
  try {
    $function(),
    fn:error(xs:QName("ASSERT-THROWS-ERROR-FAILED"), $message-prefix || "Did not throw an error")
  } catch ($ex) {
      if ($ex/error:name eq "ASSERT-THROWS-ERROR-FAILED") then
        xdmp:rethrow()
      else if ($error-code) then
        if ($ex/error:code eq $error-code or $ex/error:name eq $error-code) then
          if ($expected-error-message and fn:not($ex/error:message/text() eq $expected-error-message)) then (
            xdmp:log($ex, "info"),
            fn:error(xs:QName("ASSERT-THROWS-ERROR-FAILED"), $message-prefix
              || "Found expected error code, but not expected message:" || " expected: [" || $expected-error-message
              || "] actual: [" || $ex/error:message/fn:string() || "]")
          )
          else
            test:success()
        else (
          xdmp:log($ex, "info"),
          fn:error(xs:QName("ASSERT-THROWS-ERROR-FAILED"), $message-prefix
              || "Did not find expected error code: expected: [" || $error-code || "] actual: ["
              || $ex/error:name/fn:string() || "]")
        )
      else
        test:success()
  }
};

Any function passed in that has multiple parameters will go to the old functions. Any function that has no parameters should just work. If someone passes in a non-function as $function, they will get an XDMP-NOTFUNC exception.

If this approach looks good to others, I'll put together a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant