test-ids.rkt (2091B)
1 #lang racket 2 3 (require "../typed-untyped.rkt") 4 (define-typed/untyped-test-module 5 (require-typed/untyped phc-toolkit/ids) 6 (require-typed/untyped phc-toolkit/typed-rackunit) 7 (require (for-syntax racket/syntax 8 phc-toolkit/untyped/ids)) 9 10 (check-equal?: (format-ids #'a "~a-~a" #'() #'()) 11 '()) 12 13 (check-equal?: (map syntax->datum 14 (format-ids #'a "~a-~a" #'(x1 x2 x3) #'(a b c))) 15 '(x1-a x2-b x3-c)) 16 17 ;; Since the presence of "Syntax" in the parameters list makes format-ids 18 ;; require a chaperone contract instead of a flat contract, we can't run the 19 ;; two tests below directly, we would need to require the untyped version of 20 ;; this file, which causes a cycle in loading. 21 22 (define-syntax (test1 stx) 23 (syntax-case stx () 24 [(_ (let1 d1) x y) 25 (begin 26 (define/with-syntax (foo-x foo-y) 27 (format-ids (λ (xy) 28 (if (string=? (symbol->string (syntax->datum xy)) 29 "b") 30 stx 31 #'())) 32 "foo-~a" 33 #'(x y))) 34 #'(let1 d1 (let ((foo-b 2) (foo-c 'b)) (cons foo-x foo-y))))])) 35 36 (check-equal?: (test1 (let ((foo-b 1) (foo-c 'a))) b c) 37 '(1 . b)) 38 39 (define-syntax (fubar stx) 40 (define/with-syntax (v1 ...) #'(1 2 3)) 41 (define/with-syntax (v2 ...) #'('a 'b 'c)) 42 ;; the resulting ab and ab should be distinct identifiers: 43 (define/with-syntax (id1 ...) (format-temp-ids "~a" #'(ab cd ab))) 44 (define/with-syntax (id2 ...) (format-temp-ids "~a" #'(ab cd ab))) 45 #'(let ([id1 v1] ...) 46 (let ([id2 v2] ...) 47 (list (cons id1 id2) ...)))) 48 49 (check-equal?: (fubar) '((1 . a) (2 . b) (3 . c))) 50 51 (define-syntax (test-concise stx) 52 (syntax-case stx () 53 [(_ a ...) 54 (let () 55 (define-temp-ids #:concise "~a!" (a ...)) 56 #''(a! ...))])) 57 (check-equal? (test-concise one "two" 3) 58 '(one! two! 3!)))