test-ein-utils.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. (eval-when-compile (require 'cl))
  2. (require 'ert)
  3. (require 'ein) ; for `ein:version'
  4. (require 'ein-utils)
  5. (ert-deftest ein-url-simple ()
  6. (should (equal (ein:url 8888) "http://127.0.0.1:8888"))
  7. (should (equal (ein:url "http://localhost") "http://localhost")))
  8. (ert-deftest ein-url-slashes ()
  9. (loop for a in '("a" "a/" "/a")
  10. do (loop for b in '("b" "/b")
  11. do (should (equal (ein:url 8888 a b)
  12. "http://127.0.0.1:8888/a/b")))
  13. do (should (equal (ein:url 8888 a "b/")
  14. "http://127.0.0.1:8888/a/b/"))))
  15. (ert-deftest ein-trim-simple ()
  16. (should (equal (ein:trim "a") "a"))
  17. (should (equal (ein:trim " a ") "a"))
  18. (should (equal (ein:trim "\na\n") "a")))
  19. (ert-deftest ein-trim-middle-spaces ()
  20. (should (equal (ein:trim "a b") "a b"))
  21. (should (equal (ein:trim " a b ") "a b"))
  22. (should (equal (ein:trim "\na b\n") "a b")))
  23. (ert-deftest ein-trim-left-simple ()
  24. (should (equal (ein:trim-left "a") "a"))
  25. (should (equal (ein:trim-left " a ") "a "))
  26. (should (equal (ein:trim-left "\na\n") "a\n")))
  27. (ert-deftest ein-trim-right-simple ()
  28. (should (equal (ein:trim-right "a") "a"))
  29. (should (equal (ein:trim-right " a ") " a"))
  30. (should (equal (ein:trim-right "\na\n") "\na")))
  31. (ert-deftest ein:trim-indent-empty ()
  32. (should (equal (ein:trim-indent "") "")))
  33. (ert-deftest ein:trim-indent-one-line ()
  34. (should (equal (ein:trim-indent "one line") "one line")))
  35. (ert-deftest ein:trim-indent-one-newline ()
  36. (should (equal (ein:trim-indent "one line\n") "one line\n")))
  37. (ert-deftest ein:trim-indent-multi-lines-no-trim ()
  38. (let ((original "\
  39. def func():
  40. pass
  41. ")
  42. (trimmed "\
  43. def func():
  44. pass
  45. "))
  46. (should (equal (ein:trim-indent original) trimmed))))
  47. (ert-deftest ein:trim-indent-multi-lines-one-trim ()
  48. (let ((original "\
  49. def func():
  50. pass
  51. ")
  52. (trimmed "\
  53. def func():
  54. pass
  55. "))
  56. (should (equal (ein:trim-indent original) trimmed))))
  57. (ert-deftest ein:trim-indent-multi-lines-with-empty-lines ()
  58. (let ((original "\
  59. def func():
  60. pass
  61. ")
  62. (trimmed "\
  63. def func():
  64. pass
  65. "))
  66. (should (equal (ein:trim-indent original) trimmed))))
  67. ;;; Text manipulation on buffer
  68. (ert-deftest ein:find-leftmot-column-simple-cases ()
  69. (loop for (indent text) in
  70. '(;; No indent
  71. (0 "\
  72. def f():
  73. pass")
  74. ;; Indented python code
  75. (4 "\
  76. def f():
  77. pass")
  78. ;; Deeper indent can come first
  79. (4 "\
  80. # indent = 8
  81. # indent 4")
  82. ;; With empty lines
  83. (4 "\
  84. # indent = 8
  85. # indent 4
  86. ")
  87. )
  88. do (with-temp-buffer
  89. (insert text)
  90. (should (= (ein:find-leftmot-column (point-min) (point-max))
  91. indent)))))
  92. ;;; Misc
  93. (ert-deftest ein:list-insert-after ()
  94. (should (equal (ein:list-insert-after '(a) 'a 'X) '(a X)))
  95. (should (equal (ein:list-insert-after '(a b c) 'a 'X) '(a X b c)))
  96. (should (equal (ein:list-insert-after '(a b c) 'b 'X) '(a b X c)))
  97. (should (equal (ein:list-insert-after '(a b c) 'c 'X) '(a b c X)))
  98. (should-error (ein:list-insert-after '(a b c) 'd 'X)))
  99. (ert-deftest ein:list-insert-before ()
  100. (should (equal (ein:list-insert-before '(a) 'a 'X) '(X a)))
  101. (should (equal (ein:list-insert-before '(a b c) 'a 'X) '(X a b c)))
  102. (should (equal (ein:list-insert-before '(a b c) 'b 'X) '(a X b c)))
  103. (should (equal (ein:list-insert-before '(a b c) 'c 'X) '(a b X c)))
  104. (should-error (ein:list-insert-before '(a b c) 'd 'X)))
  105. (ert-deftest ein:list-move-left ()
  106. (should (equal (ein:list-move-left '(a) 'a) '(a)))
  107. (should (equal (ein:list-move-left '(a b) 'a) '(b a)))
  108. (should (equal (ein:list-move-left '(a b) 'b) '(b a)))
  109. (should (equal (ein:list-move-left '(a b c d) 'a) '(b c d a)))
  110. (should (equal (ein:list-move-left '(a b c d) 'b) '(b a c d)))
  111. (should (equal (ein:list-move-left '(a b c d) 'c) '(a c b d)))
  112. (should (equal (ein:list-move-left '(a b c d) 'd) '(a b d c)))
  113. (should-error (ein:list-move-left '(a b c d) 'X)))
  114. (ert-deftest ein:list-move-right ()
  115. (should (equal (ein:list-move-right '(a) 'a) '(a)))
  116. (should (equal (ein:list-move-right '(a b) 'a) '(b a)))
  117. (should (equal (ein:list-move-right '(a b) 'b) '(b a)))
  118. (should (equal (ein:list-move-right '(a b c d) 'a) '(b a c d)))
  119. (should (equal (ein:list-move-right '(a b c d) 'b) '(a c b d)))
  120. (should (equal (ein:list-move-right '(a b c d) 'c) '(a b d c)))
  121. (should (equal (ein:list-move-right '(a b c d) 'd) '(d a b c)))
  122. (should-error (ein:list-move-right '(a b c d) 'X)))
  123. (defun ein:testing-choose-setting-should-equal
  124. (setting value desired &optional single-p)
  125. (let ((setting setting))
  126. (should (equal (ein:choose-setting 'setting value single-p) desired))))
  127. (ert-deftest ein:choose-setting-single-string ()
  128. (let ((test 'ein:testing-choose-setting-should-equal))
  129. (funcall test "a" nil "a")
  130. (funcall test "a" 'whatever "a")))
  131. (ert-deftest ein:choose-setting-single-int ()
  132. (let ((test #'ein:testing-choose-setting-should-equal))
  133. (funcall test 1 nil 1 #'integerp)
  134. (funcall test 1 'whatever 1 #'integerp)))
  135. (ert-deftest ein:choose-setting-alist ()
  136. (let* ((test (lambda (&rest args)
  137. (apply #'ein:testing-choose-setting-should-equal
  138. '(("a" . 1) ("b" . 2) ("c" . 3))
  139. args))))
  140. (funcall test "a" 1)
  141. (funcall test "b" 2)))
  142. (ert-deftest ein:choose-setting-func ()
  143. (let* ((test (lambda (&rest args)
  144. (apply #'ein:testing-choose-setting-should-equal
  145. (lambda (x) 1)
  146. args))))
  147. (funcall test nil 1)
  148. (funcall test 'whatever 1)))