add more operations on number snips

This commit is contained in:
Robby Findler 2017-04-18 08:01:23 -05:00
parent 0c79a90c69
commit 366b27b05b
5 changed files with 49 additions and 24 deletions

View File

@ -193,22 +193,38 @@
(proc-doc/names
number-snip:make-repeating-decimal-snip
(real? boolean? . -> . (is-a?/c snip%))
(-> real? boolean? number-snip:is-number-snip?)
(num show-prefix?)
@{Makes a number snip that shows the decimal expansion for @racket[number].
@{Makes a @tech{number snip} that shows the decimal expansion for @racket[number].
The boolean indicates if a @litchar{#e} prefix appears on the number.
See also @racket[number-snip:make-fraction-snip].})
(proc-doc/names
number-snip:make-fraction-snip
(real? boolean? . -> . (is-a?/c snip%))
(-> real? boolean? number-snip:is-number-snip?)
(num show-prefix-in-decimal-view?)
@{Makes a number snip that shows a fractional view of @racket[number].
@{Makes a @tech{number snip} that shows a fractional view of @racket[number].
The boolean indicates if a @litchar{#e} prefix appears on the number, when
shown in the decimal state.
See also @racket[number-snip:make-repeating-decimal-snip].})
(proc-doc/names
number-snip:is-number-snip?
(-> any/c boolean?)
(v)
@{Determines if @racket[v] is a @deftech{number snip}, i.e., created
by @racket[number-snip:make-fraction-snip]
or @racket[number-snip:make-repeating-decimal-snip].
All values that answer @racket[#t] to this predicate are also @racket[snip%]s.})
(proc-doc/names
number-snip:get-number
(-> number-snip:is-number-snip? real?)
(ns)
@{Returns the number that this @tech{number snip} displays.})
(thing-doc
comment-box:snipclass

View File

@ -92,6 +92,8 @@
(send number-snip get-text 0 1)]
[else default]))])))
(define (get-number s) (send s get-number))
(define (is-number-snip? x) (is-a? x number-snip%))
(define number-snip%
(class* snip% (readable-snip<%> number-snip-convertible<%>)

View File

@ -9,9 +9,11 @@
(snip-class%))
(define-signature number-snip^ extends number-snip-class^
(make-repeating-decimal-snip
make-fraction-snip))
make-fraction-snip
is-number-snip?
get-number))
(define-signature number-snip/int^ extends number-snip^
(is-number-snip?))
())
(define-signature comment-box-class^
(snip%))

View File

@ -66,7 +66,7 @@ signal failures when there aren't any.
| make sure that mred:the-frame-group records frames correctly.
| fake user input expected.
- number snip: |# number-snip.rkt #|
- number snip: number-snip.rkt -- now runs directly via raco test
| some tests for the number-snip% class

View File

@ -1,22 +1,27 @@
#lang racket/base
(require "test-suite-utils.rkt")
(require "test-suite-utils.rkt"
racket/contract
framework
file/convertible
rackunit)
(test
'number-snip-convert-text
(λ (x) (or (equal? "1/2" x) (equal? "0.5" x)))
(lambda ()
(queue-sexp-to-mred
`((dynamic-require 'file/convertible 'convert)
(check-true
(let ()
(define x
(convert
(number-snip:make-fraction-snip 1/2 #f)
'text
#f))))
#f))
(or (equal? "1/2" x) (equal? "0.5" x))))
(test
'number-snip-convert-png
bytes?
(lambda ()
(queue-sexp-to-mred
`((dynamic-require 'file/convertible 'convert)
(number-snip:make-fraction-snip 1/2 #f)
'png-bytes
#f))))
(check-true
(bytes?
(convert
(number-snip:make-fraction-snip 1/2 #f)
'png-bytes
#f)))
(check-true (number-snip:is-number-snip? (number-snip:make-fraction-snip 3/2 #t)))
(check-false (number-snip:is-number-snip? 3/2))
(check-equal? 3/2 (number-snip:get-number (number-snip:make-fraction-snip 3/2 #t)))