Skip to content

Commit cdc9aa5

Browse files
committed
move some stuff to utils and write some tests
1 parent c6ce96c commit cdc9aa5

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/mobdap/handler.clj

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[clojure.string :as string]
88
[mobdap.adapter :as adapter]
99
[mobdap.debug-server :as debug-server]
10+
[mobdap.utils :refer [float-to-string map-over-map to-int]]
1011
[taoensso.timbre :as log]))
1112

1213
(def ^:private go-handler (atom nil))
@@ -25,11 +26,6 @@
2526
:stackframes (atom [])
2627
:var-index (atom {})})
2728

28-
(defn- to-int [value]
29-
(if (int? value)
30-
value
31-
(Integer/parseInt value)))
32-
3329
(defn- find-source-dir [handler filepath]
3430
(let [file (io/file filepath)]
3531
(cond
@@ -156,9 +152,6 @@
156152
(adapter/send-message! (:adapter handler) response)
157153
handler))
158154

159-
(defn- map-over-map [fun m]
160-
(reduce-kv (fn [m k v] (assoc m k (fun k v))) {} m))
161-
162155
(defn- parse-heap-value [ident]
163156
(when-let [[_ type addr] (re-find #"(.+):\s*(0x[A-Za-z0-9]+)" ident)]
164157
{:type (keyword type)
@@ -402,9 +395,6 @@
402395
(get-in m [:extras :upvalues]))))
403396
data))
404397

405-
(defn- float-to-string [n]
406-
(.replaceAll (format "%.16f" n) "\\.?0*$" ""))
407-
408398
(defn- var-name [n]
409399
(cond
410400
(keyword? n) (name n)

src/mobdap/utils.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns mobdap.utils)
2+
3+
(defn to-int [value]
4+
(if (int? value)
5+
value
6+
(Integer/parseInt value)))
7+
8+
(defn map-over-map [fun m]
9+
(reduce-kv (fn [m k v] (assoc m k (fun k v))) {} m))
10+
11+
(defn float-to-string [n]
12+
(.replaceAll (format "%.16f" n) "\\.?0*$" ""))

test/mobdap/utils_test.clj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(ns mobdap.utils-test
2+
(:require
3+
[clojure.test :refer [deftest is testing]]
4+
[mobdap.utils :refer [float-to-string map-over-map to-int]]))
5+
6+
(deftest test-to-int
7+
(testing "int just returns as is"
8+
(is (= 1337 (to-int 1337))))
9+
(testing "strings convert properly"
10+
(is (= 42 (to-int "42")))))
11+
12+
(deftest test-map-over-map
13+
(testing "square every item"
14+
(is (match? {:a 1 :b 4 :c 9 :d 16} (map-over-map #(* %2 %2) {:a 1 :b 2 :c 3 :d 4}))))
15+
(testing "turn value to key"
16+
(is (match? {:a :a :b :b :c :c} (map-over-map (fn [k _] k) {:a 1 :b 2 :c 3})))))
17+
18+
(deftest test-float-to-string
19+
(testing "make nice looking float strings"
20+
(is "1.337" (float-to-string 1.337))))
21+

0 commit comments

Comments
 (0)