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

Custom assertions #63

Open
waywardmonkeys opened this issue Apr 23, 2015 · 1 comment
Open

Custom assertions #63

waywardmonkeys opened this issue Apr 23, 2015 · 1 comment

Comments

@waywardmonkeys
Copy link
Member

It is reasonable for users of testworks to want to be able to create their own assetions so that they can perform a custom check and, if it fails, a nice error message that is informative.

This would replace something like:

define function static-type-check?(lambda        :: <&method>,
                                   expected-type :: <type-estimate-values>)
  => (stc :: <boolean>)
  // Useful thing to put in the body of a test: infer the return values
  // of lambda, and ask if they match expected-type.
  local method final-computation-type(c :: <&method>)
          // What is the type of the final computation?  (I.e., the return.)
          let cache = make(<type-cache>);
          type-estimate-in-cache(c, cache);                         // fill cache w/types
          type-estimate-in-cache(final-computation(body(c)), cache) // just the last guy
        end;
  let found-type = final-computation-type(lambda);
  if (type-estimate=?(expected-type, found-type))
    #t
  else
    when (*static-type-check?-verbose?*)
      // Sometimes you want a diagnostic for the failure cases.
      dynamic-bind (*print-method-bodies?* = #t)
        format-out("\nFor %=:\nExpected type: %=\n  Inferred type: %=\n\n",
                   lambda, expected-type, found-type)
      end
    end;
    #f
  end
end;

define macro typist-inference-test-definer
  // Define manual compiler test & remember the name in typist inference registry
  { define typist-inference-test ?test-name:name
      ?subtests
    end }
  => {
       define test ?test-name ()
         with-testing-context (#f)
           ?subtests
         end
       end }
subtests:
  // ;-separated test specs expand into a conjunction of test results
  { }               => { }
  { ?subtest; ... } => { ?subtest ... }
subtest:
  // Wrap with try ... end and hand off to static-type-check? to match
  // against the values specification.
  { } => { }
  { ?:expression TYPE: ?val:* }
    => { assert-true(static-type-check?(compile-to-method(?expression),
                                        make(<type-estimate-values>, ?val))); }
end;

We don't want to just know if the test was returning #t or #f. We want to know if one value as type-estimate=? with the other, and if not, a useful error message that gives both values.

@cgay
Copy link
Member

cgay commented Nov 27, 2018

I think there's nothing preventing someone from writing their own assert-type-estimate= macro that does the complicated test and then ends up calling either assert-true(#t) or assert-true(#f, "what the test was and the reason it failed"). Am I misunderstanding the request?

(We should definitely make a fail("reason") function if we don't have one already.)

@cgay cgay added this to the Prioritization Test milestone Dec 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants