summaryrefslogtreecommitdiff
path: root/test/nrepl-dict-tests.el
blob: 0011ff7c4a55e847b61c5bae5be7453579b3f9bf (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; nrepl-dict-tests.el

;; Copyright © 2012-2018 Tim King, Bozhidar Batsov

;; Author: Tim King <kingtim@gmail.com>
;;         Bozhidar Batsov <bozhidar@batsov.com>
;;         Artur Malabarba <bruce.connor.am@gmail.com>

;; This file is NOT part of GNU Emacs.

;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see `http://www.gnu.org/licenses/'.

;;; Commentary:

;; This file is part of CIDER

;;; Code:

(require 'buttercup)
(require 'nrepl-dict)

(describe "nrepl-dict-merge"
  :var (input)
  (before-each
    (setq input '(dict 2 4 1 2 "10" "90" "a" "b")))

  (it "merges dictionaries"
    (expect (nrepl-dict-merge input '(dict 1 3 "10" me)) :to-equal '(dict 2 4 1 3 "10" me "a" "b"))
    (expect input :to-equal '(dict 2 4 1 3 "10" me "a" "b")))

  (it "handles nil values"
    (expect (nrepl-dict-merge nil '(dict 1 3 "10" me)) :to-equal '(dict 1 3 "10" me))
    (expect (nrepl-dict-merge '(dict 1 3 "10" me) nil) :to-equal '(dict 1 3 "10" me))))

(describe "nrepl-dict-contains"
  :var (input)

  (it "returns non-nil if dict contains the element"
    (setq input '(dict 1 "a" 2 "B" "3" "d" 4 nil sym yes nil nil-val))

    (expect (nrepl-dict-contains input 1) :to-be-truthy)
    (expect (nrepl-dict-contains input 2) :to-be-truthy)
    (expect (nrepl-dict-contains input "3") :to-be-truthy)
    (expect (nrepl-dict-contains input 4) :to-be-truthy)
    (expect (nrepl-dict-contains input 'sym) :to-be-truthy))

  (it "allows `nil' to be a key in the nREPL dict"
    (setq input '(dict 1 "a" 2 "B" "3" "d" 4 nil sym yes nil nil-val))
    (expect (nrepl-dict-contains input nil) :to-be-truthy))

  (it "returns `nil' if dict doesnt contain the element"
    (setq input '(dict 1 "a" 2 "B" "3" "d" 4 nil sym yes))

    (expect (nrepl-dict-contains input 11) :to-equal nil)
    (expect (nrepl-dict-contains input 12) :to-equal nil)
    (expect (nrepl-dict-contains input "13") :to-equal nil)
    (expect (nrepl-dict-contains input 14) :to-equal nil)
    (expect (nrepl-dict-contains input 'missing) :to-equal nil)
    (expect (nrepl-dict-contains input nil) :to-equal nil)))

(describe "nrepl-dict-get"
  :var (input)

  (describe "when key is present in the dict"
    (before-all
      (setq input '(dict 1 "a" 2 "B" "3" "d" 4 nil sym yes nil nil-val)))

    (it "returns the corresponding value"
      (expect (nrepl-dict-get input 1) :to-equal "a")
      (expect (nrepl-dict-get input 2) :to-equal "B")
      (expect (nrepl-dict-get input "3") :to-equal "d")
      (expect (nrepl-dict-get input 4) :to-equal nil)
      (expect (nrepl-dict-get input 'sym) :to-equal'yes)
      (expect (nrepl-dict-get input nil) :to-equal 'nil-val))

    (it "ignores the default value, if given"
      (expect (nrepl-dict-get input 1 "default") :to-equal "a")
      (expect (nrepl-dict-get input 2 "default") :to-equal "B")
      (expect (nrepl-dict-get input "3" "default") :to-equal "d")
      (expect (nrepl-dict-get input 4 "default") :to-equal nil)
      (expect (nrepl-dict-get input 'sym "default") :to-equal'yes)
      (expect (nrepl-dict-get input nil "default") :to-equal 'nil-val)))

  (describe "when key is not present in the dict"
    (before-all
      (setq input '(dict 1 "a" 2 "B" "3" "d" 4 nil sym yes)))

    (it "returns nil, when default value is not given"
      (expect (nrepl-dict-get input 11) :to-equal nil)
      (expect (nrepl-dict-get input "13") :to-equal nil)
      (expect (nrepl-dict-get input 14) :to-equal nil)
      (expect (nrepl-dict-get input 'missing) :to-equal nil)
      (expect (nrepl-dict-get input nil) :to-equal nil))

    (it "returns the default value, if given"
      (expect (nrepl-dict-get input 11 "default") :to-equal "default")
      (expect (nrepl-dict-get input 21 "default") :to-equal "default")
      (expect (nrepl-dict-get input "31" "default") :to-equal "default")
      (expect (nrepl-dict-get input 41 "default") :to-equal "default")
      (expect (nrepl-dict-get input 'missing "default") :to-equal "default")
      (expect (nrepl-dict-get input nil "default") :to-equal "default"))))

(describe "nrepl--cons"
  (it "cons's the obj onto the list or dict"
    (expect (nrepl--cons '(23 . 44) '(dict (2 . 3) (3 . 4) (4 . 5)))
            :to-equal '(dict (23 . 44) (2 . 3) (3 . 4) (4 . 5)))))

(describe "nrepl--push"
  (it "cons's the obj to the top element of the stack"
    (expect (nrepl--push 34 '(())) :to-equal '((34)))
    (expect (nrepl--push '(34) '(() (1 2 3))) :to-equal '(((34)) (1 2 3)))
    (expect (nrepl--push 34 '((1))) :to-equal '((34 1)))
    (expect (nrepl--push 34 '((1) (2))) :to-equal '((34 1) (2)))
    (expect (nrepl--push '(34) '((1) (2))) :to-equal '(((34) 1) (2)))
    (expect (nrepl--push 34 '((dict a b) (2))) :to-equal '((dict 34 a b) (2)))))

(describe "nrepl--keys"
  (it "returns all keys in the nREPL dict"
    (expect (nrepl-dict-keys '(dict (2 . 3) (3 . 4) (4 . 5)))
            :to-equal '((2 . 3) (4 . 5)))))

(describe "nrepl--vals"
  (it "returns all values in the nREPL dict"
    (expect (nrepl-dict-vals '(dict (2 . 3) (3 . 4) (4 . 5)))
            :to-equal '((3 . 4) nil))))

(describe "nrepl--map"
  (it "maps a fn over all key-value pairs of a dict"
    (expect (nrepl-dict-map (lambda (k v) (+ k v))
                            '(dict 0 1 2 3 4 5))
            :to-equal '(1 5 9))))

(describe "nrepl--merge"
  :var (dict1 dict2)
  (it "preserves id and session keys of dict1"
    (setq dict1 '(dict "id" 1 "session" 1 "blah" (1 2))
          dict2 '(dict "id" 2 "session" 2))
    (expect (nrepl--merge dict1 dict2)
            :to-equal '(dict "id" 1 "session" 1 "blah" (1 2))))

  (it "appends all other keys"
    (setq dict1 '(dict "id" 1 "session" 1 "blah" (1 2) "x" "aaa" "y" (dict "z" "A"))
          dict2 '(dict "id" 2 "session" 2 "blah" (3 4) "x" "AAA" "y" (dict "z" "B")))
    (expect (nrepl--merge dict1 dict2)
            :to-equal '(dict "id" 1 "session" 1 "blah" (1 2 3 4) "x" "aaaAAA" "y" (dict "z" "AB"))))

  (it "dict1 is updated destructively"
    (setq dict1 '(dict "id" 1 "session" 1 "blah" (1 2))
          dict2 '(dict "id" 2 "session" 2 "blah" (3 4))
          dict3 '(dict "id" 1 "session" 1 "blah" (1 2)))
    (nrepl--merge dict1 dict2)
    (expect dict1 :not :to-equal dict3)))