summaryrefslogtreecommitdiff
path: root/test/dompa/coordinates_test.cljc
blob: eb118d291f992ab4e7b7c9ec4ecf4703db437bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
(ns dompa.coordinates-test
  (:require [clojure.test :refer [deftest testing is]]
            [dompa.coordinates :as coordinates]))

(deftest compose-test
  (testing "Create coordinates"
    (let [html "<div>hello</div>"]
      (is (= {:html html
              :coordinates [[0 4] [5 9] [10 15]]}
             (coordinates/compose html)))))

  (testing "Create coordinates with invalid HTML"
    (let [html "<div>hello"]
      (is (= {:html html
              :coordinates [[0 4] [5 9]]}
             (coordinates/compose html))))

    (let [html "<div>hello</span>"]
      (is (= {:html html
              :coordinates [[0 4] [5 9] [10 16]]}
             (coordinates/compose html))))

    (let [html "<div"]
      (is (= {:html html
              :coordinates []}
             (coordinates/compose html))))

    (let [html "div>"]
      (is (= {:html html
              :coordinates [[0 3]]}
             (coordinates/compose html))))

    (let [html "<>"]
      (is (= {:html html
              :coordinates [[0 1]]}
             (coordinates/compose html)))))

  (testing "Create coordinates with just text"
    (let [html "hello"]
      (is (= {:html html
              :coordinates [[0 4]]}
             (coordinates/compose html)))))

  (testing "Create coordinates with text starting"
    (let [html "hello<div></div>"]
      (is (= {:html html
              :coordinates [[0 4] [5 9] [10 15]]}
             (coordinates/compose html)))))

  (testing "Create coordinates with text ending"
    (let [html "<div></div>hello"]
      (is (= {:html html
              :coordinates [[0 4] [5 10] [11 15]]}
             (coordinates/compose html))))))

(deftest unify-test
  (testing "Unify coordinates"
    (let [html "<div>hello</div>"]
      (is (= {:html html
              :coordinates [[0 15] [5 9]]}
             (-> (coordinates/compose html)
                 coordinates/unify)))))

  (testing "Unify coordinates with invalid HTML"
    (let [html "<div>hello"]
      (is (= {:html html
              :coordinates [[0 4] [5 9]]}
             (-> (coordinates/compose html)
                 coordinates/unify))))

    (let [html "<div>hello</span>"]
      (is (= {:html html
              :coordinates [[0 4] [5 9]]}
             (-> (coordinates/compose html)
                 coordinates/unify)))))

  (testing "Unify coordinates with just text"
    (let [html "hello"]
      (is (= {:html html
              :coordinates [[0 4]]}
             (-> (coordinates/compose html)
                 coordinates/unify)))))

  (testing "Unify coordinates with text starting"
    (let [html "hello<div></div>"]
      (is (= {:html html
              :coordinates [[0 4] [5 15]]}
             (-> (coordinates/compose html)
                 coordinates/unify)))))

  (testing "Unify coordinates with text ending"
    (let [html "<div></div>hello"]
      (is (= {:html html
              :coordinates [[0 10] [11 15]]}
             (-> (coordinates/compose html)
                 coordinates/unify))))))

(deftest nodes-test
  (testing "Create nodes"
    (is (= [{:name :div
             :attrs {}
             :children [{:name :dompa/text
                         :value "hello"}]}]
           (-> "<div>hello</div>"
               coordinates/compose
               coordinates/unify
               coordinates/->nodes))))

  (testing "Create nodes with attributes"
    (is (= [{:attrs {:class "some test classes"
                     :data-attr "something"
                     :checked true}
             :children [{:name :dompa/text
                         :value "hello"}]
             :name :div}]
           (-> "<div class=\"some test classes\" data-attr=\"something\" checked>hello</div>"
               coordinates/compose
               coordinates/unify
               coordinates/->nodes)))))