diff --git a/README.md b/README.md index 984f582..5db9b4e 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ optional font info and optional worklogs info, e.g.: :worklogs [(...)]} -`:font-path` should be the path to a font file, e.g. "/usr/share/fonts/truetype/freefont/FreeSans.ttf" -See `resources/config.edn` for an example configuration. +`:font-path` should be the path to a font file, e.g. `"/usr/share/fonts/truetype/freefont/FreeSans.ttf"` +See [`resources/config.edn`](https://github.com/mruwnik/invoices/blob/master/resources/config.edn) for an example configuration. ### Seller diff --git a/src/invoices/core.clj b/src/invoices/core.clj index e09062c..b039b01 100644 --- a/src/invoices/core.clj +++ b/src/invoices/core.clj @@ -57,7 +57,7 @@ :default 1 :parse-fn #(Integer/parseInt %)] ["-w" "--when DATE" "The month for which to generate the invoice" - :default (java.time.LocalDate/now) + :default (-> (java.time.LocalDate/now) (.minusMonths 1)) :parse-fn #(java.time.LocalDate/parse %)] ;; A non-idempotent option (:default is applied first) ["-c" "--company NIP" "companies for which to generate invoices. All, if not provided" diff --git a/src/invoices/email.clj b/src/invoices/email.clj index 38747f2..0b05b9a 100644 --- a/src/invoices/email.clj +++ b/src/invoices/email.clj @@ -56,20 +56,20 @@ (->> content str/split-lines (remove str/blank?) - (map #(str/split % #"[\s;]")))) + (map #(str/split % #"[\s;]+")))) (defn zip-item [headers cell] (into (sorted-map) (map vector headers cell))) -(defn parse-float [s] - (Float. (re-find #"[\d\.]+" s ))) +(defn parse-double [s] + (Double. (re-find #"-?[\d\.]+" s ))) (defn extract-items [headers message] (->> message mess/get-content split-cells (map (partial zip-item headers)) - (map #(update % :worked parse-float)))) + (map #(update % :worked parse-double)))) (defn get-worklogs "Get all worklogs for the given month from the given imap server." diff --git a/test/invoices/email_test.clj b/test/invoices/email_test.clj new file mode 100644 index 0000000..f18a76d --- /dev/null +++ b/test/invoices/email_test.clj @@ -0,0 +1,30 @@ +(ns invoices.email-test + (:require [invoices.email :refer :all] + [clojure.test :refer :all])) + + +(deftest test-split-cells + (let [cells [["row1-col1", "row1-col2", "row1-col3"] + ["row2-col1", "row2-col2", "row2-col3"] + ["row3-col1", "row3-col2", "row3-col3"]]] + (testing "Check whether cells get split correctly" + (is (= cells (split-cells "row1-col1;row1-col2;row1-col3\nrow2-col1;row2-col2;row2-col3\nrow3-col1;row3-col2;row3-col3\n"))) + (is (= cells (split-cells "row1-col1 row1-col2 row1-col3\nrow2-col1 row2-col2 row2-col3\nrow3-col1 row3-col2 row3-col3\n"))) + (is (= cells (split-cells "row1-col1 row1-col2 row1-col3\nrow2-col1 row2-col2 row2-col3\nrow3-col1 row3-col2 row3-col3\n"))) + (is (= cells (split-cells "row1-col1; row1-col2; row1-col3\nrow2-col1; row2-col2; row2-col3\nrow3-col1; row3-col2; row3-col3\n")))) + (testing "Check whether extra whitespace gets removed" + (is (= cells (split-cells "row1-col1 row1-col2 row1-col3\nrow2-col1 row2-col2 row2-col3\nrow3-col1; \trow3-col2 row3-col3\n")))))) + +(deftest test-zip-item + (testing "Check whether items get correctly zipped" + (is (= (zip-item [:coll1 :coll2 :coll3] ["row1-col1", "row1-col2", "row1-col3"]) + {:coll1 "row1-col1" :coll2 "row1-col2" :coll3 "row1-col3"})))) + +(deftest test-parse-double + (testing "Check whether doubles get correctly parsed" + (is (= (parse-double "123") 123.0)) + (is (= (parse-double "123.1234") 123.1234)) + (is (= (parse-double "123.654321543") 123.654321543)) + (is (= (parse-double "0.00000") 0.0)) + (is (= (parse-double "-123") -123.0)) + (is (= (parse-double "asdasd123adasd32") 123.0))))