Permalink
Browse files

test trampoline, and refine `bind` definition

  • Loading branch information...
1 parent a5d37f4 commit 5b5a2982e774aafda01aecc0fe9f2bb910244d80 @youngnh committed May 18, 2012
Showing with 25 additions and 2 deletions.
  1. +2 −2 src/the/parsatron.clj
  2. +23 −0 test/parsatron/test-trampoline.clj
View
@@ -80,11 +80,11 @@
(letfn [(pcok [item state]
(sequentially
(fn [q] (Continue. #(q state cok cerr cok cerr)))
- (Continue. #(f item))))
+ (f item)))
(peok [item state]
(sequentially
(fn [q] (Continue. #(q state cok cerr eok eerr)))
- (Continue. #(f item))))]
+ (f item)))]
(Continue. #(p state pcok cerr peok eerr)))))
(defn nxt
@@ -0,0 +1,23 @@
+(ns parsatron.test-trampoline
+ (:refer-clojure :exclude [char])
+ (:use [the.parsatron]
+ [clojure.test])
+ (:import (the.parsatron Continue Ok)))
+
+(deftest test-always
+ (testing "always is a fn"
+ (is (fn? (always 5))))
+
+ (testing "with no next parser, always returns Ok"
+ (let [p (always 5)
+ result (p nil nil nil (fn eok [item _] (Ok. item)) nil)]
+ (is (= (Ok. 5) result))))
+
+ (testing "bound to a next parser, always returns Continue"
+ (let [p (bind (always 5) (fn [x] (always (+ x 2))))
+ p-continue (p nil nil nil (fn eok [item _] (Ok. item)) nil)]
+ (is (instance? Continue p-continue))
+ (let [q-continue ((:fn p-continue))]
+ (is (instance? Continue q-continue))
+ (let [result ((:fn q-continue))]
+ (is (= (Ok. 7) result)))))))

0 comments on commit 5b5a298

Please sign in to comment.