|
| 1 | +Red [ |
| 2 | + Title: "CSV codec test script" |
| 3 | + Author: ["Boleslav Březovský" "Oldes"] |
| 4 | + File: %csv-test.r3 |
| 5 | + Rights: "Copyright (C) 2011-2019 Red Foundation. All rights reserved." |
| 6 | + License: "BSD-3 - https://github.com/red/red/blob/origin/BSD-3-License.txt" |
| 7 | + Needs: [%../quick-test-module.r3] |
| 8 | + Note: { |
| 9 | + Base of this file are Red CSV tests. Rebol CSV codec does not have same results! |
| 10 | + Main differencies between Red as Rebol are: |
| 11 | + 1. when not used block of blocks with `to-csv`, than result does not have a LF on the tail. |
| 12 | + 2. Rebol does not support `/skip` and `/quote` refinements. |
| 13 | + 3. Rebol does not support `to-csv` input as a block of objects or maps. |
| 14 | + 4. No `/header`, `/as-columns`, `/as-records`, `/flat`, `/align` and `/trim` refinements. |
| 15 | + 5. Rebol always quotes strings even when not necessary. |
| 16 | + } |
| 17 | +] |
| 18 | + |
| 19 | +~~~start-file~~~ "CSV codec" |
| 20 | + |
| 21 | +try [import 'csv] |
| 22 | +if find codecs 'CSV [ |
| 23 | +===start-group=== "to-csv" |
| 24 | + --test-- "to-csv-1-single-line" |
| 25 | + --assert {"none"} = to-csv [none] ; no LF at tail like in Red! |
| 26 | + --assert {1,2,3} = to-csv [1 2 3] |
| 27 | + --assert {1,2,3^/} = to-csv [[1 2 3]] |
| 28 | + --assert {"hello","world"} = to-csv [hello world] |
| 29 | + --assert {"hello world","from","Red"} = to-csv ["hello world" from Red] |
| 30 | + --assert {"hello,world","hello","world"} = to-csv ["hello,world" hello world] |
| 31 | + --assert {1,"^/",3} = to-csv [1 "^/" 3] |
| 32 | + --assert {1,2022-05-02 10:06:32,3,"a",""""} = [1 2-May-2022/10:06:32 $3 #"a" #"^""] |
| 33 | + --test-- "to-csv-2-multi-line" |
| 34 | + --assert "1,2,3^/4,5,6^/" = to-csv [[1 2 3][4 5 6]] |
| 35 | + --assert "1,2,3^/4,5,6^/7,8,9^/" = to-csv [[1 2 3][4 5 6][7 8 9]] |
| 36 | +; --test-- "to-csv-3-skip" |
| 37 | +; --assert "1,2,3^/4,5,6^/" = to-csv/skip [1 2 3 4 5 6] 3 |
| 38 | + --test-- "to-csv-4-with" |
| 39 | + --assert "1;2;3" = to-csv/with [1 2 3] #";" |
| 40 | + --assert "1;2;3^/4;5;6^/" = to-csv/with [[1 2 3][4 5 6]] #";" |
| 41 | +; --assert "1;2;3^/4;5;6^/" = to-csv/with/skip [1 2 3 4 5 6] #";" 3 |
| 42 | + --test-- "to-csv-5-quote" |
| 43 | + --assert equal? {"hel""lo"} to-csv [{hel"lo}] |
| 44 | + --assert equal? {"hel""""lo"} to-csv [{hel""lo}] |
| 45 | +; --assert equal? |
| 46 | +; "'hello world',',',''''" |
| 47 | +; to-csv/quote ["hello world" "," "'"] #"'" |
| 48 | + --test-- "to-csv-6-unaligned" |
| 49 | + --assert "1,2,3^/1,2,3,4^/" = to-csv [[1 2 3][1 2 3 4]] ; it is error in Red |
| 50 | + --test-- "to-csv-7-fix-4424" |
| 51 | + --assert {"x x"} = to-csv tmp: ["x x"] |
| 52 | + --assert tmp = ["x x"] ; we need to make sure original was not modified |
| 53 | + unset 'tmp |
| 54 | +; --test-- "to-csv-block-of-keyval" |
| 55 | +; --assert {a,b^/1,2^/3,4^/} = to-csv reduce [object [a: 1 b: 2] object [a: 3 b: 4]] |
| 56 | +; --assert {a,b^/1,2^/3,4^/} = to-csv reduce [make map! [a: 1 b: 2] make map! [a: 3 b: 4]] |
| 57 | +===end-group=== |
| 58 | + |
| 59 | +===start-group=== "load-csv" |
| 60 | + --test-- "load-csv-1-single-line" |
| 61 | + --assert [["1" "2" "3"]] = load-csv {1,2,3^/} |
| 62 | + --assert [["1" "2" "3"]] = load-csv {1,2,3} |
| 63 | + --test-- "load-csv-2-multi-line" |
| 64 | + --assert [["1" "2" "3"]["4" "5" "6"]] = load-csv {1,2,3^/4,5,6^/} |
| 65 | + --assert [["1" "2" "3"]["4" "5" "6"]] = load-csv {1,2,3^/4,5,6} |
| 66 | + --test-- "load-csv-3-spaces" |
| 67 | + --assert [["1" "2 " " 3"]] = load-csv {1,2 , 3^/} |
| 68 | + --assert [["1" "2 " " 3"]] = load-csv {1,2 , 3} |
| 69 | + --test-- "load-csv-4-quotes" |
| 70 | + --assert [["aaa" {b"bb} { "ccc"}]] = load-csv {"aaa","b""bb", "ccc"} |
| 71 | + --test-- "load-csv-5-comma" |
| 72 | + --assert [["a" "b,b" "c"]] = load-csv {a,"b,b",c} |
| 73 | + --test-- "load-csv-6-newline" |
| 74 | + --assert [["a" "b^/b" "c"]] = load-csv {a,"b^/b",c} |
| 75 | +===end-group=== |
| 76 | + |
| 77 | +===start-group=== "load-csv-with" |
| 78 | + --test-- "load-csv-with-1-single-line" |
| 79 | + --assert [["1" "2" "3"]] = load-csv/with {1;2;3^/} #";" |
| 80 | + --assert [["1" "2" "3"]] = load-csv/with {1;2;3} #";" |
| 81 | + --test-- "load-csv-with-2-multi-line" |
| 82 | + --assert [["1" "2" "3"]["4" "5" "6"]] = load-csv/with {1;2;3^/4;5;6^/} #";" |
| 83 | + --assert [["1" "2" "3"]["4" "5" "6"]] = load-csv/with {1;2;3^/4;5;6} #";" |
| 84 | +===end-group=== |
| 85 | + |
| 86 | +;===start-group=== "load-csv-header" |
| 87 | +; --test-- "load-csv-header-1" |
| 88 | +; --assert #("a" ["1"] "b" ["2"] "c" ["3"]) = load-csv/header {a,b,c^/1,2,3} |
| 89 | +; --assert #("a" ["1" "4"] "b" ["2" "5"] "c" ["3" "6"]) |
| 90 | +; = load-csv/header {a,b,c^/1,2,3^/4,5,6} |
| 91 | +; --test-- "load-csv-header-2-error" |
| 92 | +; --assert error? try [load-csv/header {a,b,c}] |
| 93 | +;===end-group=== |
| 94 | +; |
| 95 | +;===start-group=== "load-csv-as-columns" |
| 96 | +; --test-- "load-csv-as-columns-1" |
| 97 | +; --assert #("A" ["1"] "B" ["2"] "C" ["3"]) = load-csv/as-columns {1,2,3} |
| 98 | +; --assert #("A" ["1" "4"] "B" ["2" "5"] "C" ["3" "6"]) |
| 99 | +; = load-csv/as-columns {1,2,3^/4,5,6} |
| 100 | +; --test-- "load-csv-as-columns" |
| 101 | +; --assert #("a" ["1"] "b" ["2"] "c" ["3"]) |
| 102 | +; = load-csv/as-columns/header {a,b,c^/1,2,3} |
| 103 | +;===end-group=== |
| 104 | +; |
| 105 | +;===start-group=== "load-csv-as-records" |
| 106 | +; --test-- "load-csv-as-records-1" |
| 107 | +; --assert [#("A" "1" "B" "2" "C" "3")] = load-csv/as-records {1,2,3} |
| 108 | +; --assert [ |
| 109 | +; #("A" "1" "B" "2" "C" "3") |
| 110 | +; #("A" "4" "B" "5" "C" "6") |
| 111 | +; ] = load-csv/as-records {1,2,3^/4,5,6} |
| 112 | +; --test-- "load-csv-as-records-2-header" |
| 113 | +; --assert [ |
| 114 | +; #("A" "1" "B" "2" "C" "3") |
| 115 | +; #("A" "4" "B" "5" "C" "6") |
| 116 | +; ] = load-csv/as-records/header {A,B,C^/1,2,3^/4,5,6} |
| 117 | +; --test-- "load-csv-as-records-2-header-error" |
| 118 | +; --assert error? try [load-csv/as-records/header {a,b,c}] |
| 119 | +;===end-group=== |
| 120 | +; |
| 121 | +;===start-group=== "load-csv-flat" |
| 122 | +; --test-- "load-csv-flat-1" |
| 123 | +; --assert ["1" "2" "3"] = load-csv/flat {1,2,3} |
| 124 | +; --assert ["1" "2" "3" "4" "5" "6"] = load-csv/flat {1,2,3^/4,5,6} |
| 125 | +;===end-group=== |
| 126 | + |
| 127 | +;===start-group=== "load-csv-align" |
| 128 | +;===end-group=== |
| 129 | + |
| 130 | +;===start-group=== "load-csv-trim" |
| 131 | +; --test-- "load-csv-trim-1" |
| 132 | +; --assert [["1" "2" "3" "4"]] = load-csv/trim {1, 2, 3 , 4} |
| 133 | +;===end-group=== |
| 134 | + |
| 135 | +;===start-group=== "load-csv-quote" |
| 136 | +; --test-- "load-csv-quote-1" |
| 137 | +; --assert equal? |
| 138 | +; load-csv/quote "'hello world',',',''''^/" #"'" |
| 139 | +; [["hello world" "," "'"]] |
| 140 | +;===end-group=== |
| 141 | + |
| 142 | +;===start-group=== "load-csv-wrong-refinements" |
| 143 | +; --test-- "load-csv-as-columns-as-records" |
| 144 | +; --assert error? try [load-csv/as-columns/as-records "1,2,3"] |
| 145 | +;; --test-- "load-csv-flat-align" |
| 146 | +;; --assert error? try [load-csv/flat/align "1,2,3"] |
| 147 | +; --test-- "load-csv-flat-as-records" |
| 148 | +; --assert error? try [load-csv/flat/as-records "1,2,3"] |
| 149 | +; --test-- "load-csv-flat-as-columns" |
| 150 | +; --assert error? try [load-csv/flat/as-columns "1,2,3"] |
| 151 | +;===end-group=== |
| 152 | + |
| 153 | +===start-group=== "csv-codec" |
| 154 | + res: [["1" "2" "3"]] |
| 155 | + str: {1,2,3^/} |
| 156 | +; --test-- "csv-codec load/as" |
| 157 | +; --assert res = load/as str 'csv ;<-- this should work in Rebol as well! |
| 158 | + --test-- "csv-codec decode" |
| 159 | + --assert res = decode 'csv str |
| 160 | +; --test-- "csv-codec save/as" |
| 161 | +; --assert {1,2,3^/} = save/as none [1 2 3] 'csv |
| 162 | +; --assert {1,2,3^/} = save/as none [[1 2 3]] 'csv |
| 163 | +; --assert {1,2,3^/} = save/as none ["1" "2" "3"] 'csv |
| 164 | +; --assert {1,2,3^/} = save/as none [["1" "2" "3"]] 'csv |
| 165 | + --test-- "csv-codec encode" |
| 166 | + --assert {1,2,3} = encode 'csv [1 2 3] |
| 167 | + --assert {1,2,3^/} = encode 'csv [[1 2 3]] |
| 168 | + --assert {"1","2","3"} = encode 'csv ["1" "2" "3"] |
| 169 | + --assert {"1","2","3"^/} = encode 'csv [["1" "2" "3"]] |
| 170 | + |
| 171 | +===end-group=== |
| 172 | +] |
| 173 | + |
| 174 | +~~~end-file~~~ |
0 commit comments