-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
parser_rules.cljc
112 lines (96 loc) · 3.64 KB
/
parser_rules.cljc
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
(ns datascript.test.parser-rules
(:require
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
:clj [clojure.test :as t :refer [is are deftest testing]])
[datascript.core :as d]
[datascript.db :as db]
[datascript.parser :as dp]
[datascript.test.core :as tdc])
#?(:clj
(:import [clojure.lang ExceptionInfo])))
(deftest clauses
(are [form res] (= (set (dp/parse-rules form)) res)
'[[(rule ?x)
[?x :name _]]]
#{(dp/->Rule
(dp/->PlainSymbol 'rule)
[ (dp/->RuleBranch
(dp/->RuleVars nil [(dp/->Variable '?x)])
[(dp/->Pattern
(dp/->DefaultSrc)
[(dp/->Variable '?x) (dp/->Constant :name) (dp/->Placeholder)])]) ])}))
(deftest rule-vars
(are [form res] (= (set (dp/parse-rules form)) res)
'[[(rule [?x] ?y)
[_]]]
#{(dp/->Rule
(dp/->PlainSymbol 'rule)
[ (dp/->RuleBranch
(dp/->RuleVars [(dp/->Variable '?x)] [(dp/->Variable '?y)])
[(dp/->Pattern (dp/->DefaultSrc) [(dp/->Placeholder)])]) ])}
'[[(rule [?x ?y] ?a ?b)
[_]]]
#{(dp/->Rule
(dp/->PlainSymbol 'rule)
[ (dp/->RuleBranch
(dp/->RuleVars [(dp/->Variable '?x) (dp/->Variable '?y)]
[(dp/->Variable '?a) (dp/->Variable '?b)])
[(dp/->Pattern (dp/->DefaultSrc) [(dp/->Placeholder)])]) ])}
'[[(rule [?x])
[_]]]
#{(dp/->Rule
(dp/->PlainSymbol 'rule)
[ (dp/->RuleBranch
(dp/->RuleVars [(dp/->Variable '?x)] nil)
[(dp/->Pattern (dp/->DefaultSrc) [(dp/->Placeholder)])]) ])})
(is (thrown-with-msg? ExceptionInfo #"Cannot parse rule-vars"
(dp/parse-rules '[[(rule) [_]]])))
(is (thrown-with-msg? ExceptionInfo #"Cannot parse rule-vars"
(dp/parse-rules '[[(rule []) [_]]])))
(is (thrown-with-msg? ExceptionInfo #"Rule variables should be distinct"
(dp/parse-rules '[[(rule ?x ?y ?x) [_]]])))
(is (thrown-with-msg? ExceptionInfo #"Rule variables should be distinct"
(dp/parse-rules '[[(rule [?x ?y] ?z ?x) [_]]])))
)
(deftest branches
(are [form res] (= (set (dp/parse-rules form)) res)
'[[(rule ?x)
[:a]
[:b]]
[(rule ?x)
[:c]]]
#{(dp/->Rule
(dp/->PlainSymbol 'rule)
[ (dp/->RuleBranch
(dp/->RuleVars nil [(dp/->Variable '?x)])
[(dp/->Pattern (dp/->DefaultSrc) [(dp/->Constant :a)])
(dp/->Pattern (dp/->DefaultSrc) [(dp/->Constant :b)])])
(dp/->RuleBranch
(dp/->RuleVars nil [(dp/->Variable '?x)])
[(dp/->Pattern (dp/->DefaultSrc) [(dp/->Constant :c)])]) ])}
'[[(rule ?x)
[:a]
[:b]]
[(other ?x)
[:c]]]
#{(dp/->Rule
(dp/->PlainSymbol 'rule)
[ (dp/->RuleBranch
(dp/->RuleVars nil [(dp/->Variable '?x)])
[(dp/->Pattern (dp/->DefaultSrc) [(dp/->Constant :a)])
(dp/->Pattern (dp/->DefaultSrc) [(dp/->Constant :b)])]) ])
(dp/->Rule
(dp/->PlainSymbol 'other)
[ (dp/->RuleBranch
(dp/->RuleVars nil [(dp/->Variable '?x)])
[(dp/->Pattern (dp/->DefaultSrc) [(dp/->Constant :c)])]) ])}
)
(is (thrown-with-msg? ExceptionInfo #"Rule branch should have clauses"
(dp/parse-rules '[[(rule ?x)]])))
(is (thrown-with-msg? ExceptionInfo #"Arity mismatch"
(dp/parse-rules '[[(rule ?x) [_]]
[(rule ?x ?y) [_]]])))
(is (thrown-with-msg? ExceptionInfo #"Arity mismatch"
(dp/parse-rules '[[(rule ?x) [_]]
[(rule [?x]) [_]]])))
)