util_test.cljs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (ns frontend.util-test
  2. (:require [cljs.test :refer [deftest is testing]]
  3. [frontend.util :as util]
  4. [frontend.config :as config]
  5. [frontend.modules.shortcut.data-helper :as shortcut-data-helper]))
  6. (deftest test-find-first
  7. (testing "find-first"
  8. (is (= 1 (util/find-first identity [1])))))
  9. (deftest test-delete-emoji-current-pos
  10. (testing "safe current position from end for emoji"
  11. (is (= 3 (util/safe-dec-current-pos-from-end "abc😀d" 5)))
  12. (is (= 3 (util/safe-dec-current-pos-from-end "abc😀" 5)))
  13. (is (= 0 (util/safe-dec-current-pos-from-end "😀" 2)))
  14. (is (= 0 (util/safe-dec-current-pos-from-end "a" 1)))
  15. (is (= 4 (util/safe-dec-current-pos-from-end "abcde" 5)))
  16. (is (= 1 (util/safe-dec-current-pos-from-end "中文" 2)))
  17. (is (= 0 (util/safe-dec-current-pos-from-end "中" 1)))
  18. (is (= 0 (util/safe-dec-current-pos-from-end "a" 1))))
  19. (testing "safe current position from start for emoji"
  20. (is (= 5 (util/safe-inc-current-pos-from-start "abc😀d" 3)))
  21. (is (= 2 (util/safe-inc-current-pos-from-start "abcde" 1)))
  22. (is (= 1 (util/safe-inc-current-pos-from-start "中文" 0)))
  23. (is (= 2 (util/safe-inc-current-pos-from-start "😀" 0)))
  24. (is (= 1 (util/safe-inc-current-pos-from-start "中" 0)))
  25. (is (= 1 (util/safe-inc-current-pos-from-start "a" 0)))))
  26. (deftest test-get-line-pos
  27. (testing "get-line-pos"
  28. (is (= 3 (util/get-line-pos "abcde" 3)))
  29. (is (= 4 (util/get-line-pos "abcd\ne" 4)))
  30. (is (= 0 (util/get-line-pos "abcd\ne" 5)))
  31. (is (= 4 (util/get-line-pos "abc😀d" 5)))
  32. (is (= 1 (util/get-line-pos "abc\nde" 5)))
  33. (is (= 1 (util/get-line-pos "abc\n😀d" 6)))
  34. (is (= 2 (util/get-line-pos "ab\nc😀d" 6)))
  35. (is (= 1 (util/get-line-pos "abc\nde\nf" 5)))
  36. (is (= 1 (util/get-line-pos "abc\n😀d\ne" 6)))
  37. (is (= 2 (util/get-line-pos "ab\nc😀d\ne" 6)))))
  38. (deftest test-get-text-range
  39. (testing "get-text-range"
  40. (is (= "" (util/get-text-range "abcdefg" 0 true)))
  41. (is (= "" (util/get-text-range "abcdefg" 0 false)))
  42. (is (= "abcdefg" (util/get-text-range "abcdefg" 10 true)))
  43. (is (= "abcdefg" (util/get-text-range "abcdefg" 10 false)))
  44. (is (= "abc" (util/get-text-range "abcdefg" 3 true)))
  45. (is (= "abc" (util/get-text-range "abcdefg" 3 false)))
  46. (is (= "abc" (util/get-text-range "abcdefg\nhijklmn" 3 true)))
  47. (is (= "abcdefg\nhij" (util/get-text-range "abcdefg\nhijklmn" 3 false)))
  48. (is (= "abcdefg\nhijklmn" (util/get-text-range "abcdefg\nhijklmn" 10 false)))
  49. (is (= "abcdefg\nhijklmn\nopq" (util/get-text-range "abcdefg\nhijklmn\nopqrst" 3 false)))
  50. (is (= "a😀b" (util/get-text-range "a😀bcdefg" 3 true)))
  51. (is (= "a😀b" (util/get-text-range "a😀bcdefg" 3 false)))
  52. (is (= "a😀b" (util/get-text-range "a😀bcdefg\nhijklmn" 3 true)))
  53. (is (= "a😀bcdefg\nhij" (util/get-text-range "a😀bcdefg\nhijklmn" 3 false)))
  54. (is (= "a😀bcdefg\nh😀i" (util/get-text-range "a😀bcdefg\nh😀ijklmn" 3 false)))))
  55. (deftest test-memoize-last
  56. (testing "memoize-last add test"
  57. (let [actual-ops (atom 0)
  58. m+ (util/memoize-last (fn [x1 x2]
  59. (swap! actual-ops inc) ;; side effect for counting
  60. (+ x1 x2)))]
  61. (is (= (m+ 1 1) 2))
  62. (is (= @actual-ops 1))
  63. (is (= (m+ 1 1) 2))
  64. (is (= (m+ 1 1) 2))
  65. (is (= @actual-ops 1))
  66. (is (= (m+ 1 2) 3))
  67. (is (= @actual-ops 2))
  68. (is (= (m+ 2 3) 5))
  69. (is (= @actual-ops 3))
  70. (is (= (m+ 3 5) 8))
  71. (is (= @actual-ops 4))
  72. (is (= (m+ 3 5) 8))
  73. (is (= @actual-ops 4)))))
  74. (deftest test-media-format-from-input
  75. (testing "predicate file type from ext (html5 supported)"
  76. (is (= (config/ext-of-audio? "file.mp3") true))
  77. (is (= (config/ext-of-audio? "fIle.mP3") true))
  78. (is (= (config/ext-of-audio? "https://x.com/file.mp3") true))
  79. (is (= (config/ext-of-audio? "file.wma") false))
  80. (is (= (config/ext-of-audio? "file.wma" false) true))
  81. (is (= (config/ext-of-video? "file.mp4") true))
  82. (is (= (config/ext-of-video? "file.mp3") false))
  83. (is (= (config/ext-of-image? "file.svg") true))
  84. (is (= (config/ext-of-image? "a.file.png") true))
  85. (is (= (config/ext-of-image? "file.tiff") false))))