A convenient assertion macro - with one caveat

21 views
Skip to first unread message

Dexter Lagan

unread,
May 7, 2020, 5:22:29 AM5/7/20
to Racket Users
Hi,

  I made a simple macro which saves me the trouble of defining a test module, requiring RackUnit and then declaring '(module+ test' after each procedure definition, as I like to keep unit tests close by. The repo :


  Here's the macro, apologies for the broken formatting :

(define-syntax (assert stx)
(syntax-parse stx
[(_ ?a ?b)
#'(module+ test
(require rackunit)
(check-equal? ?a ?b #'?a))]
[(_ ?a)
#'(module+ test
(require rackunit)
(check-true ?a #'?a))]))

The macro works great, and I was able to pipe through an assertion message through rackunit's check-equal? and other procedures.
I have one question however: would there be a way to make DrRacket point to the failed assertion line instead of the macro itself when a failure occurs? Here's a screenshot of how it looks like at the moment:

assert.PNG

  It would be even better if DrRacket highlighted the (assert ...) line itself, but I'm asking a bit much :) If assert is an actual module, I'm not sure where DrRacket would then point at.
I also have doubts about whenever Racket will remove the test module once compiled. Since macros are processed at compile-time, I'm guessing it would remove the test module right after, but let me know if you have a more definite answer.

Dex

Laurent

unread,
May 7, 2020, 5:41:07 AM5/7/20
to Dexter Lagan, Racket Users
Check out `make-check-location` and friends, and maybe `with-check-info*`

You may have to combine with the syntax-information extracted from the syntax object `stx`.


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CACUENrLX%3D9ix2kv9MNpp3nNoSN%3DsWBx%2BFTRdwNyNA7EiVWQ0mA%40mail.gmail.com.

Jens Axel Søgaard

unread,
May 7, 2020, 5:51:41 AM5/7/20
to Dexter Lagan, Racket Users
You can use syntax/loc to give a piece of syntax location information.

#lang racket
(require (for-syntax syntax/parse))


(define-syntax (assert stx)
  (syntax-parse stx
    [(_assert ?a ?b)
     (quasisyntax/loc stx
       (module+ test
         (require rackunit)
         #,(syntax/loc stx (check-equal? ?a ?b #'?a))))]
    [(_assert ?a)
     (quasisyntax/loc stx
       (module+ test
         (require rackunit)
         #,(syntax/loc stx (check-true ?a #'?a))))]))


/Jens Axel
Racket Stories
https://racket-stories.com

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CACUENrLX%3D9ix2kv9MNpp3nNoSN%3DsWBx%2BFTRdwNyNA7EiVWQ0mA%40mail.gmail.com.


--
--
Jens Axel Søgaard

Dexter Lagan

unread,
May 7, 2020, 6:09:14 AM5/7/20
to Jens Axel Søgaard, Racket Users
Awesome! Thanks. Racket is freaking amazing.

Dex

On May 7, 2020, at 11:51 AM, Jens Axel Søgaard <jens...@soegaard.net> wrote:


<assert.PNG>


  It would be even better if DrRacket highlighted the (assert ...) line itself, but I'm asking a bit much :) If assert is an actual module, I'm not sure where DrRacket would then point at.
I also have doubts about whenever Racket will remove the test module once compiled. Since macros are processed at compile-time, I'm guessing it would remove the test module right after, but let me know if you have a more definite answer.

Dex

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CACUENrLX%3D9ix2kv9MNpp3nNoSN%3DsWBx%2BFTRdwNyNA7EiVWQ0mA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages