use hasheq rather than hash

dev-fixit
Matthew Butterick 9 years ago
parent 610666cbce
commit 65e96c8232

@ -209,7 +209,7 @@
(define+provide+safe (attrs->hash . items-in) (define+provide+safe (attrs->hash . items-in)
(() #:rest (listof can-be-txexpr-attrs?) . ->* . hash?) (() #:rest (listof can-be-txexpr-attrs?) . ->* . hash-eq?)
;; can be liberal with input because they're all just nested key/value pairs ;; can be liberal with input because they're all just nested key/value pairs
;; but still need this function to make sure that 'foo and "foo" are treated as the same hash key ;; but still need this function to make sure that 'foo and "foo" are treated as the same hash key
(define items (reverse (define items (reverse
@ -225,11 +225,11 @@
[value (->txexpr-attr-value (cadr items))] [value (->txexpr-attr-value (cadr items))]
[rest (cddr items)]) [rest (cddr items)])
(cons (cons key value) (make-key-value-list rest))))) (cons (cons key value) (make-key-value-list rest)))))
(make-immutable-hash (make-key-value-list items))) (make-immutable-hasheq (make-key-value-list items)))
(define+provide+safe (hash->attrs attr-hash) (define+provide+safe (hash->attrs attr-hash)
(hash? . -> . txexpr-attrs?) (hash-eq? . -> . txexpr-attrs?)
(map (λ(k) (list k (hash-ref attr-hash k))) (hash-keys attr-hash))) (map (λ(k) (list k (hash-ref attr-hash k))) (hash-keys attr-hash)))

@ -306,11 +306,11 @@ Predicate for functions that handle @racket[_txexpr-attrs]. Covers values that a
@deftogether[( @deftogether[(
@defproc[ @defproc[
(attrs->hash [x can-be-txexpr-attrs?] ...) (attrs->hash [x can-be-txexpr-attrs?] ...)
hash?] hash-eq?]
@defproc[ @defproc[
(hash->attrs (hash->attrs
[h hash?]) [h hash-eq?])
txexpr-attrs?] txexpr-attrs?]
)] )]
@ -319,7 +319,7 @@ Convert @racket[_attrs] to an immutable hash, and back again.
@examples[#:eval my-eval @examples[#:eval my-eval
(define tx '(div [[id "top"][class "red"]] "Hello" (p "World"))) (define tx '(div [[id "top"][class "red"]] "Hello" (p "World")))
(attrs->hash (get-attrs tx)) (attrs->hash (get-attrs tx))
(hash->attrs '#hash((class . "red") (id . "top"))) (hash->attrs '#hasheq((class . "red") (id . "top")))
] ]
@defproc[ @defproc[

@ -1,7 +1,8 @@
#lang racket/base #lang racket/base
(require (for-syntax racket/base racket/syntax)) (require (for-syntax racket/base racket/syntax))
(define-syntax (eval-as-untyped stx) ;; use a separate test file to avoid cycle in loading
(define-syntax (test-safe-and-unsafe stx)
(syntax-case stx () (syntax-case stx ()
[(_ exprs ...) [(_ exprs ...)
(with-syntax ([sym (syntax-e (generate-temporary))] (with-syntax ([sym (syntax-e (generate-temporary))]
@ -9,17 +10,21 @@
(datum->syntax stx `(begin (datum->syntax stx `(begin
(module ,(syntax->datum #'sym) racket (module ,(syntax->datum #'sym) racket
(require rackunit "main.rkt") (require rackunit "main.rkt")
(define-syntax (values->list stx)
(syntax-case stx ()
[(_ values-expr) #'(call-with-values (λ () values-expr) list)]))
,@(syntax->datum #'(exprs ...))) ,@(syntax->datum #'(exprs ...)))
(require ',(syntax->datum #'sym)) (require ',(syntax->datum #'sym))
(module ,(syntax->datum #'sym2) racket (module ,(syntax->datum #'sym2) racket
(require rackunit (submod "main.rkt" safe)) (require rackunit (submod "main.rkt" safe))
,@(syntax->datum #'(exprs ...)))
(require ',(syntax->datum #'sym2))) stx))]))
(eval-as-untyped
(define-syntax (values->list stx) (define-syntax (values->list stx)
(syntax-case stx () (syntax-case stx ()
[(_ values-expr) #'(call-with-values (λ () values-expr) list)])) [(_ values-expr) #'(call-with-values (λ () values-expr) list)]))
,@(syntax->datum #'(exprs ...)))
(require ',(syntax->datum #'sym2))) stx))]))
(test-safe-and-unsafe
(check-true (txexpr-tag? 'foo)) (check-true (txexpr-tag? 'foo))
(check-false (txexpr-tag? "foo")) (check-false (txexpr-tag? "foo"))
(check-false (txexpr-tag? 3)) (check-false (txexpr-tag? 3))
@ -115,11 +120,11 @@
(check-equal? (->txexpr-attr-value "foo") "foo") (check-equal? (->txexpr-attr-value "foo") "foo")
(check-equal? (->txexpr-attr-value 'foo) "foo") (check-equal? (->txexpr-attr-value 'foo) "foo")
(check-equal? (attrs->hash '((foo "bar"))) '#hash((foo . "bar"))) (check-equal? (attrs->hash '((foo "bar"))) '#hasheq((foo . "bar")))
(check-equal? (attrs->hash '((foo "bar")) '(foo "fraw")) '#hash((foo . "fraw"))) (check-equal? (attrs->hash '((foo "bar")) '(foo "fraw")) '#hasheq((foo . "fraw")))
(check-equal? (attrs->hash '((foo "bar")) '(foo "fraw") 'foo "dog") '#hash((foo . "dog"))) (check-equal? (attrs->hash '((foo "bar")) '(foo "fraw") 'foo "dog") '#hasheq((foo . "dog")))
(check-equal? (apply set (hash->attrs '#hash((foo . "bar")(hee . "haw")))) (check-equal? (apply set (hash->attrs '#hasheq((foo . "bar")(hee . "haw"))))
(apply set '((foo "bar")(hee "haw")))) (apply set '((foo "bar")(hee "haw"))))
(check-equal? (attr-ref '(p ((foo "bar"))) 'foo) "bar") (check-equal? (attr-ref '(p ((foo "bar"))) 'foo) "bar")

Loading…
Cancel
Save