Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First draft of future function wrappers.

  • Loading branch information...
commit 5c55f407dbb39652fa8adffaceef9d54ab506098 0 parents
@ifesdjeen authored
23 README.md
@@ -0,0 +1,23 @@
+# Clojure VCR
+
+This is (yet another) implementation of [VCR rubygem](https://github.com/myronmarston/vcr). As [@myronmarston](https://github.com/myronmarston)
+originally described it: Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
+
+Personally, I'm still sceptical about mocking HTTP requests due to many reasons. One of them is, of course, the fact that library is made for
+integration, and mocking your APIs detaches your development / CI from the original service, which can make your tests pass, but lib itself will
+be broken. Use it wisely.
+
+## What's there?
+
+Until now (about 1 day in development), only a rough prototype of a function that's going to wrap HTTP requests is done. But lots of things planned:
+
+ * persist your test results to the VCR cassete (YAML file)
+ * replay your tests
+ * schedule cassete removal, set up logic for making cassete outdated
+ * mock your HTTP calls
+
+## License
+
+Copyright © 2012 Alex P
+
+Distributed under the Eclipse Public License, the same as Clojure.
9 project.clj
@@ -0,0 +1,9 @@
+(defproject vcr-clj "0.1.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :url "http://example.com/FIXME"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.3.0"]
+ [clj-yaml "0.3.1"]
+ [robert/hooke "1.1.2"]
+ [clj-http "0.4.1"]])
10 src/vcr_clj/core.clj
@@ -0,0 +1,10 @@
+(ns vcr-clj.core)
+
+(defn wrap-fn
+ "Wrap or replace some function with your own function"
+ [qualifier wrapper]
+ (alter-var-root
+ qualifier
+ (fn [original-fn]
+ (fn [& caller-arguments]
+ (wrapper caller-arguments original-fn)))))
93 test/vcr_clj/core_test.clj
@@ -0,0 +1,93 @@
+(ns vcr-clj.core-test
+ (:require [clj-http.client :as client])
+ (:use clojure.test
+ vcr-clj.core))
+
+(deftest wrap-fn-test
+ (testing "Replace function (arity 0) with a different implementation"
+ (defn wrap-fn-original
+ []
+ "wrap-fn-original-return-value")
+
+ (is (= "wrap-fn-original-return-value" (wrap-fn-original)))
+
+ (wrap-fn #'wrap-fn-original
+ (fn [caller-arguments original-fn]
+ "wrap-fn-replaced-return-value"))
+
+ (is (= "wrap-fn-replaced-return-value" (wrap-fn-original))))
+
+ (testing "Replace function (arity 1) with a different implementation"
+ (defn wrap-fn-original-arity-1
+ [a]
+ (format "original-value-is-%s" a))
+
+ (is (= "original-value-is-aaa" (wrap-fn-original-arity-1 "aaa")))
+
+ (wrap-fn #'wrap-fn-original-arity-1
+ (fn [caller-arguments original-fn]
+ (format "changed-value-is-%s" (first caller-arguments))))
+
+ (is (= "changed-value-is-aaa" (wrap-fn-original-arity-1 "aaa"))))
+
+ (testing "Replace function (arity 1 and optional keys) with a different implementation"
+ (defn wrap-fn-original-arity-1-and-optional-keys
+ [a &{:keys [b c d]}]
+ (format "original-values-for-a-%s-b-%d-c-%d-d-%d" a b c d))
+
+ (is (= "original-values-for-a-aaa-b-1-c-2-d-3" (wrap-fn-original-arity-1-and-optional-keys "aaa" :b 1 :c 2 :d 3)))
+
+ (wrap-fn #'wrap-fn-original-arity-1-and-optional-keys
+ (fn [[a & {:keys [b c d]}] original-fn]
+ (format "changed-values-for-a-%s-b-%d-c-%d-d-%d" a b c d)))
+
+ (is (= "changed-values-for-a-aaa-b-1-c-2-d-3" (wrap-fn-original-arity-1-and-optional-keys "aaa" :b 1 :c 2 :d 3))))
+
+
+
+ (testing "Chain function (arity 0) with a different implementation"
+ (defn wrap-fn-original
+ []
+ "wrap-fn-original-return-value")
+
+ (is (= "wrap-fn-original-return-value" (wrap-fn-original)))
+
+ (wrap-fn #'wrap-fn-original
+ (fn [caller-arguments original-fn]
+ (let [original-result (apply original-fn caller-arguments)]
+ (str original-result " " "wrap-fn-chained-return-value"))))
+
+ (is (= "wrap-fn-original-return-value wrap-fn-chained-return-value" (wrap-fn-original))))
+
+ (testing "Chain function (arity 1) with a different implementation"
+ (defn wrap-fn-original-arity-1
+ [a]
+ (format "original-value-is-%s" a))
+
+ (is (= "original-value-is-aaa" (wrap-fn-original-arity-1 "aaa")))
+
+ (wrap-fn #'wrap-fn-original-arity-1
+ (fn [caller-arguments original-fn]
+ (let [original-result (apply original-fn caller-arguments)]
+ (str original-result " " (format "chained-value-is-%s" (first caller-arguments))))))
+
+ (is (= "original-value-is-aaa chained-value-is-aaa" (wrap-fn-original-arity-1 "aaa"))))
+
+ (testing "Chain function (arity 1 and optional keys) with a different implementation"
+ (defn wrap-fn-original-arity-1-and-optional-keys
+ [a &{:keys [b c d]}]
+ (format "original-values-for-a-%s-b-%d-c-%d-d-%d" a b c d))
+
+ (is (= "original-values-for-a-aaa-b-1-c-2-d-3" (wrap-fn-original-arity-1-and-optional-keys "aaa" :b 1 :c 2 :d 3)))
+
+ (wrap-fn #'wrap-fn-original-arity-1-and-optional-keys
+ (fn [[a & {:keys [b c d]} :as caller-arguments] original-fn]
+ (let [original-result (apply original-fn caller-arguments)]
+ (str original-result " " (format "chained-values-for-a-%s-b-%d-c-%d-d-%d" a b c d)))))
+
+ (is (= "original-values-for-a-aaa-b-1-c-2-d-3 chained-values-for-a-aaa-b-1-c-2-d-3" (wrap-fn-original-arity-1-and-optional-keys "aaa" :b 1 :c 2 :d 3))))
+
+
+
+ )
+
Please sign in to comment.
Something went wrong with that request. Please try again.