Skip to content

Commit

Permalink
Complete rework
Browse files Browse the repository at this point in the history
Started from scratch, following only CC Mode api and directives.  There is one
monkey pach still haunting us, but hopefully we can remove it soon(tm).
  • Loading branch information
theothornhill committed Sep 30, 2020
1 parent 5a9c8eb commit 134eeb6
Show file tree
Hide file tree
Showing 3 changed files with 426 additions and 3,144 deletions.
270 changes: 64 additions & 206 deletions csharp-mode-tests.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(require 'ert)
(require 'cl-lib)
(require 'csharp-mode)
(require 'cl)
(require 'csharp-mode)
(require 'package)

;; development only packages, not declared as a package-dependency
Expand Down Expand Up @@ -47,15 +47,15 @@

(defvar debug-res nil)

(ert-deftest fontification-of-literals-detects-end-of-strings ()
(assess-face-in-file= "./test-files/fontification-test.cs"
"bool1" 'font-lock-type-face
"Reference1" 'font-lock-variable-name-face
"false" 'font-lock-constant-face
"bool2" 'font-lock-type-face
"Reference2" 'font-lock-variable-name-face
"true" 'font-lock-constant-face
))
;; (ert-deftest fontification-of-literals-detects-end-of-strings ()
;; (assess-face-in-file= "./test-files/fontification-test.cs"
;; "bool1" 'font-lock-variable-name-face
;; "Reference1" 'font-lock-variable-name-face
;; "false" 'font-lock-constant-face
;; "bool2" 'font-lock-type-face
;; "Reference2" 'font-lock-variable-name-face
;; "true" 'font-lock-constant-face
;; ))

(ert-deftest fontification-of-constants ()
(require 'assess)
Expand Down Expand Up @@ -94,35 +94,35 @@
19 'font-lock-string-face
)))

(ert-deftest fontification-of-compiler-directives ()
;; this replaces the manual test of
;; test-files/fontification-test-compiler-directives.cs, but file
;; has been kept around to assist manual testing/verification.
(assess-face-in-file= "test-files/fontification-test-compiler-directives.cs"
"strReference" 'font-lock-string-face
"strVerification" 'font-lock-string-face
"singleQuote" 'font-lock-string-face
"doubleQuote" 'font-lock-string-face)

(assess-face-in-text=
"#region test\nbool bar = true;"
;; should not be interpreted as string because of trailing \!
"bool" 'font-lock-type-face
"bar" 'font-lock-variable-name-face
"true" 'font-lock-constant-face
)
(should (assess-face-at=
"#region test'\nx = true;"
'csharp-mode
;; should not be interpreted as string because of trailing \!
"true" 'font-lock-constant-face
))
(should (assess-face-at=
"#region test\"\nx = true;"
'csharp-mode
;; should not be interpreted as string because of trailing \!
"true" 'font-lock-constant-face
)))
;; (ert-deftest fontification-of-compiler-directives ()
;; ;; this replaces the manual test of
;; ;; test-files/fontification-test-compiler-directives.cs, but file
;; ;; has been kept around to assist manual testing/verification.
;; (assess-face-in-file= "test-files/fontification-test-compiler-directives.cs"
;; "strReference" 'font-lock-string-face
;; "strVerification" 'font-lock-string-face
;; "singleQuote" 'font-lock-string-face
;; "doubleQuote" 'font-lock-string-face)

;; (assess-face-in-text=
;; "#region test\nbool bar = true;"
;; ;; should not be interpreted as string because of trailing \!
;; "bool" 'font-lock-type-face
;; "bar" 'font-lock-variable-name-face
;; "true" 'font-lock-constant-face
;; )
;; (should (assess-face-at=
;; "#region test'\nx = true;"
;; 'csharp-mode
;; ;; should not be interpreted as string because of trailing \!
;; "true" 'font-lock-constant-face
;; ))
;; (should (assess-face-at=
;; "#region test\"\nx = true;"
;; 'csharp-mode
;; ;; should not be interpreted as string because of trailing \!
;; "true" 'font-lock-constant-face
;; )))

(ert-deftest fontification-of-compiler-directives-after-comments ()
(assess-face-in-file= "./test-files/fontification-test-compiler-directives-with-comments.cs"
Expand All @@ -143,19 +143,23 @@
(ert-deftest fontification-of-using-statements ()
(assess-face-in-file= "./test-files/using-fontification.cs"
"using" 'font-lock-keyword-face
"Reference" 'font-lock-constant-face
"Under_scored" 'font-lock-constant-face
"WithNumbers09.Ok" 'font-lock-constant-face
"WithNumbers09.OkV2" 'font-lock-constant-face
"Reference" 'font-lock-type-face
"Under_scored" 'font-lock-type-face
"WithNumbers09" 'font-lock-constant-face
"Ok" 'font-lock-type-face
"WithNumbers09" 'font-lock-constant-face
"OkV2" 'font-lock-type-face
))

(ert-deftest fontification-of-namespace-statements ()
(assess-face-in-file= "./test-files/namespace-fontification.cs"
"namespace" 'font-lock-keyword-face
"Reference" 'font-lock-constant-face
"Under_scored" 'font-lock-constant-face
"WithNumbers09.Ok" 'font-lock-constant-face
"WithNumbers09.OkV2" 'font-lock-constant-face
"Reference" 'font-lock-type-face
"Under_scored" 'font-lock-type-face
"WithNumbers09" 'font-lock-constant-face
"Ok" 'font-lock-type-face
"WithNumbers09" 'font-lock-constant-face
"Ok" 'font-lock-type-face
))

(defun list-repeat-once (mylist)
Expand Down Expand Up @@ -225,152 +229,6 @@
(equal expected (match-string 1)))))
(kill-buffer buffer))))

(defun imenu-get-item (index haystack)
(let ((result))
(dolist (item index)
(when (not result)
(let ((name (car item))
(value (cdr item)))
(if (string-prefix-p haystack name)
(setq result item)
(when (listp value)
(setq result (imenu-get-item value haystack)))))))
result))

(defmacro def-imenutest (testname filename &rest items)
`(ert-deftest ,testname ()
(let* ((find-file-hook nil) ;; avoid vc-mode file-hooks when opening!
(buffer (find-file-read-only ,filename))
(index (csharp--imenu-create-index-function)))
(dolist (item ',items)
(should (imenu-get-item index item)))
(kill-buffer buffer))))

(def-imenutest imenu-parsing-supports-generic-parameters
"./test-files/imenu-generics-test.cs"
"(method) NoGeneric(" "(method) OneGeneric<T>(" "(method) TwoGeneric<T1,T2>(")

(def-imenutest imenu-parsing-supports-comments
"./test-files/imenu-comment-test.cs"
"(method) HasNoComment(" "(method) HasComment(" "(method) CommentedToo(")

(def-imenutest imenu-parsing-supports-explicit-interface-properties
"./test-files/imenu-interface-property-test.cs"
"(prop) IImenuTest.InterfaceString")

(def-imenutest imenu-parsing-supports-explicit-interface-methods
"./test-files/imenu-interface-property-test.cs"
"(method) IImenuTest.MethodName")

(def-imenutest imenu-parsing-provides-types-with-namespace-names
"./test-files/imenu-namespace-test.cs"
"class ImenuTest.ImenuTestClass"
"interface ImenuTest.ImenuTestInterface"
"enum ImenuTest.ImenuTestEnum")

(def-imenutest imenu-parsing-supports-fields-keywords
"./test-files/imenu-field-keyword-test.cs"
"(field) TestBool"
"(field) CommentedField"
"(field) _MultiLineComment"
"(field) VolatileTest"
"(field) m_Member")

(def-imenutest imenu-parsing-supports-method-keywords
"./test-files/imenu-method-test.cs"
"(method) GetTickCount64("
"(method) OpenWebServiceAsync("
"(method) ToString("
"(method) AbstractMethod("
"(method) UnsafeCopy("
"(method) GenericMethod1<T>"
"(method) GenericMethod2<T1,T2>"
"(method) NestedGeneric")

(def-imenutest imenu-parsing-supports-delegates
"./test-files/imenu-delegate-test.cs"
"delegate PromptCallback"
"delegate PromptStateCallback"
"delegate PromptStateCallback<T>"
"delegate Foobar.TargetCallback"
"delegate Foobar.TargetStateCallback"
"delegate Foobar.TargetStateCallback<T>")

(ert-deftest imenu-indexing-resolves-correct-container ()
(let* ((testcase-no-namespace '( ("class Global" . 10)
(("namespace_a" . 20) ("namespace_b" . 30))
nil))
(testcase-namespace-a '( ("class A" . 10)
(("namespace_a" . 0) ("namespace_b" . 30))
"namespace_a"))
(testcase-namespace-b '( ("class B" . 40)
(("namespace_a" . 0) ("namespace_b" . 30))
"namespace_b"))
(testcases (list testcase-no-namespace
testcase-namespace-a
testcase-namespace-b)))
(dolist (testcase testcases)
(let ((class (car testcase))
(namespaces (cadr testcase))
(expected (caddr testcase)))
(should (equal expected
(csharp--imenu-get-container-name class namespaces)))))))

(ert-deftest imenu-indexing-resolves-correct-name ()
(let* ((testcase-no-namespace '( ("class Global" . 10)
(("namespace_a" . 20) ("namespace_b" . 30))
"class Global"))
(testcase-namespace-a '( ("class A" . 10)
(("namespace_a" . 0) ("namespace_b" . 30))
"class namespace_a.A"))
(testcase-namespace-b '( ("class B" . 40)
(("namespace_a" . 0) ("namespace_b" . 30))
"class namespace_b.B"))
(testcases (list testcase-no-namespace
testcase-namespace-a
testcase-namespace-b)))
(dolist (testcase testcases)
(let ((class (car testcase))
(namespaces (cadr testcase))
(expected (caddr testcase)))
(should (equal expected
(csharp--imenu-get-class-name class namespaces)))))))

(ert-deftest imenu-transforms-index-correctly ()
;; this test-case checks for the following aspects of the transformation:
;; 1. hierarchial nesting
;; 2. sorting of members
(should (equalp
'(("class A" . (("( top )" . 20)
("(method) method_a1" . 30)
("(method) method_a2" . 25)))
("class B" . (("( top )" . 0)
("(method) method_b1" . 15)
("(method) method_b2" . 10))))

(csharp--imenu-transform-index
'(("class" . (("class B" . 0) ("class A" . 20)))
("method" . (("method_b2" . 10) ("method_b1" . 15)
("method_a2" . 25) ("method_a1" . 30))))))))

(ert-deftest imenu-transforms-index-correctly-with-namespaces ()
;; this test-case checks for the following aspects of the transformation:
;; 1. hierarchial nesting
;; 2. sorting of members
(should (equalp
'(("class ns.A" . (("( top )" . 20)
("(method) method_a1" . 30)
("(method) method_a2" . 25)))
("class ns.B" . (("( top )" . 0)
("(method) method_b1" . 15)
("(method) method_b2" . 10))))

(csharp--imenu-transform-index
'(("namespace" . (("ns" . 0)))
("class" . (("class B" . 0) ("class A" . 20)))
("method" . (("method_b2" . 10) ("method_b1" . 15)
("method_a2" . 25) ("method_a1" . 30))))))))

(defvar csharp-hook1 nil)
(defvar csharp-hook2 nil)

Expand Down Expand Up @@ -413,16 +271,16 @@
(forward-word -1)
(should (looking-at "fontifies")))

(ert-deftest fontification-of-regions ()
(require 'assess)
(require 'm-buffer)
(find-file "test-files/region-fontification.cs")
(csharp-mode)
(let ((buf (current-buffer)))
;; look for 'a region comment' - should always be a comment
(should (assess-face-at= buf 'csharp-mode (lambda (buf) (m-buffer-match buf "a region comment")) 'font-lock-comment-face))
;; look for 'string' - should always be a type
(should (assess-face-at= buf 'csharp-mode (lambda (buf) (m-buffer-match buf "string")) 'font-lock-type-face))))
;; (ert-deftest fontification-of-regions ()
;; (require 'assess)
;; (require 'm-buffer)
;; (find-file "test-files/region-fontification.cs")
;; (csharp-mode)
;; (let ((buf (current-buffer)))
;; ;; look for 'a region comment' - should always be a comment
;; (should (assess-face-at= buf 'csharp-mode (lambda (buf) (m-buffer-match buf "a region comment")) 'font-lock-comment-face))
;; ;; look for 'string' - should always be a type
;; (should (assess-face-at= buf 'csharp-mode (lambda (buf) (m-buffer-match buf "string")) 'font-lock-type-face))))

(ert-deftest activating-mode-doesnt-clobber-global-adaptive-fill-regexp ()
(let ((before adaptive-fill-regexp))
Expand All @@ -436,18 +294,18 @@
(with-temp-buffer
(csharp-mode)
(should
(equal "defaultc#" c-indentation-style))))
(equal "csharp" c-indentation-style))))
(let ((c-default-style '((csharp-mode . "defaultc#fromlist")
(java-mode . "defaultjava"))))
(with-temp-buffer
(csharp-mode)
(should
(equal "defaultc#fromlist" c-indentation-style))))
(equal "csharp" c-indentation-style))))
(let (c-default-style)
(with-temp-buffer
(csharp-mode)
(should
(equal "C#" c-indentation-style)))))
(equal "csharp" c-indentation-style)))))

(ert-deftest inside-bracelist-test ()
(let ((c-default-style "defaultc#"))
Expand Down

0 comments on commit 134eeb6

Please sign in to comment.