Source

dram / test / dram / test / parser.clj

Full commit
  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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
(ns dram.test.parser
  (:require [dram.parser :as p]
            [clojure.test :refer :all]
            [the.parsatron :refer [run]]))


(defmacro is-error [input parser]
  `(~'is (~'thrown? RuntimeException (run ~parser ~input))))

(defmacro parses-as [input parser output]
  `(~'is (~'= ~output (run ~parser ~input))))

(defmacro testing-parser [parser desc & data]
  (let [pairs (partition 2 data)]
    (apply list 'testing desc
           (map (fn [[input output]]
                  `(parses-as ~input ~parser ~output))
                pairs))))

(defmacro testing-parser-errors [parser desc & data]
  (apply list 'testing desc
         (map (fn [input]
                `(is-error ~input ~parser))
              data)))


(deftest optional-test
  (testing-parser
    (p/optional (p/literal-integer))
    "The optional parser makes another parser optional!"

    "42" 42
    "a"  nil))

(deftest whitespace-test
  (testing-parser
    (p/optional-whitespace) "Optional whitespace parses to nil."

    ""       nil
    "     "  nil
    "\t"     nil
    "\n"     nil
    "\n \t " nil)

  (testing-parser
    (p/required-whitespace) "Required whitespace parses to nil."

    "     "  nil
    "\t"     nil
    "\n"     nil
    "\n \t " nil)

  (testing-parser-errors
    (p/required-whitespace) "Required whitespace is actually required."

    ""
    "foo"))

(deftest integer-test
  (testing-parser
    (p/literal-integer) "Parsing integers results in Clojure integers."

    "1"    1
    "10"   10
    "9234" 9234)

  (testing-parser
    (p/literal-integer) "Parsing negative integers results in integers too."

    "-1"    -1
    "-2945" -2945)

  (testing-parser-errors
    (p/literal-integer) "Parsing garbage with the integer parser fails."

    ""
    " 1"
    "foo"
    "-"
    " -1"
    "- 1"
    "-a12"))

(deftest string-test
  (testing-parser
    (p/literal-string)
    "Literal strings of simple characters parse to Clojure strings."

    "\"\""      ""
    "\"foo\""   "foo"
    "\" bar \"" " bar ")

  (testing-parser
    (p/literal-string)
    "Escape sequences are supported in strings."

    "\"a\\nb\""   "a\nb"
    "\"a\\\\b\""  "a\\b"
    "\"a\\\\nb\"" "a\\nb"
    "\"a\\\"b\""  "a\"b")

  (testing-parser-errors
    (p/literal-string)
    "Garbage doesn't parse as strings."

    "foo"
    "\"foo"
    "foo\"")

  (testing-parser
    (p/literal-string)
    "Parses the first bit as a string, so it should succeed (for now)."

    "\"fo\"o\"" "fo"))

(deftest literal-test
  (testing-parser
    (p/literal) "Literals can parse integers."

    "-42" -42
    "585" 585)

  (testing-parser
    (p/literal) "Literals can parse strings."

    "\"foo\"" "foo"))

(deftest value-test
  (letfn [(v [base filters]
            {:type :value :base base :filters filters})
          (f [path args]
            {:path path :args args})]
    (testing-parser
      (p/value)
      "A context value's base can be a path or a literal."

      "42"         (v 42 [])
      "\"foo\""    (v "foo" [])
      "user.email" (v ["user" "email"] [])
      "users.0"    (v ["users" "0"] []))

    (testing-parser
      (p/value)
      "A context value can be filtered through one or more filters."

      "42|abs"        (v 42 [(f ["abs"] [])])
      "42|math.floor" (v 42 [(f ["math" "floor"] [])])

      "\"foo\"|reverse|upper"
      (v "foo" [(f ["reverse"] [])
                (f ["upper"] [])])

      "\"foo\"|reverse|upper|custom.dogs"
      (v "foo" [(f ["reverse"] [])
                (f ["upper"] [])
                (f ["custom" "dogs"] [])]))

    (testing-parser
      (p/value)
      "Filters can take arguments."

      "total|add:extra.widgets"
      (v ["total"] [(f ["add"] [["extra" "widgets"]])])

      "description|trim:10"
      (v ["description"] [(f ["trim"] [10])])

      "description|slice:0,30"
      (v ["description"] [(f ["slice"] [0 30])])

      "user.join-date|date:\"yyyy-mm\",\"est\""
      (v ["user" "join-date"] [(f ["date"] ["yyyy-mm" "est"])])

      "number_of_cats|pluralize:\"y,ies\""
      (v ["number_of_cats"] [(f ["pluralize"] ["y,ies"])])

      "foo|join:\",\"|strip:\" ,.{}\"|slice:20,30,2|length"
      (v ["foo"] [(f ["join"] [","])
                  (f ["strip"] [" ,.{}"])
                  (f ["slice"] [20 30 2])
                  (f ["length"] [])]))))

(deftest variable-test
  (testing-parser
    (p/variable) "Variables can be simple literals."

    "{{ 42 }}"      42
    "{{ -2 }}"      -2
    "{{ \"foo\" }}" "foo")

  (testing-parser
    (p/variable) "Variables can handle wonky whitespace."

    "{{42}}"         42
    "{{ 42}}"        42
    "{{42 }}"        42
    "{{42  }}"       42
    "{{\n\t\n\t42}}" 42))

(deftest extends-test
  (testing-parser
    (p/tag-extends)
    "{% extends ... %} parses to its own custom AST element."

    "{% extends \"p\" %}"        {:type :extends :path "p"}
    "{% extends \"foo/bar\" %}"  {:type :extends :path "foo/bar"})

  (testing-parser-errors
    (p/tag-extends)
    "{% extends ... %} requires a non-empty argument."

    "{% extends \"\" %}"
    "{% extends %}")

  (testing-parser-errors
    (p/tag-extends)
    "{% extends ... %} doesn't accept garbage."

    "{% extends foo %}"
    "{% extends \"foo\" foo %}"
    "{% extends foo \"foo\" %}"
    "{% extends foo\"foo\" %}"
    "{% extends 43 %}"
    "{% extends foo/bar %}"
    "{% extends the quick brown fox %}"))

(deftest block-test
  (testing-parser
    (p/tag-block-open)
    "{% block ... %} parses to an intermediate AST element."

    "{% block cats %}"            {:name "cats"}
    "{% block boots-and-cats %}"  {:name "boots-and-cats"}
    "{% block hello-world_585 %}" {:name "hello-world_585"}
    "{% block a %}"               {:name "a"}
    "{% block a_ %}"              {:name "a_"})

  (testing-parser-errors
    (p/tag-block-open)
    "{% block ... %} requires valid block names."

    "{% block 1 %}"
    "{% block -1 %}"
    "{% block -foo %}"
    "{% block __foo %}"
    "{% block 12dogs %}"
    "{% block c&ats %}"
    "{% block boots and cats %}"
    "{% block \"rochester-made\" %}"
    "{% block dogs* %}"
    "{% block dogs% %}"
    "{% block dogs} %}")

  (testing-parser
    (p/tag-block-open)
    "{% block ... %} allows wonky whitespace."

    "{%block foo%}"           {:name "foo"}
    "{%   block foo%}"        {:name "foo"}
    "{%block      foo     %}" {:name "foo"}
    "{%\n\nblock\tfoo\n%}"    {:name "foo"})

  (testing-parser-errors
    (p/tag-block-open)
    "{% block ... %} REQUIRES whitespace between block and the name."

    "{% blockfoo %}")

  (testing-parser
    (p/tag-block-close)
    "{% endblock %} parses to nil and allows weird whitespace."

    "{% endblock %}"   nil
    "{%\nendblock\t%}" nil
    "{%endblock %}"    nil
    "{% endblock%}"    nil)

  (testing-parser-errors
    (p/tag-block-close)
    "{% endblock %} does NOT take a block name (for now)."

    "{% endblock foo %}")

  (testing-parser
    (p/tag-block)
    "Empty blocks are totally fine."

    "{% block foo %}{% endblock %}" {:type :block :name "foo" :contents []})

  (testing-parser
    (p/tag-block)
    "Blocks can contain anything except other blocks."

    "{% block foo %}hi{% endblock %}"
    {:type :block :name "foo" :contents ["hi"]}

    "{% block foo %}hi {{ 1 }} five{% endblock %}"
    {:type :block :name "foo" :contents ["hi " 1 " five"]}))

(deftest raw-text-test
  (testing-parser
    (p/raw-text) "Raw text parses to a Clojure string."

    "Hello"             "Hello"
    "hello there world" "hello there world"
    "  { foo } is okay" "  { foo } is okay"
    "so is { % foo % }" "so is { % foo % }")

  (testing-parser
    (p/raw-text) "Reserved characters do not parse as raw text."

    "Hello{{ world }}"       "Hello"
    "Hello{% block world %}" "Hello")

  (testing-parser-errors
    (p/raw-text) "Raw text is not zero-length."

    "{{ world }}"
    ""))

(deftest path-test
  (testing-parser
    (p/path) "A path parses to a seq of strings."

    "hello"             ["hello"]
    "hello.world"       ["hello" "world"]
    "users.0"           ["users" "0"]
    "users.0.full-name" ["users" "0" "full-name"]
    "0"                 ["0"]
    "user.full_name"    ["user" "full_name"])

  (testing-parser-errors
    (p/path) "A path can't parse garbage."

    "/foo"
    "..oo"))

(deftest template-chunk-test
  (testing-parser
    (p/template-chunk) "A template chunk can be raw text."

    "Hello"     "Hello"
    "  { foo }" "  { foo }")

  (testing-parser
    (p/template-chunk) "A template chunk can be a variable."

    "{{ 1 }}" 1))

(deftest template-base-test
  (letfn [(bt [contents]
            {:type :base :contents contents})]
    (testing-parser
      (p/template-base)
      "A base template can be made up of raw text, variables, ...."

      ""                        (bt [])
      "Hello"                   (bt ["Hello"])
      "Hello {{ \"Steve\" }}"   (bt ["Hello " "Steve"])
      "Age: {{ 27 }} years old" (bt ["Age: " 27 " years old"]))

    (testing-parser
      (p/template-base)
      "A base template can contain blocks."

      "{% block foo %}{% endblock %}"
      (bt [{:type :block :name "foo" :contents []}])

      "hello {% block username %}{% endblock %}"
      (bt ["hello "
           {:type :block :name "username" :contents []}])

      "foo {% block a %}{% endblock %} bar {{ 42 }}"
      (bt ["foo "
           {:type :block :name "a" :contents []}
           " bar "
           42]))))

(deftest template-child-test
  (letfn [(ct [extends blocks]
            {:type :child :extends extends :blocks blocks})]
    (testing-parser
      (p/template-child)
      "A child template requires an extends tag."

      "{% extends \"a\" %}"     (ct "a" {})
      "  {% extends \"a\" %}"   (ct "a" {})
      "{% extends \"a\" %}\n\n" (ct "a" {}))

    (testing-parser
      (p/template-child)
      "A child template may contain blocks to override."

      "
      {% extends \"a\" %}
      {% block foo %}{% endblock %}
      "
      (ct "a" {"foo" []})

      "
      {% extends \"a\" %}
      {% block foo %}hello world{% endblock %}
      {% block bar %}{{ 10 }}{% endblock %}
      "
      (ct "a" {"foo" ["hello world"]
               "bar" [10]}))))