Note: This question is building on the answers to the questions preceding it. Rather than include the code from the last questions each time, I am only including that code which is specific to this question. If I seem to be missing a function or two, I suggest you go look at one of the previous answers!
Exercise 153: Design the functions
;; Editor -> Editor
;; move the cursor position one 1String left, if possible
;(define (editor-lft ed)
; ed)
;
;; Editor -> Editor
;; move the cursor position one 1String right, if possible
;(define (editor-rgt ed)
; ed)
;; Editor -> Editor
;; delete one 1String to the left of the cursor, if possible
;(define (editor-del ed)
; ed)
These are all fairly straight forward functions - simple list manipulation with no recursion to worry about. The challenge here lies in coming up with a reasonable test suite.
; Editor -> Editor
; move the cursor position one 1String left, if possible
; All we need to do is remove the leading character of the
; 'pre' list and append it to the start of the 'post' list
(define (editor-lft ed)
(cond ((empty? (editor-pre ed)) ed)
(else (make-editor (rest (editor-pre ed))
(cons (first (editor-pre ed))
(editor-post ed))))))
; simple case - text on both sides, should move cursor to
; the left
(check-expect (editor-lft (create-editor "eht" "word"))
(create-editor "ht" "eword"))
; empty on left hand side - shouldn't change state
(check-expect (editor-lft (create-editor "" "word"))
(create-editor "" "word"))
I can't think of any other test cases we need to cover here - if editor-post is populated or empty, it shouldn't make any difference to our functions.
; Editor -> Editor
; move the cursor position one 1String right, if possible
; This is the inverse of editor-lft - remove the first character
; from the 'post' text and prepend that to the 'pre' text
(define (editor-rgt ed)
(cond ((empty? (editor-post ed)) ed)
(else (make-editor (append
(explode (first (editor-post ed)))
(editor-pre ed) )
(rest (editor-post ed))))))
; simple case - text on both sides, should move cursor to the right
; Bear in mind that editor-pre is stored reversed, so we need to add chars to the front
; of it.
(check-expect (editor-rgt (create-editor "eht" "cat"))
(create-editor "ceht" "at"))
; empty on right hand side - shouldn't change state
(check-expect (editor-rgt (create-editor "word" ""))
(create-editor "word" ""))
; empty on both sides - shouldn't change state
(check-expect (editor-rgt (create-editor "" ""))
(create-editor "" ""))
; Editor -> Editor
; delete one 1String to the left of the cursor, if possible
(define (editor-del ed)
(cond ((empty? (editor-pre ed)) ed)
(else (make-editor (rest (editor-pre ed))
( editor-post ed)))))
; simple case - string populated on both sides should just
; delete the first chart to the left
(check-expect (editor-del (create-editor "eht" "cat"))
(create-editor "ht" "cat"))
; if the editor-pre is not populated, then nothing should change
; (as there is nothing to delete)
(check-expect (editor-del (create-editor "" "cat"))
(create-editor "" "cat"))
; if the editor-post is not populated, then should still
; be a straight forward delete
(check-expect (editor-del (create-editor "eht " ""))
(create-editor "ht " ""))
; if the editor-post *and* editor-pre are not populated,
; then nothing should happen
(check-expect (editor-del (create-editor "" ""))
(create-editor "" ""))
; and finally, check what happens when there is exactly
; one character to delete
(check-expect (editor-del (create-editor "a" "xxx"))
(create-editor "" "xxx"))
No comments:
Post a Comment