From 7fe9a04b6d57debafc881c8ced56dafc951d77cc Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 10 Dec 2015 12:28:07 -0800 Subject: [PATCH] add `check-txexprs-equal?` --- main.rkt | 25 ++++++++++++++++++++----- scribblings/txexpr.scrbl | 31 +++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/main.rkt b/main.rkt index dd6576c..1dd028a 100644 --- a/main.rkt +++ b/main.rkt @@ -50,7 +50,7 @@ (define+provide+safe (txexpr-attrs? x) predicate/c - (and (list? x) (andmap txexpr-attr? x))) + (and (list? x) (andmap txexpr-attr? x))) (define+provide+safe (txexpr-element? x) @@ -60,7 +60,7 @@ (define+provide+safe (txexpr-elements? x) predicate/c - (and (list? x) (andmap txexpr-element? x))) + (and (list? x) (andmap txexpr-element? x))) (define+provide+safe (txexpr-attr-key? x) @@ -245,8 +245,8 @@ (define+provide+safe (attr-join tx key value) (txexpr? can-be-txexpr-attr-key? can-be-txexpr-attr-value? . -> . txexpr?) (define starting-values (string-split (if (attrs-have-key? tx key) - (attr-ref tx key) - ""))) + (attr-ref tx key) + ""))) (attr-set tx key (string-join `(,@starting-values ,value) " "))) @@ -340,4 +340,19 @@ [(txexpr? x) (if (member (get-tag x) '(script style)) (make-txexpr (get-tag x) (get-attrs x) (map ->cdata (get-elements x))) (make-txexpr (get-tag x) (get-attrs x) (map loop (get-elements x))))] - [else x])))) \ No newline at end of file + [else x])))) + +(require rackunit) +(provide+safe check-txexprs-equal?) +(define-simple-check (check-txexprs-equal? tx1 tx2) + ;; txexprs are deemed equal if they differ only in the ordering of attributes. + ;; therefore, to check them, 1) sort their attributes, 2) straight list comparison. + (define symbolstring syms)))) + (define (sort-attrs x) + (if (txexpr? x) + (let-values ([(tag attr elements) (txexpr->values x)]) + (make-txexpr tag (sort attr #:key car symbol