Skip to content

Instantly share code, notes, and snippets.

@samth
Last active August 29, 2015 14:11
Show Gist options
  • Save samth/e66e12d87bc101bd691b to your computer and use it in GitHub Desktop.
Save samth/e66e12d87bc101bd691b to your computer and use it in GitHub Desktop.
(module+ test
(require rackunit)
(let ([ip (open-input-string "2 + 3")])
(define (ts type tok)
(token-struct type tok #f #f #f #f #f))
(define (pt val from to)
(position-token val (position from #f #f) (position to #f #f)))
(define (check-lex1 type tok a b)
(check-equal? (smalltalk/lex ip)
(pt (ts type tok) a b)))
(define (check-lex2 type tok a b c)
(check-equal? (smalltalk/lex ip)
(pt (pt (ts type tok) b c) a b)))
(check-lex1 'NUM "2" 1 2)
(check-lex2 'BIN "+" 2 3 4)
(check-lex2 'NUM "3" 4 5 6)))
(module+ test
(require rackunit
racket/match)
(define (check-lex input . expecteds)
(let ([ip (open-input-string input)]
[from2 #f]
[to2 #f])
(for ([expected expecteds])
(match expected
[`(#f ,n ,m)
(set! from2 n)
(set! to2 m)]
[`(,type ,tok ,from ,to)
(if from2
(begin
(check-equal? (smalltalk/lex ip)
(pt (pt (ts type tok) from to) from2 to2))
(set! from2 #f)
(set! to2 #f))
(check-equal? (smalltalk/lex ip)
(pt (ts type tok) from to)))]))))
(define (ws n m) (list #f n m))
(define (tk a b c d) (list a b c d))
(check-lex "2 + 3"
(tk 'NUM "2" 1 2)
(ws 2 3)
(tk 'BIN "+" 3 4)
(ws 4 5)
(tk 'NUM "3" 5 6)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment