2 * @brief Test CSV escaping routines
4 /* Copyright (C) 2011,2012,2013,2015 Olly Betts
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include "csvescape.h"
40 #define UNCHANGED NULL
42 static testcase csv_testcases
[] = {
44 { "hello world", UNCHANGED
},
45 { "#$!%", UNCHANGED
},
46 { "\\ foo \\", UNCHANGED
},
47 // Check 3 examples from docs/omegascript.rst:
48 { "Safe in CSV!", UNCHANGED
},
49 { "Not \"safe\"", "\"Not \"\"safe\"\"\"" },
50 { "3, 2, 1", "\"3, 2, 1\"" },
52 { "Two\nlines", "\"Two\nlines\"" },
53 { "Two\r\nDOS lines", "\"Two\r\nDOS lines\"" },
54 // Test every possible character (except '\0') encodes as it should:
59 { "\x01\x02\x03\x04\x05\x06\x07", UNCHANGED
},
60 { "\x08\x09\x0B\x0C\x0E\x0F", UNCHANGED
},
61 { "\x10\x11\x12\x13\x14\x15\x16\x17", UNCHANGED
},
62 { "\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", UNCHANGED
},
63 { " !#$%&'()*+-./", UNCHANGED
},
64 { "0123456789:;<=>?", UNCHANGED
},
65 { "@ABCDEFGHIJKLMNO", UNCHANGED
},
66 { "PQRSTUVWXYZ[\\]^_", UNCHANGED
},
67 { "`abcdefghijklmno", UNCHANGED
},
68 { "pqrstuvwxyz{|}~\x7F", UNCHANGED
},
69 { "\x80\x81\x82\x83\x84\x85\x86\x87", UNCHANGED
},
70 { "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F", UNCHANGED
},
71 { "\x90\x91\x92\x93\x94\x95\x96\x97", UNCHANGED
},
72 { "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F", UNCHANGED
},
73 { "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7", UNCHANGED
},
74 { "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF", UNCHANGED
},
75 { "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7", UNCHANGED
},
76 { "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF", UNCHANGED
},
77 { "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7", UNCHANGED
},
78 { "\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF", UNCHANGED
},
79 { "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7", UNCHANGED
},
80 { "\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF", UNCHANGED
},
81 { "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7", UNCHANGED
},
82 { "\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF", UNCHANGED
},
83 { "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7", UNCHANGED
},
84 { "\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF", UNCHANGED
},
89 for (testcase
* e
= csv_testcases
; e
->input
; ++e
) {
90 const char * input
= e
->input
;
92 string result
= input
;
94 const char * expected
= e
->result
;
95 if (expected
== UNCHANGED
) expected
= input
;
96 if (result
!= expected
) {
97 cerr
<< "csv_escape of '" << input
<< "' should be "
98 "'" << expected
<< "', got '" << result
<< "'" << endl
;
102 // Test csv_escape_always().
104 csv_escape_always(result
);
105 string expected_always
= expected
;
106 if (e
->result
== UNCHANGED
) {
107 expected_always
.insert(0, "\"");
108 expected_always
.append("\"");
110 if (result
!= expected_always
) {
111 cerr
<< "csv_escape_always of '" << input
<< "' should be "
112 "'" << expected
<< "', got '" << result
<< "'" << endl
;