[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master f7e8629 26/60: Add tests
From: |
Junpeng Qiu |
Subject: |
[elpa] master f7e8629 26/60: Add tests |
Date: |
Tue, 25 Oct 2016 17:45:14 +0000 (UTC) |
branch: master
commit f7e862978e1d6f5ef6b64f5320c1380a94377487
Author: Junpeng Qiu <address@hidden>
Commit: Junpeng Qiu <address@hidden>
Add tests
---
parsec-tests.el | 429 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 429 insertions(+)
diff --git a/parsec-tests.el b/parsec-tests.el
new file mode 100644
index 0000000..5e8abd0
--- /dev/null
+++ b/parsec-tests.el
@@ -0,0 +1,429 @@
+;;; parsec-tests.el --- Tests for parsec.el -*- lexical-binding: t;
-*-
+
+;; Copyright (C) 2016 Junpeng Qiu
+
+;; Author: Junpeng Qiu <address@hidden>
+;; Keywords:
+
+;; 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:
+
+;;
+
+;;; Code:
+
+(require 'ert)
+(require 'parsec)
+
+(defun test-parsec-error-new (expected found)
+ (parsec-error-new (format "Found \"%s\" -> Expected \"%s\""
+ found expected)))
+
+(ert-deftest test-parsec-ch ()
+ (should
+ (equal
+ (parsec-with-input "ab"
+ (parsec-ch ?a)
+ (parsec-ch ?b))
+ "b"))
+ (should
+ (equal
+ (parsec-with-input "ab"
+ (parsec-query (parsec-ch ?a) :beg))
+ 1)))
+
+(ert-deftest test-parsec-satisfy ()
+ (should
+ (equal
+ (parsec-with-input "ab"
+ (parsec-ch ?a)
+ (parsec-satisfy (lambda (c) (char-equal c ?b))))
+ "b"))
+ (should
+ (equal
+ (parsec-with-input "ab"
+ (parsec-ch ?a)
+ (parsec-query (parsec-satisfy (lambda (c) (char-equal c ?b))) :end))
+ 3)))
+
+(ert-deftest test-parsec-eol ()
+ (should
+ (equal
+ (parsec-with-input "\na"
+ (parsec-newline)
+ (parsec-ch ?a))
+ "a"))
+ (should
+ (equal
+ (parsec-with-input "\r\na"
+ (parsec-crlf)
+ (parsec-ch ?a))
+ "a"))
+ (should
+ (equal
+ (parsec-with-input "\r\na"
+ (parsec-eol)
+ (parsec-ch ?a))
+ "a"))
+ (should
+ (equal
+ (parsec-with-input "\na"
+ (parsec-eol)
+ (parsec-ch ?a))
+ "a"))
+ (should
+ (equal
+ (parsec-with-input "\ra"
+ (parsec-eol)
+ (parsech-ch ?a))
+ (test-parsec-error-new "\n" "a"))))
+
+(ert-deftest test-parsec-eof ()
+ (should
+ (equal
+ (parsec-with-input "\r\na"
+ (parsec-eol)
+ (parsec-ch ?a)
+ (parsec-eof))
+ nil)))
+
+(ert-deftest test-parsec-re ()
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-query
+ (parsec-re "\\(a\\)\\(bc\\)")
+ :group 2))
+ "bc")))
+
+(ert-deftest test-parsec-one-of ()
+ (should
+ (equal
+ (parsec-with-input "^]-"
+ (parsec-many-as-string (parsec-one-of ?^ ?\] ?-)))
+ "^]-"))
+ (should
+ (equal
+ (parsec-with-input "^-"
+ (parsec-many-as-string (parsec-one-of ?^ ?-)))
+ "^-")))
+
+(ert-deftest test-parsec-none-of ()
+ (should
+ (equal
+ (parsec-with-input "-[]"
+ (parsec-none-of ?\] ?^)
+ (parsec-one-of ?\[ ?\])
+ (parsec-none-of ?- ?^))
+ "]")))
+
+(ert-deftest test-parsec-str ()
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-str "abc"))
+ "abc"))
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-or (parsec-str "ac")
+ (parsec-ch ?a)))
+ "a")))
+
+(ert-deftest test-parsec-string ()
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-string "abc"))
+ "abc"))
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-or (parsec-string "ac")
+ (parsec-ch ?a)))
+ (test-parsec-error-new "c" "b")))
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-or (parsec-try (parsec-string "ac"))
+ (parsec-ch ?a)))
+ "a")))
+
+(ert-deftest test-parsec-or ()
+ (should
+ (equal
+ (parsec-with-input "1"
+ (parsec-or (parsec-letter)
+ (parsec-digit)))
+ "1"))
+ (should
+ (equal
+ (parsec-with-input "124"
+ (parsec-or (parsec-string "13")
+ (parsec-ch ?1)))
+ (test-parsec-error-new "3" "2")))
+ (should
+ (equal
+ (parsec-with-input "124"
+ (parsec-or (parsec-str "13")
+ (parsec-ch ?1)))
+ "1")))
+
+(ert-deftest test-parsec-collect-optional ()
+ (should
+ (equal
+ (parsec-with-input "abc-def"
+ (parsec-collect-as-string
+ (parsec-and
+ (parsec-ch ?a)
+ (parsec-str "bc"))
+ (parsec-optional (parsec-ch ?-))
+ (parsec-and
+ (parsec-return (parsec-str "de")
+ (parsec-ch ?f)))))
+ "bc-de"))
+ (should
+ (equal
+ (parsec-with-input "abcdef"
+ (parsec-collect-as-string
+ (parsec-and
+ (parsec-ch ?a)
+ (parsec-str "bc"))
+ (parsec-optional (parsec-ch ?-))
+ (parsec-and
+ (parsec-return (parsec-str "de")
+ (parsec-ch ?f)))))
+ "bcde")))
+
+(ert-deftest test-parsec-try ()
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-or (parsec-try (parsec-string "abd"))
+ (parsec-str "abc")))
+ "abc")))
+
+(ert-deftest test-parsec-error-handles ()
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-with-error-message "foo"
+ (parsec-str "abd")))
+ (parsec-error-new "foo")))
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-with-error-message "foo"
+ (parsec-str "abc")))
+ "abc"))
+ (should
+ (equal
+ (condition-case err
+ (parsec-with-input "abc"
+ (parsec-ensure-with-error-message "foo"
+ (parsec-str "abd")))
+ (error (cdr err)))
+ '("foo")))
+ (should
+ (equal
+ (condition-case err
+ (parsec-with-input "abc"
+ (parsec-ensure-with-error-message "foo"
+ (parsec-str "abc")))
+ (error (cdr err)))
+ "abc")))
+
+(ert-deftest test-parsec-many ()
+ (should
+ (equal
+ (parsec-with-input "aaaaab"
+ (parsec-collect-as-string
+ (parsec-many-as-string (parsec-ch ?a))
+ (parsec-many-as-string (parsec-ch ?c))
+ (parsec-many1-as-string (parsec-ch ?b))))
+ "aaaaab"))
+ (should
+ (equal
+ (parsec-with-input "aaaaab"
+ (parsec-collect-as-string
+ (parsec-many-as-string (parsec-ch ?a))
+ (parsec-many-as-string (parsec-ch ?c))
+ (parsec-many1-as-string (parsec-ch ?b))
+ (parsec-many1-as-string (parsec-ch ?c))))
+ (test-parsec-error-new "c" "`EOF'")))
+ (should
+ (equal
+ (parsec-with-input "abababaa"
+ (parsec-many1-as-string (parsec-string "ab")))
+ (test-parsec-error-new "b" "a")))
+ (should
+ (equal
+ (parsec-with-input "abababaa"
+ (parsec-many1-as-string (parsec-try (parsec-string "ab")))
+ (parsec-str "aa"))
+ "aa"))
+ (should
+ (equal
+ (parsec-with-input "abababaa"
+ (parsec-many1-as-string (parsec-str "ab"))
+ (parsec-str "aa"))
+ "aa")))
+
+
+(ert-deftest test-parsec-till ()
+ (should
+ (equal
+ (parsec-with-input "abcd"
+ (parsec-many-till-as-string (parsec-any-ch) (parsec-ch ?d)))
+ "abc"))
+ (should
+ (equal
+ (parsec-with-input "abcd"
+ (parsec-many-till-as-string (parsec-any-ch) (parsec-ch ?d) :both))
+ '("abc" . "d")))
+ (should
+ (equal
+ (parsec-with-input "abcd"
+ (parsec-many-till-as-string (parsec-any-ch) (parsec-ch ?d) :end))
+ "d"))
+ (should
+ (equal
+ (parsec-with-input "abcd"
+ (parsec-with-error-message "eof"
+ (parsec-many-till-as-string (parsec-any-ch) (parsec-ch ?e))))
+ (parsec-error-new "eof")))
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-until-as-string (parsec-ch ?c)))
+ "ab"))
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-until-as-string (parsec-ch ?c) :end))
+ "c"))
+ (should
+ (equal
+ (parsec-with-input "abc"
+ (parsec-query (parsec-until-as-string (parsec-ch ?c)) :beg))
+ 1)))
+
+(ert-deftest test-parsec-not-followed-by ()
+ (should
+ (equal
+ (parsec-with-input "abd"
+ (parsec-collect*
+ (parsec-str "ab")
+ (parsec-not-followed-by (parsec-ch ?c))
+ (parsec-ch ?d)))
+ '("ab" "d"))))
+
+(ert-deftest test-parsec-endby ()
+ (should
+ (equal
+ (parsec-with-input "abc\ndef"
+ (parsec-endby (parsec-many-as-string (parsec-letter))
+ (parsec-eol-or-eof)))
+ '("abc" "def"))))
+
+(ert-deftest test-parsec-sepby ()
+ (should
+ (equal
+ (parsec-with-input "ab,cd,ef"
+ (parsec-sepby (parsec-many-as-string (parsec-re "[^,]"))
+ (parsec-ch ?,)))
+ '("ab" "cd" "ef"))))
+
+(ert-deftest test-parsec-between ()
+ (should
+ (equal
+ (parsec-with-input "{abc}"
+ (parsec-between
+ (parsec-ch ?\{) (parsec-ch ?\})
+ (parsec-or
+ (parsec-str "ac")
+ (parsec-many-as-string (parsec-letter)))))
+ "abc"))
+ (should
+ (equal
+ (parsec-with-input "{abc}"
+ (parsec-between
+ (parsec-ch ?\{) (parsec-ch ?\})
+ (parsec-or
+ (parsec-string "ac")
+ (parsec-many-as-string (parsec-letter)))))
+ (test-parsec-error-new "c" "b"))))
+
+(ert-deftest test-parsec-count ()
+ (should
+ (equal
+ (parsec-with-input "aaaab"
+ (parsec-return (parsec-count-as-string 3 (parsec-ch ?a))
+ (parsec-many1 (parsec-one-of ?a ?b))))
+ "aaa")))
+
+(ert-deftest test-parsec-option ()
+ (should
+ (equal
+ (parsec-with-input "ab"
+ (parsec-option "opt" (parsec-string "ac")))
+ (test-parsec-error-new "c" "b")))
+ (should
+ (equal
+ (parsec-with-input "ab"
+ (parsec-option "opt" (parsec-str "ac")))
+ "opt"))
+ (should
+ (equal
+ (parsec-with-input "ab"
+ (parsec-option "opt" (parsec-string "ab")))
+ "ab")))
+
+(ert-deftest test-parsec-optional ()
+ (should
+ (equal
+ (parsec-with-input "abcdef"
+ (parsec-collect-as-string
+ (parsec-str "abc")
+ (parsec-optional (parsec-ch ?-))
+ (parsec-str "def")))
+ "abcdef"))
+ (should
+ (equal
+ (parsec-with-input "abc-def"
+ (parsec-collect-as-string
+ (parsec-str "abc")
+ (parsec-optional (parsec-ch ?-))
+ (parsec-str "def")))
+ "abc-def"))
+ (should
+ (equal
+ (parsec-with-input "abcdef"
+ (parsec-collect-as-string
+ (parsec-str "abc")
+ (parsec-from-just (parsec-optional-maybe (parsec-ch ?-)))
+ (parsec-str "def")))
+ "abcdef"))
+ (should
+ (equal
+ (parsec-with-input "abc-def"
+ (parsec-collect-as-string
+ (parsec-str "abc")
+ (parsec-from-just (parsec-optional-maybe (parsec-ch ?-)))
+ (parsec-str "def")))
+ "abc-def")))
+
+(provide 'parsec-tests)
+;;; parsec-tests.el ends here
- [elpa] master 0c3408a 01/60: Init commit, (continued)
- [elpa] master 0c3408a 01/60: Init commit, Junpeng Qiu, 2016/10/25
- [elpa] master 1929932 02/60: Split into two files, Junpeng Qiu, 2016/10/25
- [elpa] master fd77961 25/60: Add a few simple API, Junpeng Qiu, 2016/10/25
- [elpa] master 12d2ad6 24/60: Bug fixes, Junpeng Qiu, 2016/10/25
- [elpa] master adf4706 20/60: Add many-till, notFollowedBy and fix others, Junpeng Qiu, 2016/10/25
- [elpa] master 32809ad 30/60: Add parsec-error-new-2, Junpeng Qiu, 2016/10/25
- [elpa] master 3503e4a 13/60: Rename for easier understanding, Junpeng Qiu, 2016/10/25
- [elpa] master 4fb2abe 29/60: Update simple-csv-parser, Junpeng Qiu, 2016/10/25
- [elpa] master fa2e6f1 27/60: Add gitignore, Junpeng Qiu, 2016/10/25
- [elpa] master a06220c 42/60: Update README, Junpeng Qiu, 2016/10/25
- [elpa] master f7e8629 26/60: Add tests,
Junpeng Qiu <=
- [elpa] master bc11325 50/60: Add file examples/.nosearch, Junpeng Qiu, 2016/10/25
- [elpa] master 1da4344 32/60: Update url-str-parser, Junpeng Qiu, 2016/10/25
- [elpa] master 89dd2ac 54/60: Fix some wording in README, Junpeng Qiu, 2016/10/25
- [elpa] master 55515ca 48/60: Add missing dependencies, Junpeng Qiu, 2016/10/25
- [elpa] master 2e9f962 59/60: Add new package parsec to externals-list, Junpeng Qiu, 2016/10/25
- [elpa] master 3827d26 36/60: Add docs for user-interface functions & combinators, Junpeng Qiu, 2016/10/25
- [elpa] master 8c108be 56/60: Add parsec-peek and parsec-peek-p, Junpeng Qiu, 2016/10/25
- [elpa] master 6001a70 12/60: Refine and add more parsec API, Junpeng Qiu, 2016/10/25
- [elpa] master da878fa 18/60: Use parsec-query for other return values, Junpeng Qiu, 2016/10/25
- [elpa] master 054a753 06/60: Rename to parsec, Junpeng Qiu, 2016/10/25