|
| 1 | +; Copyright (c) Rich Hickey. All rights reserved. |
| 2 | +; The use and distribution terms for this software are covered by the |
| 3 | +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
| 4 | +; which can be found in the file epl-v10.html at the root of this distribution. |
| 5 | +; By using this software in any fashion, you are agreeing to be bound by |
| 6 | +; the terms of this license. |
| 7 | +; You must not remove this notice, or any other, from this software. |
| 8 | + |
| 9 | +(ns cljs.printing-test |
| 10 | + (:refer-clojure :exclude [iter]) |
| 11 | + (:require [cljs.test :refer-macros [deftest testing is]] |
| 12 | + [clojure.string :as s] |
| 13 | + [clojure.set :as set])) |
| 14 | + |
| 15 | +(deftest test-print-knobs |
| 16 | + (testing "Testing printing knobs" |
| 17 | + (is (= (binding [*print-length* 0] (str [1 2 3 4 5 6 7 8 9 0])) |
| 18 | + "[...]")) |
| 19 | + (is (= (binding [*print-length* 1] (str [1 2 3 4 5 6 7 8 9 0])) |
| 20 | + "[1 ...]")) |
| 21 | + (is (= (binding [*print-length* 2] (str [1 2 3 4 5 6 7 8 9 0])) |
| 22 | + "[1 2 ...]")) |
| 23 | + (is (= (binding [*print-length* 10] (str [1 2 3 4 5 6 7 8 9 0])) |
| 24 | + "[1 2 3 4 5 6 7 8 9 0]")) |
| 25 | + ;; CLJS-804 |
| 26 | + (is (= (binding [*print-length* 10] (str {:foo "bar"})) |
| 27 | + "{:foo \"bar\"}")) |
| 28 | + (is (= (binding [*print-length* 0] (str {:foo "bar" :baz "woz"})) |
| 29 | + "{...}")) |
| 30 | + (is (#{"{:foo \"bar\", ...}" "{:baz \"woz\", ...}"} |
| 31 | + (binding [*print-length* 1] (str {:foo "bar" :baz "woz"})))) |
| 32 | + (is (#{"{:foo \"bar\", :baz \"woz\"}" "{:baz \"woz\", :foo \"bar\"}"} |
| 33 | + (binding [*print-length* 10] (str {:foo "bar" :baz "woz"}))))) |
| 34 | + ) |
| 35 | + |
| 36 | +(deftest test-print-with-opts |
| 37 | + (testing "Testing printing with opts - :more-marker" |
| 38 | + ; CLJS-1016 |
| 39 | + (is (= (pr-str-with-opts [[1 2 3]] {:more-marker "<MORE-MARKER>" :print-length 0}) |
| 40 | + "[<MORE-MARKER>]")) |
| 41 | + (is (= (pr-str-with-opts [[1 2 3]] {:more-marker "\u2026" :print-length 1}) |
| 42 | + "[1 \u2026]")) |
| 43 | + (is (#{"#{1 2 \u2026}" "#{1 3 \u2026}" |
| 44 | + "#{2 1 \u2026}" "#{2 3 \u2026}" |
| 45 | + "#{3 1 \u2026}" "#{3 2 \u2026}"} |
| 46 | + (pr-str-with-opts [#{1 2 3}] {:more-marker "\u2026" :print-length 2}))) |
| 47 | + (is (= (pr-str-with-opts ['(1 2 3)] {:more-marker "\u2026" :print-length 2}) |
| 48 | + "(1 2 \u2026)")) |
| 49 | + (is (#{"{:1 1, :2 2, \u2026}" "{:1 1, :3 3, \u2026}" |
| 50 | + "{:2 2, :1 1, \u2026}" "{:2 2, :3 3, \u2026}" |
| 51 | + "{:3 3, :1 1, \u2026}" "{:3 3, :2 2, \u2026}"} |
| 52 | + (pr-str-with-opts [{:1 1 :2 2 :3 3}] {:more-marker "\u2026" :print-length 2})))) |
| 53 | + |
| 54 | + (testing "Testing printing with opts - :alt-impl" |
| 55 | + ; CLJS-1010 |
| 56 | + (is (= (pr-str-with-opts [[1 2 3]] {:alt-impl (fn [obj writer opts] ((:fallback-impl opts) obj writer opts))}) |
| 57 | + "[1 2 3]")) |
| 58 | + (is (= (pr-str-with-opts [[1 2 3]] {:alt-impl (fn [obj writer opts] (-write writer (str "<" obj ">")))}) |
| 59 | + "<[1 2 3]>")) |
| 60 | + (is (= (pr-str-with-opts [[:start 1 2 [:middle] 3 4 :end] :standalone] {:alt-impl (fn [obj writer opts] |
| 61 | + (if (keyword? obj) |
| 62 | + (-write writer (str "|" (name obj) "|")) |
| 63 | + ((:fallback-impl opts) obj writer opts)))}) |
| 64 | + "[|start| 1 2 [|middle|] 3 4 |end|] |standalone|")) |
| 65 | + (is (= (pr-str-with-opts [[1 2 3]] {:alt-impl (fn [obj writer opts])}) |
| 66 | + ""))) |
| 67 | + ) |
| 68 | + |
| 69 | +(defrecord PrintMe [a b]) |
| 70 | + |
| 71 | +(deftest test-printing |
| 72 | + (testing "Testing pr-str" |
| 73 | + (is (= (pr-str) "")) |
| 74 | + (is (= (pr-str 1) "1")) |
| 75 | + (is (= (pr-str -1) "-1")) |
| 76 | + (is (= (pr-str -1.5) "-1.5")) |
| 77 | + (is (= (pr-str [3 4]) "[3 4]")) |
| 78 | + (is (= (pr-str "foo") "\"foo\"")) |
| 79 | + (is (= (pr-str :hello) ":hello")) |
| 80 | + (is (= (pr-str 'goodbye) "goodbye")) |
| 81 | + ;;(is (= (pr-str #{1 2 3}) "#{1 2 3}")) |
| 82 | + (is (= (pr-str '(7 8 9)) "(7 8 9)")) |
| 83 | + (is (= (pr-str '(deref foo)) "(deref foo)")) |
| 84 | + (is (= (pr-str '(quote bar)) "(quote bar)")) |
| 85 | + (is (= (pr-str 'foo/bar) "foo/bar")) |
| 86 | + (is (= (pr-str \a) "\"a\"")) |
| 87 | + (is (= (pr-str :foo/bar) ":foo/bar")) |
| 88 | + (is (= (pr-str nil) "nil")) |
| 89 | + (is (= (pr-str true) "true")) |
| 90 | + (is (= (pr-str false) "false")) |
| 91 | + (is (= (pr-str "string") "\"string\"")) |
| 92 | + (is (= (pr-str ["üñîçó∂£" :ทดสอบ/你好 'こんにちは]) "[\"üñîçó∂£\" :ทดสอบ/你好 こんにちは]")) |
| 93 | + (is (= (pr-str "escape chars \t \r \n \\ \" \b \f") "\"escape chars \\t \\r \\n \\\\ \\\" \\b \\f\"")) |
| 94 | + (is (= (pr-str (PrintMe. 1 2)) "#cljs.printing-test.PrintMe{:a 1, :b 2}")) |
| 95 | + (is (= (pr-str (js/Date. "2010-11-12T13:14:15.666-05:00")) |
| 96 | + "#inst \"2010-11-12T18:14:15.666-00:00\"")) |
| 97 | + (doseq [month (range 1 13) |
| 98 | + day (range 1 29) |
| 99 | + hour (range 1 23)] |
| 100 | + (let [pad (fn [n] |
| 101 | + (if (< n 10) |
| 102 | + (str "0" n) |
| 103 | + n)) |
| 104 | + inst (str "2010-" (pad month) "-" (pad day) "T" (pad hour) ":14:15.666-00:00")] |
| 105 | + (is (= (pr-str (js/Date. inst)) (str "#inst \"" inst "\""))))) |
| 106 | + (let [uuid-str "550e8400-e29b-41d4-a716-446655440000" |
| 107 | + uuid (cljs.core/uuid uuid-str)] |
| 108 | + (is (= (pr-str uuid) (str "#uuid \"" uuid-str "\"")))) |
| 109 | + ;; pr-str PersistentQueueSeq - CLJS-800 |
| 110 | + (is (= (pr-str (rest (conj cljs.core.PersistentQueue.EMPTY 1 2 3))) "(2 3)")) |
| 111 | + (is (= "\"asdf\" \"asdf\"" (pr-str "asdf" "asdf"))) |
| 112 | + ;; Different hash map order on self-host |
| 113 | + (is (#{"[1 true {:a 2, :b #\"x\\\"y\"} #js [3 4]]" |
| 114 | + "[1 true {:b #\"x\\\"y\", :a 2} #js [3 4]]"} |
| 115 | + (pr-str [1 true {:a 2 :b #"x\"y"} (array 3 4)])))) |
| 116 | + (testing "Testing print-str" |
| 117 | + (is (= (print-str "asdf") "asdf"))) |
| 118 | + (testing "Testing println-str" |
| 119 | + (is (= (println-str "asdf") "asdf\n"))) |
| 120 | + (testing "Testing prn-str" |
| 121 | + (is (= (prn-str) "\n")) |
| 122 | + (is (= (prn-str "asdf") "\"asdf\"\n")) |
| 123 | + ;; Different hash map order on self-host |
| 124 | + (is (#{"[1 true {:a 2, :b 42} #js [3 4]]\n" |
| 125 | + "[1 true {:b 42, :a 2} #js [3 4]]\n"} |
| 126 | + (prn-str [1 true {:a 2 :b 42} (array 3 4)])))) |
| 127 | + (testing "Testing with-out-str" |
| 128 | + (is (= "12" (with-out-str (print 1) (print 2)))) |
| 129 | + (is (= "12" (with-out-str (*print-fn* 1) (*print-fn* 2)))))) |
0 commit comments