diff --git a/xenomorph/xenomorph/helper.rkt b/xenomorph/xenomorph/helper.rkt index 98d3ffbf..50484423 100644 --- a/xenomorph/xenomorph/helper.rkt +++ b/xenomorph/xenomorph/helper.rkt @@ -10,6 +10,11 @@ [(input-port? arg) arg] [else (raise-argument-error '->input-port "byte string or input port" arg)])) +(define (dict-ref* d . keys) + (for/fold ([d d]) + ([k (in-list keys)]) + (dict-ref d k))) + (define private-keys '(parent _startOffset _currentOffset _length)) (define (dict->mutable-hash x) (define h (make-hasheq)) diff --git a/xenomorph/xenomorph/test/pointer-test.rkt b/xenomorph/xenomorph/test/pointer-test.rkt index 5f7fe1f8..3e1d319f 100644 --- a/xenomorph/xenomorph/test/pointer-test.rkt +++ b/xenomorph/xenomorph/test/pointer-test.rkt @@ -72,13 +72,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee "size should add to parent pointerSize" (let ([parent (mhash 'parent (mhash 'pointerSize 0))]) (check-equal? (size (+xpointer #:relative-to 'parent) 10 #:parent parent) 1) - (check-equal? (dict-ref (dict-ref parent 'parent) 'pointerSize) 1))) + (check-equal? (dict-ref* parent 'parent 'pointerSize) 1))) (test-case "size should add to global pointerSize" (let ([parent (mhash 'parent (mhash 'parent (mhash 'parent (mhash 'pointerSize 0))))]) (check-equal? (size (+xpointer #:relative-to 'global) 10 #:parent parent) 1) - (check-equal? (dict-ref (dict-ref (dict-ref (dict-ref parent 'parent) 'parent) 'parent) 'pointerSize) 1))) + (check-equal? (dict-ref* parent 'parent 'parent 'parent 'pointerSize) 1))) (test-case "size should handle void pointers" @@ -142,8 +142,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee 'pointerOffset 5 'pointers null))) (encode (+xpointer #:relative-to 'parent) 10 #:parent parent) - (check-equal? (dict-ref (dict-ref parent 'parent) 'pointerOffset) 6) - (check-equal? (dict-ref (dict-ref parent 'parent) 'pointers) (list (mhasheq 'type uint8 + (check-equal? (dict-ref* parent 'parent 'pointerOffset) 6) + (check-equal? (dict-ref* parent 'parent 'pointers) (list (mhasheq 'type uint8 'val 10 'parent parent))) (check-equal? (get-output-bytes (current-output-port)) (bytes 2)))) @@ -158,8 +158,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee 'pointerOffset 5 'pointers null))))) (encode (+xpointer #:relative-to 'global) 10 #:parent parent) - (check-equal? (dict-ref (dict-ref (dict-ref (dict-ref parent 'parent) 'parent) 'parent) 'pointerOffset) 6) - (check-equal? (dict-ref (dict-ref (dict-ref (dict-ref parent 'parent) 'parent) 'parent) 'pointers) + (check-equal? (dict-ref* parent 'parent 'parent 'parent 'pointerOffset) 6) + (check-equal? (dict-ref* parent 'parent 'parent 'parent 'pointers) (list (mhasheq 'type uint8 'val 10 'parent parent))) @@ -172,11 +172,9 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee 'startOffset 0 'pointerOffset 1 'pointers null)) - (encode (+xpointer uint8 'void) (+xvoid-pointer uint8 55) #:parent parent) + (encode (+xpointer uint8 'void) (+xvoid-pointer uint8 55) #:parent parent) (check-equal? (dict-ref parent 'pointerOffset) 2) - (check-equal? (dict-ref parent 'pointers) (list (mhasheq 'type uint8 - 'val 55 - 'parent parent))) + (check-equal? (dict-ref parent 'pointers) (list (mhasheq 'type uint8 'val 55 'parent parent))) (check-equal? (get-output-bytes (current-output-port)) (bytes 1)))) (test-case