Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 45 lines (37 sloc) 1.556 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
(ns reply.reader.jline.jline-input-reader-spec
  (:use [speclj.core])
  (:import [reply.reader.jline JlineInputReader]))

(defprotocol LineReader
  (readLine [_]))

(defrecord FakeJlineReader [f]
  LineReader
  (readLine [this] (f)))

(defn fake-reader
  ([input-fn] (fake-reader input-fn #()))
  ([input-fn set-empty-prompt]
    (let [jline-reader (FakeJlineReader. input-fn)]
      (JlineInputReader. {:jline-reader jline-reader
                          :set-empty-prompt set-empty-prompt}))))

(describe "JLineInputReader"
  (it "reads a -1 when nothing is available"
    (should= -1 (.read (fake-reader (constantly nil)))))

  (it "reads a newline"
    (should= 10 (.read (fake-reader (constantly "")))))

  (it "reads a few characters"
    (let [reader (fake-reader (constantly "abcd"))]
      (should= [97 98 99 100 10]
               (for [i (range 5)] (.read reader)))))

  (it "calls for more input on a newline"
    (let [times-readLine-called (atom 0)
          reader (fake-reader (constantly "")
                              #(swap! times-readLine-called inc))]
      (should= [10 10 10]
               (for [i (range 3)] (.read reader)))
      (should= 3 @times-readLine-called)))

  (it "makes no more calls to readLine than are necessary"
    (let [times-readLine-called (atom 0)
          reader (fake-reader (constantly "ab")
                              #(swap! times-readLine-called inc))]
      (should= [97 98 10 97 98 10 97 98 10 97]
               (for [i (range 10)] (.read reader)))
      (should= 4 @times-readLine-called))))
Something went wrong with that request. Please try again.