Skip to content

Commit

Permalink
"open" command added.
Browse files Browse the repository at this point in the history
Prefixing the list of problems on the CLI with 'open' will open these
problems in a browser instead of adding them to the current project.
  • Loading branch information
bfontaine committed Mar 10, 2014
1 parent 71a2c82 commit 3b898ad
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 25 deletions.
24 changes: 18 additions & 6 deletions src/leiningen/fore_prob.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns leiningen.fore-prob
"Populate the current project with a 4clojure problem."
(:require [clj-http.client :as http]
[clojure.java.io :as io]
[clojure.string :as cs]
[jsoup.soup :as soup]))
(:require [clj-http.client :as http]
[clojure.java.io :as io]
[clojure.string :as cs]
[jsoup.soup :as soup]
[clojure.java.browse :as browse]))

;; == Formatting helpers ==

Expand Down Expand Up @@ -197,10 +198,21 @@
(. e getMessage))))
(println (str "Cannot get problem " prob-num "."))))


(def ^:private problem-url-root "http://www.4clojure.com/problem/")

(defn- open-prob-url
"open one or more problem URLs in a browser"
[& prob-nums]
(doseq [n prob-nums]
(browse/browse-url (str problem-url-root n))))

;; == main function ==

(defn fore-prob
"main function, used by leiningen"
[project & prob-nums]
(doseq [n prob-nums]
(add-prob project n)))
(if (= (first prob-nums) "open")
(apply open-prob-url (rest prob-nums))
(doseq [n prob-nums]
(add-prob project n))))
82 changes: 63 additions & 19 deletions test/lein_fore_prob/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
clj-http.fake)
(:require [leiningen.fore-prob :as fp]
[cheshire.core :as json]
[clojure.string :as cs])
[clojure.string :as cs]
[clojure.java.browse :as browse])
(:import [java.io File]))

;; samples & helpers
Expand Down Expand Up @@ -399,25 +400,68 @@
(#'fp/add-prob project-foo 42)
(is (= @written true)))))))

(deftest fore-prob
(deftest open-prob-url
(testing "one prob"
(let [added (atom false)
proj {:foo :bar}
n "42"]
(with-redefs-fn {#'fp/add-prob (fn [prj prob-num]
(is (= prj proj))
(is (= prob-num n))
(swap! added not))}
(let [opened (atom false)]
(with-redefs-fn
{#'browse/browse-url (fn [u]
(swap! opened not)
(is (= u "http://www.4clojure.com/problem/42")))}
(fn []
(fp/fore-prob proj n)
(is (= @added true))))))
(#'fp/open-prob-url 42)
(is (= @opened true))))))

(testing "multiple probs"
(let [cnt (atom 0)
proj {:foo :bar}]
(with-redefs-fn {#'fp/add-prob (fn [prj _]
(is (= prj proj))
(swap! cnt inc))}
(fn []
(fp/fore-prob proj "42" "17" "26" "32")
(is (= @cnt 4)))))))
(let [opened (atom #{})
probs [42 37 25]]
(with-redefs-fn {#'browse/browse-url (fn [u]
(swap! opened #(conj % u)))}
(fn []
(apply #'fp/open-prob-url probs)
(is (= @opened
(into #{}
(map
#(str "http://www.4clojure.com/problem/" %)
probs)))))))))

(deftest fore-prob
(let [proj {:foo :bar}]
(testing "one prob"
(let [added (atom false)
n "42"]
(with-redefs-fn {#'fp/add-prob (fn [prj prob-num]
(is (= prj proj))
(is (= prob-num n))
(swap! added not))}
(fn []
(fp/fore-prob proj n)
(is (= @added true))))))

(testing "multiple probs"
(let [cnt (atom 0)]
(with-redefs-fn {#'fp/add-prob (fn [prj _]
(is (= prj proj))
(swap! cnt inc))}
(fn []
(fp/fore-prob proj "42" "17" "26" "32")
(is (= @cnt 4))))))

(testing "opening one prob"
(let [opened (atom false)
n "32"]
(with-redefs-fn {#'fp/open-prob-url (fn [& pn]
(is (= pn [n]))
(swap! opened not))}
(fn []
(fp/fore-prob proj "open" n)
(is (= @opened true))))))

(testing "opening one prob"
(let [opened (atom [])
probs ["34" "12" "17"]]
(with-redefs-fn {#'fp/open-prob-url (fn [& pn]
(is (= pn probs))
(swap! opened #(concat % probs)))}
(fn []
(fp/fore-prob proj "open" "34" "12" "17")
(is (= @opened probs))))))))

0 comments on commit 3b898ad

Please sign in to comment.