Skip to content

Commit

Permalink
fix the sense of the blame aspects of the blame object has a #:important
Browse files Browse the repository at this point in the history
closes PR 13692
  • Loading branch information
rfindler committed Apr 13, 2013
1 parent ab66b48 commit 79955e1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
38 changes: 25 additions & 13 deletions collects/racket/contract/private/blame.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,19 @@
make-blame))

;; s : (or/c string? #f)
(define (blame-add-context b s #:important [important #f] #:swap? [swap? #f])
(define (blame-add-context b s #:important [name #f] #:swap? [swap? #f])
(define new-original? (if swap? (not (blame-original? b)) (blame-original? b)))
(struct-copy
blame b
[original? (if swap? (not (blame-original? b)) (blame-original? b))]
[original? new-original?]
[positive (if swap? (blame-negative b) (blame-positive b))]
[negative (if swap? (blame-positive b) (blame-negative b))]
[important (or important (blame-important b))]
[important (if name (important name new-original?) (blame-important b))]
[context (if s (cons s (blame-context b)) (blame-context b))]
[top-known? #t]))

(struct important (name sense-swapped?))

(define (blame-add-unknown-context b)
(define old (blame-context b))
(struct-copy
Expand Down Expand Up @@ -157,17 +160,17 @@
(define nxt
(cond
[(eq? 'given: fst) (add-indent
(if (blame-original? blame)
(if (blame/important-original? blame)
"produced:"
"given:"))]
[(eq? 'given fst) (if (blame-original? blame)
[(eq? 'given fst) (if (blame/important-original? blame)
"produced"
"given")]
[(eq? 'expected: fst) (add-indent
(if (blame-original? blame)
(if (blame/important-original? blame)
"promised:"
"expected:"))]
[(eq? 'expected fst) (if (blame-original? blame)
[(eq? 'expected fst) (if (blame/important-original? blame)
"promised"
"expected")]
[else fst]))
Expand All @@ -180,6 +183,12 @@
new-so-far
(regexp-match #rx" $" nxt))]))]))

(define (blame/important-original? blame)
(define i (blame-important blame))
(cond
[i (important-sense-swapped? i)]
[else (blame-original? blame)]))

(define (default-blame-format blme x custom-message)
(define source-message (source-location->string (blame-source blme)))

Expand All @@ -198,18 +207,21 @@
#f
(format " at: ~a" source-message)))

(define self-or-not (if (blame-original? blme)
"broke its contract"
"contract violation"))
(define (self-or-not which-way?)
(if which-way?
"broke its contract"
"contract violation"))

(define start-of-message
(cond
[(blame-important blme)
(format "~a: ~a" (blame-important blme) self-or-not)]
(format "~a: ~a"
(important-name (blame-important blme))
(self-or-not (important-sense-swapped? (blame-important blme))))]
[(blame-value blme)
(format "~a: ~a" (blame-value blme) self-or-not)]
(format "~a: ~a" (blame-value blme) (self-or-not (blame-original? blme)))]
[else
(format "~a:" self-or-not)]))
(format "~a:" (self-or-not (blame-original? blme)))]))

(define blame-parties (blame-positive blme))
(define blaming-line
Expand Down
24 changes: 19 additions & 5 deletions collects/tests/racket/contract-test.rktl
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@

(define (contract-syntax-error-test name exp [reg #rx""])
(test #t
name
(contract-eval `(with-handlers ((exn:fail:syntax?
(lambda (x) (and (regexp-match ,reg (exn-message x)) #t))))
(eval ',exp)))))
name
(contract-eval `(with-handlers ((exn:fail:syntax?
(lambda (x) (and (regexp-match ,reg (exn-message x)) #t))))
(eval ',exp)))))

;; test/spec-passed : symbol sexp -> void
;; tests a passing specification
Expand Down Expand Up @@ -155,7 +155,7 @@
(define (has-proper-blame? msg)
(define reg
(cond
[(eq? blame 'pos) #rx"broke its contract[\n:,].*blaming: pos"]
[(eq? blame 'pos) #rx"blaming: pos"]
[(eq? blame 'neg) #rx"blaming: neg"]
[(string? blame) (string-append "blaming: " (regexp-quote blame))]
[else #f]))
Expand Down Expand Up @@ -8741,6 +8741,20 @@
'pos
'neg)])
(send (new cls%) m 3 #t)))

(contract-error-test
'class/c-tl-message
'((contract (-> (class/c (callback (->m boolean? any)))
any)
(λ (c%) (send (new c%) callback 1))
'pos 'neg)
(class object%
(super-new)
(define/public (callback x) 3)))
(λ (exn) (and (regexp-match? #rx"callback: contract violation" (exn-message exn))
(regexp-match? #rx"expected: boolean[?]" (exn-message exn))
(regexp-match? #rx"given: 1" (exn-message exn)))))


;
;
Expand Down

0 comments on commit 79955e1

Please sign in to comment.