Re: [racket-users] How to make unit test for error "unbound identifier"?

2017-04-26 Thread Alexis King
You can use convert-syntax-error from syntax/macro-testing to convert
the syntax error to a runtime error, which can be caught by RackUnit:

http://docs.racket-lang.org/syntax/macro-testing.html#%28form._%28%28lib._syntax%2Fmacro-testing..rkt%29._convert-syntax-error%29%29

Alexis

> On Apr 25, 2017, at 23:18, Jinzhou Zhang  wrote:
> 
> Hi there,
> 
> I am writing a simple macro `if-let` that tried to bind the condition to a 
> variable.
> 
> ```
> (define-simple-macro (if-let (~describe "binding pairs" [binding:expr 
> value:expr])
> (~describe "\"then\" clause" then:expr)
> (~describe "\"else\" clause" else:expr))
>  (let ([val value])
>(if val (match-let ([binding val]) then) else)))
> ```
> 
> I finished the macro and start to write unit tests for it. Then I realized 
> that
> the "binding" should not working in "else" statement. That means:
> 
> ```
> (if-let [it #f]
>  'true
>  it   ; <- should report "unbound identifier".
> )
> ```
> 
> However when I tried the following code:
> 
> ```
> (check-exn exn:fail?
>   (lambda ()
> (if-let [it #f]
> (fail "if-let: wrong-branch")
> it)))
> 
> ```
> 
> It gives error:
> 
> ```
> it: unbound identifier in module
> ```
> 
> So the question is how to catch the "unbound identifier" exception for unit 
> test?
> 
> Thanks!

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] How to make unit test for error "unbound identifier"?

2017-04-26 Thread Jinzhou Zhang
Hi there,

I am writing a simple macro `if-let` that tried to bind the condition to a 
variable.

```
(define-simple-macro (if-let (~describe "binding pairs" [binding:expr 
value:expr])
 (~describe "\"then\" clause" then:expr)
 (~describe "\"else\" clause" else:expr))
  (let ([val value])
(if val (match-let ([binding val]) then) else)))
```

I finished the macro and start to write unit tests for it. Then I realized that
the "binding" should not working in "else" statement. That means:

```
(if-let [it #f]
  'true
  it   ; <- should report "unbound identifier".
)
```

However when I tried the following code:

```
(check-exn exn:fail?
   (lambda ()
 (if-let [it #f]
 (fail "if-let: wrong-branch")
 it)))

```

It gives error:

```
 it: unbound identifier in module
```

So the question is how to catch the "unbound identifier" exception for unit 
test?

Thanks!

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.