You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/xenomorph/xenomorph/test/optional-test.rkt

116 lines
4.3 KiB
Racket

#lang racket/base
(require rackunit
6 years ago
racket/class
"../base.rkt"
6 years ago
"../number.rkt"
6 years ago
"../optional.rkt"
"../base.rkt")
#|
approximates
https://github.com/mbutterick/restructure/blob/master/test/Optional.coffee
|#
6 years ago
(test-case
6 years ago
"optional: decode should not decode when condition is falsy"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
5 years ago
(define optional (x:optional uint8 #:condition #f))
6 years ago
(check-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 0)))
(test-case
6 years ago
"optional: decode with post-decode"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
5 years ago
(define optional (x:optional uint8 #:condition #f #:post-decode (λ (val) 42)))
6 years ago
(check-equal? (decode optional) 42)
(check-equal? (pos (current-input-port)) 0)))
(test-case
6 years ago
"optional: decode should not decode when condition is a function and falsy"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
5 years ago
(define optional (x:optional uint8 #:condition (λ _ #f)))
6 years ago
(check-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 0)))
(test-case
6 years ago
"optional: decode should decode when condition is omitted"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
5 years ago
(define optional (x:optional uint8))
6 years ago
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1)))
(test-case
6 years ago
"optional: decode should decode when condition is truthy"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
5 years ago
(define optional (x:optional uint8 #:condition #t))
6 years ago
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1)))
(test-case
6 years ago
"optional: decode should decode when condition is a function and truthy"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
5 years ago
(define optional (x:optional uint8 #:condition (λ _ #t)))
6 years ago
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1)))
(test-case
6 years ago
"optional: size"
(check-equal? (send (x:optional uint8 #:condition #f) x:size) 0))
6 years ago
(test-case
6 years ago
"optional: size should return 0 when condition is a function and falsy"
(check-equal? (send (x:optional uint8 #:condition (λ _ #f)) x:size) 0))
6 years ago
(test-case
6 years ago
"optional: size should return given type size when condition is omitted"
(check-equal? (send (x:optional uint8) x:size) 1))
6 years ago
(test-case
6 years ago
"optional: size should return given type size when condition is truthy"
(check-equal? (send (x:optional uint8 #:condition #t) x:size) 1))
6 years ago
(test-case
6 years ago
"optional: size should return given type size when condition is a function and truthy"
(check-equal? (send (x:optional uint8 #:condition (λ _ #t)) x:size) 1))
6 years ago
(test-case
6 years ago
"optional: encode should not encode when condition is falsy"
6 years ago
(parameterize ([current-output-port (open-output-bytes)])
5 years ago
(define optional (x:optional uint8 #:condition #f))
6 years ago
(encode optional 128)
6 years ago
(check-equal? (get-output-bytes (current-output-port)) (bytes))))
6 years ago
(test-case
6 years ago
"optional: encode with pre-encode"
6 years ago
(parameterize ([current-output-port (open-output-bytes)])
5 years ago
(define optional (x:optional uint8 #:pre-encode (λ (val) 42)))
6 years ago
(encode optional 128)
6 years ago
(check-equal? (get-output-bytes (current-output-port)) (bytes 42))))
6 years ago
(test-case
6 years ago
"optional: encode should not encode when condition is a function and falsy"
6 years ago
(parameterize ([current-output-port (open-output-bytes)])
5 years ago
(define optional (x:optional uint8 #:condition (λ _ #f)))
6 years ago
(encode optional 128)
6 years ago
(check-equal? (get-output-bytes (current-output-port)) (bytes))))
6 years ago
(test-case
6 years ago
"optional: encode should encode when condition is omitted"
6 years ago
(parameterize ([current-output-port (open-output-bytes)])
5 years ago
(define optional (x:optional uint8))
6 years ago
(encode optional 128)
6 years ago
(check-equal? (get-output-bytes (current-output-port)) (bytes 128))))
6 years ago
(test-case
6 years ago
"optional: encode should encode when condition is truthy"
6 years ago
(parameterize ([current-output-port (open-output-bytes)])
5 years ago
(define optional (x:optional uint8 #:condition #t))
6 years ago
(encode optional 128)
6 years ago
(check-equal? (get-output-bytes (current-output-port)) (bytes 128))))
6 years ago
(test-case
6 years ago
"optional: encode should encode when condition is a function and truthy"
6 years ago
(parameterize ([current-output-port (open-output-bytes)])
5 years ago
(define optional (x:optional uint8 #:condition (λ _ #t)))
6 years ago
(encode optional 128)
6 years ago
(check-equal? (get-output-bytes (current-output-port)) (bytes 128))))