Skip to content

Commit

Permalink
Add tests for file iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Sep 19, 2015
1 parent 284f873 commit f16658a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
7 changes: 5 additions & 2 deletions colliflower-test.asd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

(defsystem "colliflower-test"
:description "Tests for colliflower"
:version "0.1.0"
:version "0.2.0"
:author "Thayne McCombs <bytecurry.software@gmail.com>"
:license "MIT"
:defsystem-depends-on (:prove-asdf)
Expand All @@ -16,7 +16,10 @@
:pathname "t/liter"
:components ((:test-file "base-test")
(:test-file "generate-test")
(:test-file "tools-test")))
(:test-file "tools-test")
(:test-file "iter-object-test")
(:test-file "file-test")
(:static-file "test.txt")))
(:module garten
:pathname "t/garten"
:components ((:test-file "base-test")
Expand Down
51 changes: 51 additions & 0 deletions t/liter/file-test.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
;;; filet-test.lisp
;;; Copyright (c) Thayne McCombs

(defpackage liter/file-test
(:use :cl :prove :liter))

(in-package liter/file-test)

(defparameter *file-path* (asdf:component-pathname
(asdf:find-component :colliflower-test
'("liter" "test.txt"))))
(defparameter *content-bytes*
'(102 105 114 115 116 10 115 101 99 111 110 100 10 116 104 105 114 100 10))

(defparameter *content-characters* (coerce "first
second
third
" 'list))

(plan nil)

(subtest "character"
(with-file-iterator (it *file-path* :element-type 'character)
(is (iterator-list it)
*content-characters*
:test #'equalp
"Read the fle as characters"))
(let* ((f (make-file-iterator *file-path*))
(it (get-iterator f)))
(is (iter-object-next f) #\f)
(is (iter-object-next f) #\i)
(is (funcall it) #\r)
(is (inext it) #\s)
(is (inext it) #\t)
(is (inext it) #\newline)
(is (coerce (iterator-list it) 'string)
"second
third
" "Iterator should match rest of string.")
(ok (iter-object-end-p f) "Iterator should be ended.")))


(subtest "bytes"
(with-file-iterator (it *file-path* :element-type '(unsigned-byte 8))
(is (iterator-list it) *content-bytes* "Read the file as bytes")))

(subtest "lines"
(with-file-iterator (it *file-path* :reader #'read-line)
(is (iterator-list it) '("first" "second" "third") "Read the file as lines")))

(finalize)
45 changes: 45 additions & 0 deletions t/liter/iter-object-test.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
;;; iter-object-test.lips
;;; Copyright (c) 2015 Thayne McCombs

(defpackage liter/iter-object-test
(:use :cl :prove :liter)
#+closer-mop
(:import-from :closer-mop
#:funcallable-standard-class))

(in-package :liter/iter-object-test)

(plan nil)

(defclass test-iterator (iter-object)
((val :initform 0 :accessor test-iterator-value))
#+closer-mop
(:metaclass funcallable-standard-class))

(defmethod iter-object-next ((obj test-iterator) &rest args)
(declare (ignore args))
(incf (test-iterator-value obj)))

(defmethod iter-object-prev ((obj test-iterator) &rest args)
(declare (ignore args))
(decf (test-iterator-value obj)))

(defmethod iter-object-end-p ((obj test-iterator))
nil)

#+closer-mop
(subtest "funcall test-iterator"
(diag "with closer-mop enabled")
(let ((it (make-instance 'test-iterator)))
(is (funcall it) 1)
(is (inext it) 2)
(is (get-iterator it) it :test #'eq "GET-ITERATOR should act as identity function.")))

(let* ((test-it (make-instance 'test-iterator))
(it (get-iterator test-it)))
(is (iter-object-next test-it) 1)
(is (funcall it) 2)
(is (iter-object-prev test-it) 1)
(is (iter-object-prev test-it) 0))

(finalize)
3 changes: 3 additions & 0 deletions t/liter/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
first
second
third

0 comments on commit f16658a

Please sign in to comment.