Skip to content

Commit

Permalink
Additional tests for regular expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnuessler committed Oct 12, 2012
1 parent e3c1e84 commit eee0fbd
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 19 deletions.
96 changes: 93 additions & 3 deletions jira-markup-mode-test.el
@@ -1,10 +1,33 @@
(require 'ert)
(require 'jira-markup-mode)

(defun with-leading-whitespace (str)
(concat " " str))

(defun should-match-trimmed (regex str)
(should (string-match regex str))
(should (string= (chomp str) (match-string 0 str))))

(defun chomp (str)
"Chomp leading and tailing whitespace from STR."
(while (string-match "^\\s-" str)
(setq str (replace-match "" t t str)))
(while (string-match "\\s-$" str)
(setq str (replace-match "" t t str)))
str)

(ert-deftest chomp-test ()
(let ((expected "foo"))
(should (string= expected (chomp " foo")))
(should (string= expected (chomp " foo")))
(should (string= expected (chomp "foo ")))
(should (string= expected (chomp " foo ")))))

;; regex
(ert-deftest jira-markup-regex-header-1-test ()
(should (string-match-p jira-markup-regex-header-1 "h1. "))
(should (string-match-p jira-markup-regex-header-1 "h1. Header"))
;; (should (string-match-p jira-markup-regex-header-1 " h1. Header "))
(should (string-match-p jira-markup-regex-header-1 " h1. Header "))
(should-not (string-match-p jira-markup-regex-header-1 "a h1. Header"))
(should-not (string-match-p jira-markup-regex-header-1 " a h1. Header")))

Expand All @@ -14,15 +37,82 @@
(should-not (string-match-p jira-markup-regex-header-2 "a h2. Header"))
(should-not (string-match-p jira-markup-regex-header-2 " a h2. Header")))

(ert-deftest jira-markup-regex-header-3-test ()
(should (string-match-p jira-markup-regex-header-3 "h3. "))
(should (string-match-p jira-markup-regex-header-3 "h3. Header"))
(should-not (string-match-p jira-markup-regex-header-3 "a h3. Header"))
(should-not (string-match-p jira-markup-regex-header-3 " a h3. Header")))

(ert-deftest jira-markup-regex-header-4-test ()
(should (string-match-p jira-markup-regex-header-4 "h4. "))
(should (string-match-p jira-markup-regex-header-4 "h4. Header"))
(should-not (string-match-p jira-markup-regex-header-4 "a h4. Header"))
(should-not (string-match-p jira-markup-regex-header-4 " a h4. Header")))

(ert-deftest jira-markup-regex-header-5-test ()
(should (string-match-p jira-markup-regex-header-5 "h5. "))
(should (string-match-p jira-markup-regex-header-5 "h5. Header"))
(should-not (string-match-p jira-markup-regex-header-5 "a h5. Header"))
(should-not (string-match-p jira-markup-regex-header-5 " a h5. Header")))

(ert-deftest jira-markup-regex-header-6-test ()
(should (string-match-p jira-markup-regex-header-6 "h6. "))
(should (string-match-p jira-markup-regex-header-6 "h6. Header"))
(should-not (string-match-p jira-markup-regex-header-6 "a h6. Header"))
(should-not (string-match-p jira-markup-regex-header-6 " a h6. Header")))

(ert-deftest jira-markup-regex-strong-test ()
(should (string-match-p jira-markup-regex-strong " *strong* "))
(should (string-match-p jira-markup-regex-strong " *str*ng* "))
(should-not (string-match-p jira-markup-regex-strong "*strong*")))

(ert-deftest jira-markup-regex-emphasis-test ()
(should (string-match-p jira-markup-regex-emphasis " _emphasis_ "))
(should (string-match-p jira-markup-regex-emphasis " _emph_sis_ "))
(should-not (string-match-p jira-markup-regex-emphasis "_emphasis_")))

(ert-deftest jira-markup-regex-citation-test ()
(should (string-match-p jira-markup-regex-citation " ??citation?? "))
(should (string-match-p jira-markup-regex-citation " ??cit??tion?? "))
(should-not (string-match-p jira-markup-regex-citation "??citation??")))

(ert-deftest jira-markup-regex-deleted-test ()
(should (string-match-p jira-markup-regex-deleted " -deleted- "))
(should (string-match-p jira-markup-regex-deleted " -dele-ted- "))
(should-not (string-match-p jira-markup-regex-deleted "--deleted--"))
(should-not (string-match-p jira-markup-regex-deleted "-deleted-")))

(ert-deftest jira-markup-regex-inserted-text ()
(should (string-match-p jira-markup-regex-inserted " +inserted+ "))
(should (string-match-p jira-markup-regex-inserted " +inser+ed+ "))
(should-not (string-match-p jira-markup-regex-inserted "+inserted+")))

(ert-deftest jira-markup-regex-superscript-text()
(should (string-match-p jira-markup-regex-superscript " ^superscript^ "))
(should (string-match-p jira-markup-regex-superscript " ^super^script^ "))
(should-not (string-match-p jira-markup-regex-superscript "^superscript^")))

(ert-deftest jira-markup-regex-subscript-text ()
(should (string-match-p jira-markup-regex-subscript " ~subscript~ "))
(should (string-match-p jira-markup-regex-subscript " ~subs~cript~ "))
(should-not (string-match-p jira-markup-regex-subscript "~subscript~")))

(ert-deftest jira-markup-regex-monospaced-text ()
(should (string-match jira-markup-regex-monospaced " {{monospaced}} "))
(should (string= " {{monospaced}} " (match-string 0 " {{monospaced}} ")))
(should (string-match jira-markup-regex-monospaced " {{mono{{}}spaced}} "))
(should (string= " {{mono{{}}spaced}} " (match-string 0 " {{mono{{}}spaced}} ")))
(should-not (string-match-p jira-markup-regex-monospaced "{{monospaced}}")))

(ert-deftest jira-markup-regex-blockquote-test ()
(should (string-match-p jira-markup-regex-blockquote "bq. "))
(should (string-match-p jira-markup-regex-blockquote " bq. "))
(should (string-match-p jira-markup-regex-blockquote "bq. the-quote"))
;; (should (string-match-p jira-markup-regex-blockquote " bq. the-quote "))
(should (string-match-p jira-markup-regex-blockquote " bq. the-quote "))
(should-not (string-match-p jira-markup-regex-blockquote "a bq. the-quote")))

(ert-deftest jira-markup-regex-hr-test ()
(should (string-match-p jira-markup-regex-hr "----"))
(should (string-match-p jira-markup-regex-hr " ----"))
(should (string-match-p jira-markup-regex-hr (with-leading-whitespace "----")))
(should-not (string-match-p jira-markup-regex-hr "---"))
(should-not (string-match-p jira-markup-regex-hr "-----")))
73 changes: 57 additions & 16 deletions jira-markup-mode.el
Expand Up @@ -282,78 +282,119 @@ Otherwise, they will be treated as [[PageName|alias text]]."
:group 'jira-markup-faces)

;; regular expressions
;; TODO
(defconst jira-markup-regex-link-inline
"\\(!?\\[[^]]*?\\]\\)\\(([^\\)]*)\\)"
"Regular expression for a [text](file) or an image link ![text](file).")

;; TODO
(defconst jira-markup-regex-link-reference
"\\(!?\\[[^]]+?\\]\\)[ ]?\\(\\[[^]]*?\\]\\)"
"Regular expression for a reference link [text][id].")

;; TODO
(defconst jira-markup-regex-reference-definition
"^ \\{0,3\\}\\(\\[.*\\]\\):\\s *\\(.*?\\)\\s *\\( \"[^\"]*\"$\\|$\\)"
"Regular expression for a link id [definition]: ...")

;; TODO
(defconst jira-markup-regex-header
"#+\\|\\S-.*\n\\(?:\\(===+\\)\\|\\(---+\\)\\)$"
"Regexp identifying Jira-Markup headers.")

; headings
(defconst jira-markup-regex-header-1
"^\\(h1\\. \\)\\(.*?\\)$"
"^\\s-*h1\\.\\s-+.*$"
"Regular expression for level 1 headers.")

(defconst jira-markup-regex-header-2
"^\\(h2\\. \\)\\(.*?\\)$"
"^\\s-*h2\\.\\s-+.*$"
"Regular expression for level 2 headers.")

(defconst jira-markup-regex-header-3
"^\\(h3\\. \\)\\(.*?\\)$"
"^\\s-*h3\\.\\s-+.*$"
"Regular expression for level 3 headers.")

(defconst jira-markup-regex-header-4
"^\\(h4\\. \\)\\(.*?\\)$"
"^\\s-*h4\\.\\s-+.*$"
"Regular expression for level 4 headers.")

(defconst jira-markup-regex-header-5
"^\\(h5\\. \\)\\(.*?\\)$"
"^\\s-*h5\\.\\s-+.*$"
"Regular expression for level 5 headers.")

(defconst jira-markup-regex-header-6
"^\\(h6\\. \\)\\(.*?\\)$"
"^\\s-*h6\\.\\s-+.*$"
"Regular expression for level 6 headers.")

(defconst jira-markup-regex-hr
"^[ \t]*----$"
"^\\s-*----$"
"Regular expression for matching Jira-Markup horizontal rules.")

;; TODO
(defconst jira-markup-regex-code
"\\(^\\|[^\\]\\)\\(\\(`\\{1,2\\}\\)\\([^ \\]\\|[^ ]\\(.\\|\n[^\n]\\)*?[^ \\]\\)\\3\\)"
"Regular expression for matching inline code fragments.")

;; TODO
(defconst jira-markup-regex-pre
"^\\( \\|\t\\).*$"
"Regular expression for matching preformatted text sections.")

;; TODO
(defconst jira-markup-regex-list
"^[ \t]*\\([0-9]+\\.\\|[\\*\\+-]\\) "
"Regular expression for matching list markers.")

(defconst jira-markup-regex-bold
"\\(^\\|[^\\]\\)\\(\\(*\\)\\(.\\|\n[^\n]\\)*?[^\\ ]\\3\\)"
"Regular expression for matching bold text.")

(defconst jira-markup-regex-italic
"\\(^\\|[^\\]\\)\\(\\(_\\)\\([^ \\]\\3\\|[^ ]\\(.\\|\n[^\n]\\)*?[^\\ ]\\3\\)\\)"
"Regular expression for matching italic text.")
; text effects
(defconst jira-markup-regex-strong
"\\s-+\\*.*\\*\\s-+"
"Regular expression for matching *strong* text.")

(defconst jira-markup-regex-emphasis
"\\s-+_.*_\\s-+"
"Regular expression for matching _emphasized_ text.")

; new
(defconst jira-markup-regex-citation
"\\s-+\\?\\?.*\\s-+"
"Regular expression for matching ??cited?? text.")

; new
(defconst jira-markup-regex-deleted
"\\s-+-.*-\\s-"
"Regular expression for matching -deleted- text.")

; new
(defconst jira-markup-regex-inserted
"\\s-+\\+.*\\+\\s-+"
"Regular expression for matching +inserted+ text.")

; new
(defconst jira-markup-regex-superscript
"\\s-+^.*^\\s-+"
"Regular expression for matching ^superscript^ text.")

; new
(defconst jira-markup-regex-subscript
"\\s-+~.*~\\s-+"
"Regular expression for matching ~subscript~ text.~")

; new
(defconst jira-markup-regex-monospaced
"\\s-+{{.*}}\\s-+"
"Regular expression for matching {{monospaced}} text.")

(defconst jira-markup-regex-blockquote
"^[ \t]*bq\\."
"Regular expression for matching blockquote lines.")

;; TODO
(defconst jira-markup-regex-line-break
" $"
"Regular expression for matching line breaks.")

;; TODO
(defconst jira-markup-regex-wiki-link
"\\[\\[\\([^]|]+\\)\\(|\\([^]]+\\)\\)?\\]\\]"
"Regular expression for matching wiki links.
Expand Down Expand Up @@ -410,8 +451,8 @@ text.")
'((1 jira-markup-reference-face t)
(2 jira-markup-url-face t)
(3 jira-markup-link-title-face t)))
(cons jira-markup-regex-bold '(2 jira-markup-bold-face))
(cons jira-markup-regex-italic '(2 jira-markup-italic-face))
(cons jira-markup-regex-strong '(2 jira-markup-bold-face))
(cons jira-markup-regex-emphasis '(2 jira-markup-italic-face))
)
"Syntax highlighting for Jira-Markup files.")

Expand Down

0 comments on commit eee0fbd

Please sign in to comment.