Skip to content

Commit e160583

Browse files
committed
Add additional tests around extraction functionality
1 parent 37f60dc commit e160583

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

project.clj

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
[pjstadig/humane-test-output "0.11.0"]]
2626
:injections [(require 'pjstadig.humane-test-output)
2727
(pjstadig.humane-test-output/activate!)]
28-
:plugins [[com.jakemccrary/lein-test-refresh "0.25.0"]]}}
28+
:plugins [[com.jakemccrary/lein-test-refresh "0.25.0"]]}
29+
:test {:resource-paths ["test-resources"] }}
2930
:min-lein-version "2.0.0")

test-resources/logs/2017-06-29.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml">
4+
<head><title>#clojure log - Jun 29 2017</title><meta content="application/xhtml+xml; charset=UTF-8" http-equiv="Content-Type" /><meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0" /><link href="irc.css" type="text/css" rel="stylesheet" /></head><body><h1>#clojure log - Jun 29 2017</h1><div id="narrow"><dl><dt><form action="http://www.google.com/cse" id="cse-search-box"><div><input name="cx" type="hidden" value="partner-pub-1237864095616304:e7qm3gycp2b" /><input name="ie" type="hidden" value="UTF-8" /><input name="q" size="10" type="text" id="q" /><input name="sa" type="submit" id="sa" value="Go" /></div></form><script src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en" type="text/javascript"></script><a href="http://www.joyofclojure.com/" id="book"><i>The Joy of Clojure</i></a></dt><dd><a href="http://clojure.org/">Main Clojure site</a></dd><dd><a href="http://groups.google.com/group/clojure">Google Group</a></dd><dd><a href="irc://irc.freenode.net/clojure">IRC</a></dd><dd><a href="/date/">List of all logged dates</a></dd></dl><div class="nav" id="nav-head"><noscript>Turn on JavaScript for date navigation.</noscript>&nbsp;</div><div id="main"><p><a class="nh" name="00:23">0:23</a> <b>fred: </b>lucy: hi
5+
</p><p><a name="00:24">0:24</a> <b>lucy: </b>hello!
6+
</p><p><a name="00:24a">0:24</a> <b>fred: </b>(inc 1) is another way to do (+ 1 1). (apply + '(1 1)) is another way to do it.
7+
</p><p><a name="00:27">0:27</a> <b>lucy: </b>neat.
8+
</p><p><a name="01:19">1:19</a> <b>bob: </b>start of thought
9+
</p><p><a name="01:20">1:20</a> more of a thought in the next message
10+
</p><p><a name="01:21">0:27</a> <b>lucy: </b>good thought.
11+
</p></div><div class="nav" id="nav-foot">&nbsp;</div><div class="foot">Logging service provided by <a href="http://n01se.net/" class="nm">n01se.net</a></div><script src="irc.js" type="text/javascript"></script></div></body></html>

test/getclojure/extract_test.clj

+72-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,79 @@
11
(ns getclojure.extract-test
22
(:require
3-
[clojure.test :refer [deftest is]]
4-
[getclojure.extract :as subject]))
3+
[clojure.test :refer [deftest testing is]]
4+
[getclojure.extract :as subject]
5+
[clojure.java.io :as io]))
56

67
(deftest extract-sexps-test
7-
(is (= '()
8-
(subject/extract-sexps "")))
8+
(testing "We extract s-expressions from strings as expected."
9+
(is (= '()
10+
(subject/extract-sexps "")))
911

10-
(is (= (into #{} ["(inc 1)" "(inc 2)" "()"])
11-
(into #{} (subject/extract-sexps "(inc 1) blah blah () (inc 2)"))))
12+
(is (= (into #{} ["(inc 1)" "(inc 2)" "()"])
13+
(into #{} (subject/extract-sexps "(inc 1) blah blah () (inc 2)"))))
1214

13-
(is (= (into #{} ["(+ (inc 1))" "(inc 2)"])
14-
(into #{} (subject/extract-sexps "(+ (inc 1)) (inc 2)"))))
15+
(is (= (into #{} ["(+ (inc 1))" "(inc 2)"])
16+
(into #{} (subject/extract-sexps "(+ (inc 1)) (inc 2)"))))
1517

16-
(is (= '()
17-
(subject/extract-sexps nil))))
18+
(is (= '()
19+
(subject/extract-sexps nil)))))
20+
21+
(deftest logfile->mapseq-test
22+
(testing "logfile->mapseq returns maps with expected data for our sample html file"
23+
(let [logfile (io/as-file (io/resource "logs/2017-06-29.html"))
24+
result (subject/logfile->mapseq logfile)]
25+
26+
(is (every? (fn [m] (some? (:nickname m)))
27+
result)
28+
"Every line contains a nickname despite some entries missing them")
29+
30+
(is (every? (fn [m] (some? (:content m)))
31+
result)
32+
"Every line contains content")
33+
34+
(is (every? (fn [m] (some? (:timestamp m)))
35+
result)
36+
"Every line contains a timestamp")
37+
38+
(is (every? (fn [m] (some? (:date m)))
39+
result)
40+
"Every line contains a date")
41+
42+
(is (= 1 (-> (filter (fn [m] (:sexps m)) result)
43+
count))
44+
"Only one line in the log contains s-expressions")
45+
46+
(is (= 3 (-> (filter (fn [m] (:sexps m)) result)
47+
first
48+
:sexps
49+
count))
50+
"The line containing s-expressions has 3 expressions"))))
51+
52+
(deftest forward-propagate-test
53+
(testing "That we fill nil values with the last seen value for the same keyword"
54+
(let [example-input [{:nickname "bill" :content "ciil"}
55+
{:nickname nil :content "cool*"}]
56+
empty-start [{:nickname nil :content "a"}
57+
{:nickname "ted" :content "b"}]]
58+
(is (= [{:nickname "bill"
59+
:content "ciil"}
60+
{:nickname "bill"
61+
:content "cool*"}]
62+
(subject/forward-propagate example-input :nickname))
63+
"We carry forward the nickname to the next nil value")
64+
(is (= [{:nickname nil
65+
:content "a"}
66+
{:nickname "ted"
67+
:content "b"}]
68+
(subject/forward-propagate empty-start :nickname))
69+
"If we start with a nil value, we don't clobber the next entry's value")
70+
(is (= [{:nickname "bill"
71+
:content "ciil"}
72+
{:nickname "bill"
73+
:content "cool*"}
74+
{:nickname "bill"
75+
:content "how is everyone?"}]
76+
(subject/forward-propagate (conj example-input {:nickname nil
77+
:content "how is everyone?"})
78+
:nickname))
79+
"We carry forward the nickname to all future nil entries, not just the next one"))))

0 commit comments

Comments
 (0)