|
2 | 2 | (:require
|
3 | 3 | [cider.nrepl.middleware.inspect :as i]
|
4 | 4 | [cider.nrepl.test-session :as session]
|
| 5 | + [clojure.string :as string] |
5 | 6 | [clojure.test :refer :all]))
|
6 | 7 |
|
7 | 8 | (def nil-result ["(\"nil\" (:newline))"])
|
|
129 | 130 | (is (= (:status response) #{"inspect-set-page-size-error" "done"}))
|
130 | 131 | (is (= (:ex response) "class java.lang.Exception"))
|
131 | 132 | (is (.startsWith (:err response) "java.lang.Exception: page-size exception"))
|
| 133 | + (is (:pp-stacktrace response))))) |
| 134 | + |
| 135 | + (testing "inspect-set-max-atom-length error handling" |
| 136 | + (with-redefs [i/swap-inspector! (fn [& _] (throw (Exception. "max-atom-length exception")))] |
| 137 | + (let [response (session/message {:op "inspect-set-max-atom-length" :max-atom-length 10})] |
| 138 | + (is (= (:status response) #{"inspect-set-max-atom-length-error" "done"})) |
| 139 | + (is (= (:ex response) "class java.lang.Exception")) |
| 140 | + (is (.startsWith (:err response) "java.lang.Exception: max-atom-length exception")) |
| 141 | + (is (:pp-stacktrace response))))) |
| 142 | + |
| 143 | + (testing "inspect-set-max-coll-size error handling" |
| 144 | + (with-redefs [i/swap-inspector! (fn [& _] (throw (Exception. "max-coll-size exception")))] |
| 145 | + (let [response (session/message {:op "inspect-set-max-coll-size" :max-coll-size 10})] |
| 146 | + (is (= (:status response) #{"inspect-set-max-coll-size-error" "done"})) |
| 147 | + (is (= (:ex response) "class java.lang.Exception")) |
| 148 | + (is (.startsWith (:err response) "java.lang.Exception: max-coll-size exception")) |
132 | 149 | (is (:pp-stacktrace response))))))
|
133 | 150 |
|
134 | 151 | (deftest inspect-var-integration-test
|
|
273 | 290 | (is (re-find #"Page size: 5, showing page: 2 of 20"
|
274 | 291 | (extract-text small-page-2))))))
|
275 | 292 |
|
| 293 | +(deftest max-atom-length-integration-test |
| 294 | + ;; Default max length is 150, so 150 - 3 = 147 chars is too long because we |
| 295 | + ;; need to leave room for an opening quote and trailing ellipsis "xxxxxxxx... |
| 296 | + ;; NOTE: We'd prefer to use `orchard.inspect/*max-atom-length*` vs. |
| 297 | + ;; hard-coding `max-len` to 150 in case the default ever changes, but test |
| 298 | + ;; code isn't processed by mranderson and so can't refer to inlined deps |
| 299 | + ;; including orchard. |
| 300 | + ;; See also https://github.com/benedekfazekas/mranderson/issues/5 |
| 301 | + (let [max-len 150 #_(var-get #'orchard.inspect/*max-atom-length*) |
| 302 | + _ (assert (> max-len 3) "inspect/*max-atom-length* is too short for this test.") |
| 303 | + too-long (pr-str [(string/join (repeat (- max-len 3) "x"))]) ;; 147 |
| 304 | + trunc-len (- max-len 4) ;; 146 |
| 305 | + x-pattern #(re-pattern (format "\"\\\\\"%s...\"" (string/join (repeat % "x")))) |
| 306 | + extract-text #(-> % :value first)] |
| 307 | + |
| 308 | + (testing "max atom length can be set for the eval op" |
| 309 | + (let [default-atom-length (session/message {:op "eval" |
| 310 | + :inspect "true" |
| 311 | + :code too-long}) |
| 312 | + short-atom-length (session/message {:op "eval" |
| 313 | + :inspect "true" |
| 314 | + :code too-long |
| 315 | + :max-atom-length 10}) |
| 316 | + unchanged-default-atom-length (session/message {:op "eval" |
| 317 | + :inspect "true" |
| 318 | + :code too-long})] |
| 319 | + (is (re-find (x-pattern trunc-len) |
| 320 | + (extract-text default-atom-length))) |
| 321 | + (is (re-find (x-pattern 6) |
| 322 | + (extract-text short-atom-length))) |
| 323 | + (is (re-find (x-pattern trunc-len) |
| 324 | + (extract-text unchanged-default-atom-length))))) |
| 325 | + |
| 326 | + (testing "max atom length can be changed without re-eval'ing last form" |
| 327 | + (let [default-atom-length (session/message {:op "eval" |
| 328 | + :inspect "true" |
| 329 | + :code too-long}) |
| 330 | + shorten-atom-length (session/message {:op "inspect-set-max-atom-length" |
| 331 | + :max-atom-length 10}) |
| 332 | + longer-atom-length (session/message {:op "inspect-set-max-atom-length" |
| 333 | + :max-atom-length 20}) |
| 334 | + unchanged-default-atom-length (session/message {:op "eval" |
| 335 | + :inspect "true" |
| 336 | + :code too-long})] |
| 337 | + (is (re-find (x-pattern trunc-len) |
| 338 | + (extract-text default-atom-length))) |
| 339 | + (is (re-find (x-pattern 6) |
| 340 | + (extract-text shorten-atom-length))) |
| 341 | + (is (re-find (x-pattern 16) |
| 342 | + (extract-text longer-atom-length))) |
| 343 | + (is (re-find (x-pattern trunc-len) |
| 344 | + (extract-text unchanged-default-atom-length))))))) |
| 345 | + |
| 346 | +(deftest max-coll-size-integration-test |
| 347 | + ;; See NOTE in `max-atom-length-integration-test` about hard-coding |
| 348 | + ;; `size-limit` here. |
| 349 | + (let [size-limit 5 #_(var-get #'orchard.inspect/*max-coll-size*) |
| 350 | + big-size (* 2 size-limit) ;; 10 |
| 351 | + big-coll (format "[(range %d)]" big-size) |
| 352 | + coll-pattern (fn [len & [truncate]] |
| 353 | + (re-pattern (format "\\( %s%s \\)" |
| 354 | + (string/join " " (range len)) |
| 355 | + (if truncate " ..." "")))) |
| 356 | + extract-text #(-> % :value first)] |
| 357 | + |
| 358 | + (testing "max coll size can be set for the eval op" |
| 359 | + (let [default-coll-size (session/message {:op "eval" |
| 360 | + :inspect "true" |
| 361 | + :code big-coll}) |
| 362 | + large-coll-size (session/message {:op "eval" |
| 363 | + :inspect "true" |
| 364 | + :code big-coll |
| 365 | + :max-coll-size big-size}) |
| 366 | + unchanged-default-coll-size (session/message {:op "eval" |
| 367 | + :inspect "true" |
| 368 | + :code big-coll})] |
| 369 | + (is (re-find (coll-pattern size-limit :truncate) ;; #"\( 0 1 2 3 4 ... \)" |
| 370 | + (extract-text default-coll-size))) |
| 371 | + (is (re-find (coll-pattern big-size) ;; #"\( 0 1 2 3 4 5 6 7 8 9 \)" |
| 372 | + (extract-text large-coll-size))) |
| 373 | + (is (re-find (coll-pattern size-limit :truncate) |
| 374 | + (extract-text unchanged-default-coll-size))))) |
| 375 | + |
| 376 | + (testing "max coll size can be changed without re-eval'ing last form" |
| 377 | + (let [default-coll-size (session/message {:op "eval" |
| 378 | + :inspect "true" |
| 379 | + :code big-coll}) |
| 380 | + large-coll-size (session/message {:op "inspect-set-max-coll-size" |
| 381 | + :max-coll-size big-size}) |
| 382 | + smaller-coll-size (session/message {:op "inspect-set-max-coll-size" |
| 383 | + :max-coll-size (dec big-size)}) |
| 384 | + unchanged-default-coll-size (session/message {:op "eval" |
| 385 | + :inspect "true" |
| 386 | + :code big-coll})] |
| 387 | + (is (re-find (coll-pattern size-limit :truncate) |
| 388 | + (extract-text default-coll-size))) |
| 389 | + (is (re-find (coll-pattern big-size) |
| 390 | + (extract-text large-coll-size))) |
| 391 | + (is (re-find (coll-pattern (dec big-size) :truncate) |
| 392 | + (extract-text smaller-coll-size))) |
| 393 | + (is (re-find (coll-pattern size-limit :truncate) |
| 394 | + (extract-text unchanged-default-coll-size))))))) |
| 395 | + |
276 | 396 | (deftest print-length-independence-test
|
277 | 397 | (testing "*print-length* doesn't break rendering of long collections"
|
278 | 398 | (is (re-find #"showing page: \d+ of \d+"
|
|
0 commit comments