Skip to content

Commit c2fa9d2

Browse files
committed
Make infer-tests give better errors.
1 parent 420bb0e commit c2fa9d2

File tree

1 file changed

+24
-15
lines changed
  • pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests

1 file changed

+24
-15
lines changed

pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/infer-tests.rkt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
rackunit
55
racket/list
66
(for-syntax racket/base syntax/parse)
7+
syntax/location syntax/srcloc
78
(rep type-rep)
89
(r:infer infer)
910

@@ -21,8 +22,8 @@
2122

2223
(begin-for-syntax
2324
(define-splicing-syntax-class result
24-
(pattern (~seq) #:with v #'#f)
25-
(pattern (~seq #:result v:expr)))
25+
(pattern (~seq) #:with v #'#f #:with exp #'#f)
26+
(pattern (~seq #:result [v:expr exp:expr])))
2627
(define-splicing-syntax-class vars
2728
(pattern (~seq) #:with vars #'empty)
2829
(pattern (~seq #:vars vars:expr)))
@@ -36,21 +37,29 @@
3637

3738
(define-syntax (infer-t stx)
3839
(syntax-parse stx
39-
([_ S:expr T:expr R:result :vars :indices :pass]
40+
([_ S:expr T:expr . rest]
4041
(syntax/loc stx
41-
(test-case (format "~a ~a~a" S T (if pass "" " should fail"))
42-
(define result (infer vars indices (list S) (list T) R.v))
43-
(unless (if pass result (not result))
44-
(fail-check "Could not infer a substitution")))))))
42+
(infer-l (list S) (list T) . rest)))))
4543

4644
(define-syntax (infer-l stx)
4745
(syntax-parse stx
48-
([_ S:expr T:expr R:result :vars :indices :pass]
49-
(syntax/loc stx
46+
([_ S:expr T:expr :vars :indices R:result :pass]
47+
(quasisyntax/loc stx
5048
(test-case (format "~a ~a~a" S T (if pass "" " should fail"))
51-
(define result (infer vars indices S T R.v))
52-
(unless (if pass result (not result))
53-
(fail-check "Could not infer a substitution")))))))
49+
(with-check-info (['location (build-source-location-list (quote-srcloc #,stx))])
50+
(define substitution (infer vars indices S T R.v))
51+
(define result (and substitution R.v (subst-all substitution R.v)))
52+
(cond
53+
[pass
54+
(unless substitution
55+
(fail-check "Could not infer a substitution"))
56+
(when result
57+
(with-check-info (['actual result] ['expected R.exp])
58+
(unless (equal? result R.exp)
59+
(fail-check "Did not infer the expected result."))))]
60+
[fail
61+
(when substitution
62+
(fail-check "Inferred an unexpected substitution."))])))))))
5463

5564

5665
(define-syntax-rule (i2-t t1 t2 (a b) ...)
@@ -90,7 +99,7 @@
9099
(test-suite "Tests for infer"
91100
(infer-t Univ Univ)
92101
(infer-t (-v a) Univ)
93-
(infer-t (-v a) (-v a) #:result (-v a))
102+
(infer-t (-v a) (-v a) #:result [(-v a) (-v a)])
94103
(infer-t Univ (-v a) #:fail)
95104
(infer-t Univ (-v a) #:vars '(a))
96105
(infer-t (-v a) Univ #:vars '(a))
@@ -112,7 +121,7 @@
112121
(infer-t (make-ListDots (-v b) 'b) (make-ListDots Univ 'b) #:indices '(b))
113122
(infer-t (-pair (-v a) (make-ListDots (-v b) 'b))
114123
(-pair (-v a) (make-ListDots (-v b) 'b))
115-
#:result (-v a))
124+
#:result [(-v a) (-v a)])
116125

117126
[infer-t (->... null ((-v a) a) (-v b)) (-> -Symbol -String) #:vars '(b) #:indices '(a)]
118127
[infer-t (->... null ((-v a) a) (make-ListDots (-v a) 'a)) (-> -String -Symbol (-lst* -String -Symbol)) #:indices '(a)]
@@ -142,7 +151,7 @@
142151
[i2-t ((-v a) . -> . (-v b)) (-> N N) ('b N) ('a (Un))]
143152

144153

145-
[i2-l (list (-v a) (-v a) (-v b))
154+
[i2-l (list (-v a) (-v a) (-v b))
146155
(list (Un (-val 1) (-val 2)) N N)
147156
'(a b) ('b N) ('a N)]
148157
[i2-l (list (-> (-v a) Univ) (-lst (-v a)))

0 commit comments

Comments
 (0)