Skip to content

Commit 0c98f68

Browse files
authored
Merge pull request #21 from PractiTest/issue_6221_FC_methods_enhancement
issue_6221_FC_methods_enhancement
2 parents 4e21943 + e69f8fe commit 0c98f68

File tree

3 files changed

+145
-111
lines changed

3 files changed

+145
-111
lines changed

src/practitest_firecracker/practitest.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
;; ===========================================================================
1515
;; api version
16-
(def ^:const fc-version "2.0.6")
16+
(def ^:const fc-version "2.1.0")
1717

1818
;; ===========================================================================
1919
;; utils
@@ -349,19 +349,19 @@
349349
tests)}))
350350

351351
(defn eval-additional-fields [suite additional-fields]
352-
(postwalk #(if (query? %) (eval-query suite {} %) %)
352+
(postwalk #(if (query? %) (eval-query suite %) %)
353353
additional-fields))
354354

355355
(defn eval-additional-testset-fields [suite additional-fields]
356-
(postwalk #(if (query? %) (eval-query suite {} %) %)
356+
(postwalk #(if (query? %) (eval-query suite %) %)
357357
additional-fields))
358358

359359
(defn sf-test-suite->pt-test-name [options suite]
360-
(let [test-name (eval-query suite {} (:pt-test-name options))]
360+
(let [test-name (eval-query suite (:pt-test-name options))]
361361
(if (string/blank? test-name) "UNNAMED" test-name)))
362362

363363
(defn sf-test-case->pt-step-name [options test-case]
364-
(let [step-name (eval-query {} test-case (:pt-test-step-name options))]
364+
(let [step-name (eval-query test-case (:pt-test-step-name options))]
365365
(if (string/blank? step-name) "UNNAMED" step-name)))
366366

367367
(defn sf-test-case->step-def [options test-case]

src/practitest_firecracker/query_dsl.clj

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
(ns practitest-firecracker.query-dsl
2+
(:require
3+
[clojure.string :as string]
4+
[clojure.edn :as edn]))
5+
6+
7+
(defn fix-abbreviations [tokens]
8+
(loop [results [] input tokens]
9+
(if (empty? input)
10+
results
11+
(let [[head & tail] input]
12+
(if (and (= 1 (count head))
13+
(not-empty (last results))
14+
(every? #?(:clj #(Character/isUpperCase %)
15+
:cljs #(.toUpperCase %))
16+
(last results)))
17+
(recur (conj (vec (drop-last results))
18+
(str (last results) head))
19+
tail)
20+
(recur (conj results head) tail))))))
21+
22+
(defn parse-int [s]
23+
#?(:clj (Integer. (re-find #"\d+" s ))
24+
:cljs (js/parseInt s 10)))
25+
26+
(defn return-error [error-message query]
27+
#?(:clj (throw
28+
(ex-info error-message
29+
{:query query}))
30+
:cljs (str error-message query)))
31+
32+
(defn parse-methods [op args query]
33+
(condp = op
34+
'tokenize-package (if (= 1 (count args))
35+
(string/split (first args) #"\.")
36+
(return-error "Syntax error: 'tokenize-package' must have one argument" query))
37+
'tokenize-class-name (if (= 1 (count args))
38+
(->> (string/split (first args) #"(?=[A-Z])")
39+
(map #(string/replace % "_" ""))
40+
(remove string/blank?)
41+
fix-abbreviations)
42+
(return-error "Syntax error: 'tokenize-class-name' must have one argument" query))
43+
'take (if (= 2 (count args))
44+
(if (or (string? (last args)) (coll? (last args)))
45+
(take (parse-int (first args)) (last args))
46+
(return-error "Syntax error: 'take' second argument has to be ISeqable (String, Array etc.)" query))
47+
(return-error "Syntax error: 'take' must have two arguments" query))
48+
'drop (if (= 2 (count args))
49+
(if (or (string? (last args)) (coll? (last args)))
50+
(drop (parse-int (first args)) (last args))
51+
(return-error "Syntax error: 'drop' second argument has to be ISeqable (String, Array etc.)" query))
52+
(return-error "Syntax error: 'drop' must have two arguments" query))
53+
'drop-last (if (<= 1 (count args) 2)
54+
(if (or (string? (last args)) (coll? (last args)))
55+
(apply drop-last args)
56+
(return-error "Syntax error: 'drop-last' second argument has to be ISeqable (String, Array etc.)" query))
57+
(return-error "Syntax error: 'drop-last' must have one or two arguments" query))
58+
'take-last (case (count args)
59+
1 (take-last 1 (first args))
60+
2 (take-last (first args) (second args))
61+
(return-error "Syntax error: 'take-last' must have one or two arguments" query))
62+
'concat (apply str args)
63+
'capitalize (if (= 1 (count args))
64+
(if (or (string? (last args)) (coll? (last args)))
65+
(map string/capitalize (first args))
66+
(return-error "Syntax error: 'capitalize' argument has to be ISeqable (String, Array etc.)" query))
67+
(return-error "Syntax error: 'capitalize' must have one argument" query))
68+
'join (if (= 1 (count args))
69+
(if (or (string? (last args)) (coll? (last args)))
70+
(string/join (first args))
71+
(return-error "Syntax error: 'join' argument has to be ISeqable (String, Array etc.)" query))
72+
(return-error "Syntax error: 'join' must have one argument" query))
73+
'split (if (= 2 (count args))
74+
(let [quoted #?(:clj (string/escape (first args) char-escape-string)
75+
:cljs (first args))
76+
complied #?(:clj (java.util.regex.Pattern/compile quoted)
77+
:cljs (js/RegExp. quoted))]
78+
(string/split (second args) complied))
79+
(return-error "Syntax error: 'split' must have two arguments" query))
80+
'get (if (= 2 (count args))
81+
(first (take 1 (drop (- (parse-int (first args)) 1) (last args))))
82+
(return-error "Syntax error: 'get' must have two arguments" query))
83+
'trim (if (= 1 (count args))
84+
(if (string? (first args))
85+
(string/trim (first args))
86+
(return-error "Syntax error: 'trim' should be applied on a string" query))
87+
(return-error "Syntax error: 'trim' must have only one argument" query))
88+
(return-error #?(:clj (format "Syntax error: unsupported function '%s'" op)
89+
:cljs (str "Syntax error: unsupported function: " op)) query)))
90+
91+
(defn eval-query [entity-hash query]
92+
(if (map? query)
93+
(let [{:keys [op args]} query
94+
args (map (partial eval-query entity-hash) args)]
95+
(parse-methods op args query))
96+
#?(:cljs (cond
97+
(= '?field query) entity-hash
98+
(string/starts-with? (str query) "?") (throw
99+
(ex-info (str "Syntax error: unsupported variable " query)
100+
{:query query}))
101+
(number? query) query
102+
:else (str query))
103+
:clj (let [key (keyword (string/join (drop 1 (str query))))]
104+
(cond
105+
(or (= :test-suite-name key)
106+
(= :test-case-name key)) (:name entity-hash)
107+
(and (not (= entity-hash nil))
108+
(contains? entity-hash key)) (key entity-hash)
109+
(string/starts-with? (str query) "?") (str "")
110+
:else (str query))))))
111+
112+
(defn read-query [s]
113+
(let [query (edn/read-string s)
114+
compiler (fn compile-query [query]
115+
(if (list? query)
116+
(let [[op & args] query]
117+
{:op (compile-query op)
118+
:args (vec (map compile-query args))})
119+
query))]
120+
#?(:cljs (if (and (not (number? query)) (not (string? query)))
121+
(with-meta (compiler query)
122+
{:query true})
123+
(compiler query))
124+
:clj (if (not (or (= java.lang.Long (type query))
125+
(= java.lang.Double (type query))
126+
(and (= java.lang.String (type query)))))
127+
(with-meta (compiler query)
128+
{:query true})
129+
(compiler query)))))
130+
131+
(defn try-read-query [s]
132+
#?(:cljs
133+
(try
134+
(read-query s)
135+
(catch js/Error e (str "caught exception: " e))
136+
(catch js/Object e
137+
(str "Error: " e)))))
138+
139+
(defn query? [obj]
140+
(boolean (:query (meta obj))))

0 commit comments

Comments
 (0)