edward

An extensible POSIX-compatible implementation of the ed(1) text editor

git clone https://git.8pit.net/edward.git

  1(import (edward ed cmd)
  2        (edward ed posix)
  3        (edward ed addr)
  4        (edward ed editor))
  5
  6(define (test-parse-cmd desc expected input)
  7  (test desc expected
  8        (let* ((cmd-input (string-append input "\n"))
  9               (cmd-pair  (%test-parse (parse-cmd) cmd-input))
 10               (cmd-addr  (car cmd-pair))
 11               (cmd-args  (cmd-args (cdr cmd-pair))))
 12          (append (list cmd-addr) cmd-args))))
 13
 14(define (test-cmd-error desc expected input)
 15  (test-parse-error desc expected (parse-cmd) input))
 16
 17;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 18
 19(test-group "append command"
 20  (test-parse-cmd "no arguments"
 21    (list #f '()) "a\n.")
 22  (test-parse-cmd "preceeding whitespaces"
 23    (list #f '()) "  a\n.")
 24  (test-parse-cmd "custom address without offset"
 25    (list
 26      (addr->range (make-addr '(nth-line . 2342)))
 27      '())
 28    "2342    a\n.")
 29  (test-parse-cmd "custom address with offset"
 30    (list
 31      (addr->range (make-addr '(last-line . ()) '(42)))
 32      '())
 33    "$+42 a\n."))
 34
 35(test-group "read command"
 36  (test-parse-cmd "no arguments"
 37    (list
 38      #f
 39      "") "r")
 40
 41  (test-parse-cmd "custom address"
 42    (list
 43      (addr->range (make-addr '(nth-line . 42)))
 44      "") "42r")
 45
 46  (test-parse-cmd "custom address and file"
 47    (list
 48      (addr->range (make-addr '(regex-backward . "foo") '(23 -42)))
 49      "foobar") "?foo? +23 -42 r foobar")
 50
 51  (test-cmd-error "filename w/o whitespace" "expected newline" "rfoo"))
 52
 53(test-group "write command"
 54  (test-parse-cmd "no arguments"
 55    (list
 56      #f
 57      "") "w")
 58
 59  (test-parse-cmd "custom address and offset, no whitespaces"
 60    (list
 61      (make-range
 62        (make-addr '(current-line))
 63        (make-addr '(current-line) '(10)))
 64      "foobar") ".,.+10w foobar")
 65
 66  (test-cmd-error "filename w/o whitespace" "expected newline" "wfoo"))
 67
 68(test-group "shell command"
 69  (test-parse-cmd "no replacements"
 70    (list #f (list "echo foobar")) "!echo foobar")
 71  (test-parse-cmd "command with replacement"
 72    (list #f (list "echo " 'current-file)) "!echo %")
 73  (test-parse-cmd "previous command"
 74    (list #f '(previous-command)) "!!")
 75  (test-parse-cmd "previous command appended"
 76    (list #f '(previous-command "foobar")) "!!foobar")
 77  (test-parse-cmd "previous command syntax not start"
 78    (list #f '("foobar !! barfoo")) "!foobar !! barfoo")
 79  (test-parse-cmd "escaped replacement"
 80    (list #f (list "echo %")) "!echo \\%")
 81  (test-parse-cmd "multiple replacements"
 82    (list #f (list "echo " 'current-file " " 'current-file)) "!echo % %"))
 83
 84(test-group "global command"
 85  (test-parse-cmd "single command no newline"
 86    (list
 87      (make-range
 88        (make-addr '(nth-line . 1))
 89        (make-addr '(last-line)))
 90      "foo"
 91      "p\n") "1,$g/foo/p")
 92
 93  (test-parse-cmd "empty command-list"
 94    (list
 95      #f
 96      "bar"
 97      "p\n") "g/bar/")
 98
 99  (test-parse-cmd "single trailing whitespace"
100    (list
101      (make-range
102        (make-addr '(nth-line . 1))
103        (make-addr '(last-line)))
104      "foobar"
105      "p \n") "1,$g/foobar/p ")
106
107  (test-parse-cmd "single command no newline"
108    (list
109      (make-range
110        (make-addr '(nth-line . 23))
111        (make-addr '(nth-line . 42)))
112      "test"
113      "p \np\n") "23,42g/test/p \\\np")
114
115  (test-cmd-error "missing regex" "expected regex" "1,$gp"))
116
117(test-group "miscellaneous"
118  (test-parse-cmd "parse command with trailing blanks"
119    (list (addr->range (make-addr '(nth-line . 2342)))
120          '())
121    "2342a     \n.")
122
123  (test-parse-cmd "append command with suffixed printing command"
124    (list (addr->range (make-addr '(nth-line . 42)))
125          '())
126     "42an\n.")
127
128  (test-cmd-error "unknown command character" "unknown command" "X")
129  (test-cmd-error "quit command with argument" "expected newline" "Qn")
130  (test-cmd-error "append command with argument" "expected newline" "a n"))