[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/parseclj c906de33a1 048/185: Rewrite all tests, and add ne
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/parseclj c906de33a1 048/185: Rewrite all tests, and add new tests for the AST "printer" |
Date: |
Tue, 28 Dec 2021 14:05:15 -0500 (EST) |
branch: elpa/parseclj
commit c906de33a1c914bf3756f45063d77ebbf17a14fa
Author: Daniel Barreto <dbarreto@talpor.com>
Commit: Daniel Barreto <dbarreto@talpor.com>
Rewrite all tests, and add new tests for the AST "printer"
---
clj-parse-test.el | 529 +++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 360 insertions(+), 169 deletions(-)
diff --git a/clj-parse-test.el b/clj-parse-test.el
index f10c0e4f63..a9888933ce 100644
--- a/clj-parse-test.el
+++ b/clj-parse-test.el
@@ -22,177 +22,368 @@
;;; Code:
-(require 'clj-parse)
+(require 'a)
(require 'ert)
+(require 'clj-parse)
-(defmacro clj-parse-eq-test (parse-to-fn test-string expected)
- `(with-temp-buffer
- (insert ,test-string)
- (goto-char 1)
- (should (equal (,parse-to-fn) ,expected))))
-
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; To Elisp code, ala edn.el
-
-(ert-deftest clj-parse-to-elisp-simple-list ()
- (clj-parse-eq-test clj-parse-to-elisp
- "(1 2 3)"
- '((1 2 3))))
-
-(ert-deftest clj-parse-to-elisp-empty-list ()
- (clj-parse-eq-test clj-parse-to-elisp
- "()"
- '(())))
-
-(ert-deftest clj-parse-to-elisp-list-size-1 ()
- (clj-parse-eq-test clj-parse-to-elisp
- "(1)"
- '((1))))
-
-(ert-deftest clj-parse-to-elisp-leafs ()
- (clj-parse-eq-test clj-parse-to-elisp
- "(nil true false hello-world)"
- '((nil t nil hello-world))))
-
-(ert-deftest clj-parse-to-elisp-qualified-symbol ()
- (clj-parse-eq-test clj-parse-to-elisp
- "clojure.string/join"
- '(clojure.string/join)))
-
-(ert-deftest clj-parse-to-elisp-nested-lists ()
- (clj-parse-eq-test clj-parse-to-elisp
- "((.9 abc (true) (hello)))"
- '(((0.9 abc (t) (hello))))))
-
-(ert-deftest clj-parse-to-elisp-strings ()
- (clj-parse-eq-test clj-parse-to-elisp
- "\"abc hello \\t\\\"x\""
- '("abc hello \t\"x"))
- (clj-parse-eq-test clj-parse-to-elisp
- "(\"---\\f---\\\"-'\\'-\\\\-\\r\\n\")"
- '(("---\f---\"-''-\\-\r\n"))))
-
-(ert-deftest clj-parse-to-elisp-chars ()
- (clj-parse-eq-test clj-parse-to-elisp
- "(\\newline \\return \\space \\tab \\a \\b \\c \\u0078 \\o171)"
- '((?\n ?\r ?\ ?\t ?a ?b ?c ?x ?y)))
- (clj-parse-eq-test clj-parse-to-elisp
- "\"\\u0078 \\o171\""
- '("x y")))
-
-(ert-deftest clj-parse-to-elisp-keywords ()
- (clj-parse-eq-test clj-parse-to-elisp
- ":foo-bar"
- '(:foo-bar)))
-
-(ert-deftest clj-parse-to-elisp-vector ()
- (clj-parse-eq-test clj-parse-to-elisp
- "[123]"
- '([123])))
-
-(ert-deftest clj-parse-to-elisp-map ()
- (clj-parse-eq-test clj-parse-to-elisp
- "{:count 123}"
- '(((:count . 123)))))
-
-(ert-deftest clj-parse-to-elisp-set ()
- (clj-parse-eq-test clj-parse-to-elisp
- "#{:x}"
- '((:x))))
-
-(ert-deftest clj-parse-to-elisp-discarded ()
- (clj-parse-eq-test clj-parse-to-elisp
- "(10 #_11 12 #_#_ 13 14)"
- '((10 12 14))))
-
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; To Clojure/EDN string
-
-(ert-deftest clj-parse-to-string-simple-list ()
- (clj-parse-eq-test clj-parse-to-string
- "( 1 2 3)"
- "(1 2 3)"))
-
-(ert-deftest clj-parse-to-string-empty-list ()
- (clj-parse-eq-test clj-parse-to-string
- "()"
- "()"))
-
-(ert-deftest clj-parse-to-string-list-size-1 ()
- (clj-parse-eq-test clj-parse-to-string
- "(1)"
- "(1)"))
-
-(ert-deftest clj-parse-to-string-leafs ()
- (clj-parse-eq-test clj-parse-to-string
- "(nil true false hello-world)"
- "(nil true false hello-world)"))
-
-(ert-deftest clj-parse-to-string-qualified-symbol ()
- (clj-parse-eq-test clj-parse-to-string
- "clojure.string/join"
- "clojure.string/join"))
-
-(ert-deftest clj-parse-to-string-nested-lists ()
- (clj-parse-eq-test clj-parse-to-string
- "((.9 abc (true) (hello)))"
- "((.9 abc (true) (hello)))"))
-
-(ert-deftest clj-parse-to-string-strings ()
- (clj-parse-eq-test clj-parse-to-string
- "\"abc hello \\t\\\"x\""
- "\"abc hello \\t\\\"x\"")
- (clj-parse-eq-test clj-parse-to-string
- "(\"---\\f---\\\"-'\\'-\\\\-\\r\\n\")"
- "(\"---\\f---\\\"-'\\'-\\\\-\\r\\n\")"))
-
-(ert-deftest clj-parse-to-string-chars ()
- (clj-parse-eq-test clj-parse-to-string
- "(\\newline \\return \\space \\tab \\a \\b \\c \\u0078 \\o171)"
- "(\\newline \\return \\space \\tab \\a \\b \\c \\u0078 \\o171)")
- (clj-parse-eq-test clj-parse-to-string
- "\"\\u0078 \\o171\""
- "\"\\u0078 \\o171\""))
-
-(ert-deftest clj-parse-to-string-keywords ()
- (clj-parse-eq-test clj-parse-to-string
- ":foo-bar"
- ":foo-bar"))
-
-(ert-deftest clj-parse-to-string-vector ()
- (clj-parse-eq-test clj-parse-to-string
- "[123]"
- "[123]"))
-
-(ert-deftest clj-parse-to-string-map ()
- (clj-parse-eq-test clj-parse-to-string
- "{:count 123}"
- "{:count 123}"))
-
-(ert-deftest clj-parse-to-string-set ()
- (clj-parse-eq-test clj-parse-to-string
- "#{:x}"
- "#{:x}"))
-
-(ert-deftest clj-parse-to-string-discarded ()
- (clj-parse-eq-test clj-parse-to-string
- "(10 #_11 12 #_#_ 13 14)"
- "(10 12 14)"))
-
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; AST
-
-(ert-deftest clj-parse-ast-simple-list ()
- (clj-parse-eq-test clj-parse
- "(1 2 3)"
- '((type . :root)
- (subnodes . (((type . :list)
- (subnodes . (((type . :number) (form . "1") (pos . 2))
- ((type . :number) (form . "2") (pos . 4))
- ((type . :number) (form . "3") (pos .
6))))))))))
+(defun clj-parse--deftest-mode (mode test-name test-string expected)
+ (let* ((parse-fn (intern (concat "clj-parse-" mode)))
+ (test-name (intern (concat (symbol-name parse-fn) "-" (symbol-name
test-name)))))
+ `(ert-deftest ,test-name ()
+ (with-temp-buffer
+ (insert ,test-string)
+ (goto-char 1)
+ (should (equal (,parse-fn) ,expected))))))
+
+(defmacro clj-parse-deftest (test-name test-string mode-vs-expected-alist)
+ (declare (indent defun))
+ `(progn
+ ,@(mapcar (lambda (vs) (clj-parse--deftest-mode (car vs)
+ test-name
+ test-string
+ (cadr vs)))
+ mode-vs-expected-alist)))
+
+
+;;; Parser modes
+;; ----------------------------------------------------------------------------
+
+(clj-parse-deftest simple-list "(1 2 3)"
+ (("edn" '((1 2 3)))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . (((:node-type . :number)
+ (:position . 2)
+ (:form . "1")
+ (:value . 1))
+ ((:node-type . :number)
+ (:position . 4)
+ (:form . "2")
+ (:value . 2))
+ ((:node-type . :number)
+ (:position . 6)
+ (:form . "3")
+ (:value . 3)))))))))))
+
+
+(clj-parse-deftest empty-list "()"
+ (("edn" '(()))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . nil))))))))
+
+(clj-parse-deftest size-1 "(1)"
+ (("edn" '((1)))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . (((:node-type . :number)
+ (:position . 2)
+ (:form . "1")
+ (:value . 1)))))))))))
+
+(clj-parse-deftest leafs "(nil true false hello-world)"
+ (("edn" '((nil t nil hello-world)))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . (((:node-type . :nil)
+ (:position . 2)
+ (:form . "nil")
+ (:value . nil))
+ ((:node-type . :true)
+ (:position . 6)
+ (:form . "true")
+ (:value . t))
+ ((:node-type . :false)
+ (:position . 11)
+ (:form . "false")
+ (:value . nil))
+ ((:node-type . :symbol)
+ (:position . 17)
+ (:form . "hello-world")
+ (:value . hello-world)))))))))))
+
+(clj-parse-deftest qualified-symbol "clojure.string/join"
+ (("edn" '(clojure.string/join))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :symbol)
+ (:position . 1)
+ (:form . "clojure.string/join")
+ (:value . clojure.string/join))))))))
+
+(clj-parse-deftest nested-lists "((.9 abc (true) (hello)))"
+ (("edn" '(((0.9 abc (t) (hello)))))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . (((:node-type . :list)
+ (:position . 2)
+ (:children ((:node-type . :number)
+ (:position . 3)
+ (:form . ".9")
+ (:value . 0.9))
+ ((:node-type . :symbol)
+ (:position . 6)
+ (:form . "abc")
+ (:value . abc))
+ ((:node-type . :list)
+ (:position . 10)
+ (:children ((:node-type
. :true)
+ (:position .
11)
+ (:form .
"true")
+ (:value .
t))))
+ ((:node-type . :list)
+ (:position . 17)
+ (:children ((:node-type
. :symbol)
+ (:position .
18)
+ (:form .
"hello")
+ (:value .
hello)))))))))))))))
+
+(clj-parse-deftest strings-1 "\"abc hello \\t\\\"x\""
+ (("edn" '("abc hello \t\"x"))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :string)
+ (:position . 1)
+ (:form . "\"abc hello \\t\\\"x\"")
+ (:value . "abc hello \t\"x"))))))))
+
+(clj-parse-deftest strings-2 "(\"---\\f---\\\"-'\\'-\\\\-\\r\\n\")"
+ (("edn" '(("---\f---\"-''-\\-\r\n")))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . (((:node-type . :string)
+ (:position . 2)
+ (:form .
"\"---\\f---\\\"-'\\'-\\\\-\\r\\n\"")
+ (:value .
"---\f---\"-''-\\-\r\n")))))))))))
+
+(clj-parse-deftest chars-1 "(\\newline \\return \\space \\tab \\a \\b \\c
\\u0078 \\o171)"
+ (("edn" '((?\n ?\r ?\ ?\t ?a ?b ?c ?x ?y)))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . (((:node-type . :character) (:position
. 2) (:form . "\\newline") (:value . ?\n))
+ ((:node-type . :character) (:position
. 11) (:form . "\\return") (:value . ?\r))
+ ((:node-type . :character) (:position
. 19) (:form . "\\space") (:value . 32))
+ ((:node-type . :character) (:position
. 26) (:form . "\\tab") (:value . ?\t))
+ ((:node-type . :character) (:position
. 31) (:form . "\\a") (:value . ?a))
+ ((:node-type . :character) (:position
. 34) (:form . "\\b") (:value . ?b))
+ ((:node-type . :character) (:position
. 37) (:form . "\\c") (:value . ?c))
+ ((:node-type . :character) (:position
. 40) (:form . "\\u0078") (:value . ?x))
+ ((:node-type . :character) (:position
. 47) (:form . "\\o171") (:value . ?y)))))))))))
+
+(clj-parse-deftest chars-2 "\"\\u0078 \\o171\""
+ (("edn" '("x y"))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :string)
+ (:position . 1)
+ (:form . "\"\\u0078 \\o171\"")
+ (:value . "x y"))))))))
+
+(clj-parse-deftest keywords ":foo-bar"
+ (("edn" '(:foo-bar))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :keyword)
+ (:position . 1)
+ (:form . ":foo-bar")
+ (:value . :foo-bar))))))))
+
+(clj-parse-deftest vector "[123]"
+ (("edn" '([123]))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :vector)
+ (:position . 1)
+ (:children . (((:node-type . :number)
+ (:position . 2)
+ (:form . "123")
+ (:value . 123)))))))))))
+
+(clj-parse-deftest map "{:count 123}"
+ (("edn" '(((:count . 123))))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :map)
+ (:position . 1)
+ (:children . (((:node-type . :keyword)
+ (:position . 2)
+ (:form . ":count")
+ (:value . :count))
+ ((:node-type . :number)
+ (:position . 9)
+ (:form . "123")
+ (:value . 123)))))))))))
+
+(clj-parse-deftest set "#{:x}"
+ (("edn" '((:x)))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :set)
+ (:position . 1)
+ (:children . (((:node-type . :keyword)
+ (:position . 3)
+ (:form . ":x")
+ (:value . :x)))))))))))
+
+(clj-parse-deftest discard "(10 #_11 12 #_#_ 13 14)"
+ (("edn" '((10 12)))
+ ("ast" '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . (((:node-type . :number)
+ (:position . 2)
+ (:form . "10")
+ (:value . 10))
+ ((:node-type . :number)
+ (:position . 10)
+ (:form . "12")
+ (:value . 12)))))))))))
+
+
+;;; Printer modes
+;; ----------------------------------------------------------------------------
+
+(ert-deftest clj-parse-ast-print-list ()
+ (should (equal "(0 1 2)"
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children .
(((:node-type . :number)
+
(:position . 2)
+ (:form .
"0")
+ (:value
. 0))
+
((:node-type . :number)
+
(:position . 4)
+ (:form .
"1")
+ (:value
. 1))
+
((:node-type . :number)
+
(:position . 6)
+ (:form .
"2")
+ (:value
. 2))))))))))))
+
+(ert-deftest clj-parse-ast-print-empty-list ()
+ (should (equal "()"
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children .
nil)))))))))
+
+(ert-deftest clj-parse-ast-print-nested-list ()
+ (should (equal "((.9 abc (true) (hello)))"
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children .
(((:node-type . :list)
+
(:position . 2)
+
(:children ((:node-type . :number)
+
(:position . 3)
+
(:form . ".9")
+
(:value . 0.9))
+
((:node-type . :symbol)
+
(:position . 6)
+
(:form . "abc")
+
(:value . abc))
+
((:node-type . :list)
+
(:position . 10)
+
(:children ((:node-type . :true)
+
(:position . 11)
+
(:form . "true")
+
(:value . t))))
+
((:node-type . :list)
+
(:position . 17)
+
(:children ((:node-type . :symbol)
+
(:position . 18)
+
(:form . "hello")
+
(:value . hello))))))))))))))))
+
+(ert-deftest clj-parse-ast-print-string ()
+ (should (equal "\"abc hello \\t\\\"x\""
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :string)
+ (:position . 1)
+ (:form . "\"abc hello
\\t\\\"x\"")
+ (:value . "abc hello
\t\"x")))))))))
+
+(ert-deftest clj-parse-ast-print-chars ()
+ (should (equal "(\\newline \\return \\space \\tab \\a \\b \\c \\u0078
\\o171)"
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :list)
+ (:position . 1)
+ (:children . (((:node-type . :character) (:position
. 2) (:form . "\\newline") (:value . ?\n))
+ ((:node-type . :character) (:position
. 11) (:form . "\\return") (:value . ?\r))
+ ((:node-type . :character) (:position
. 19) (:form . "\\space") (:value . 32))
+ ((:node-type . :character) (:position
. 26) (:form . "\\tab") (:value . ?\t))
+ ((:node-type . :character) (:position
. 31) (:form . "\\a") (:value . ?a))
+ ((:node-type . :character) (:position
. 34) (:form . "\\b") (:value . ?b))
+ ((:node-type . :character) (:position
. 37) (:form . "\\c") (:value . ?c))
+ ((:node-type . :character) (:position
. 40) (:form . "\\u0078") (:value . ?x))
+ ((:node-type . :character) (:position
. 47) (:form . "\\o171") (:value . ?y))))))))))))
+
+(ert-deftest clj-parse-ast-print-keyword ()
+ (should (equal ":foo-bar"
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :keyword)
+ (:position . 1)
+ (:form . ":foo-bar")
+ (:value .
:foo-bar)))))))))
+
+(ert-deftest clj-parse-ast-print-vector ()
+ (should (equal "[123]"
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :vector)
+ (:position . 1)
+ (:children .
(((:node-type . :number)
+
(:position . 2)
+ (:form .
"123")
+ (:value
. 123))))))))))))
+
+(ert-deftest clj-parse-ast-print-map ()
+ (should (equal "{:count 123}"
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :map)
+ (:position . 1)
+ (:children .
(((:node-type . :keyword)
+
(:position . 2)
+ (:form .
":count")
+ (:value
. :count))
+
((:node-type . :number)
+
(:position . 9)
+ (:form .
"123")
+ (:value
. 123))))))))))))
+
+(ert-deftest clj-parse-ast-print-set ()
+ (should (equal "#{:x}"
+ (clj-parse-ast-print '((:node-type . :root)
+ (:position . 0)
+ (:children . (((:node-type . :set)
+ (:position . 1)
+ (:children .
(((:node-type . :keyword)
+
(:position . 3)
+ (:form .
":x")
+ (:value
. :x))))))))))))
(provide 'clj-parse-test)
- [nongnu] elpa/parseclj d5167bf4ad 021/185: Test/lint stuff, (continued)
- [nongnu] elpa/parseclj d5167bf4ad 021/185: Test/lint stuff, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 22f2eb106f 028/185: Support \uxxxx and \oxxx escape codes in strings, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 343052c01a 014/185: Add linting to the build, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 07739abe2c 030/185: support keywords, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj ced0b91c08 029/185: "Support" namespaces symbols, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 1eef0b62c8 034/185: Support #_discard forms, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 0974b56833 035/185: Stick to non CL functions, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj f8822bb43c 040/185: Add support for tags in lexer, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 43f59dcb91 043/185: Avoid dropping whitespaces, handling them while reducing, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 7733985037 047/185: DESIGN.md-related adjustments, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj c906de33a1 048/185: Rewrite all tests, and add new tests for the AST "printer",
ELPA Syncer <=
- [nongnu] elpa/parseclj 697618dbb1 049/185: Merge pull request #1 from volrath/master, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj e7686c49ed 052/185: Greater parity with edn.el, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 2da47798d9 057/185: Enable more edn.el tests, document how time/uuid are stored, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj f6de87fbe6 067/185: Split EDN and AST handling in separate files, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj d410a1530e 096/185: Add code-checking defaults to `dir-locals.el`, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 3a92eafce9 079/185: Rename reduce-node to reduce-branch, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 7beff77b15 083/185: Introduce parseclj-parse-clojure, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 3d261f5d3c 072/185: Bump version of a, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 47cf208a91 091/185: Fix parsing of tags/discard with :lexical-preservation, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj da4bacb5f5 078/185: Rename parseclj-reduce to parseclj-parse, ELPA Syncer, 2021/12/28