1 | #include "test.h" |
2 | #include <gtest/gtest.h> |
3 | |
4 | #include <base/system.h> |
5 | #include <engine/shared/csv.h> |
6 | |
7 | static void Expect(int NumColumns, const char *const *ppColumns, const char *pExpected) |
8 | { |
9 | CTestInfo Info; |
10 | |
11 | IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE); |
12 | ASSERT_TRUE(File); |
13 | CsvWrite(File, NumColumns, ppColumns); |
14 | io_close(io: File); |
15 | |
16 | char aBuf[1024]; |
17 | File = io_open(filename: Info.m_aFilename, flags: IOFLAG_READ); |
18 | ASSERT_TRUE(File); |
19 | int Read = io_read(io: File, buffer: aBuf, size: sizeof(aBuf)); |
20 | io_close(io: File); |
21 | fs_remove(filename: Info.m_aFilename); |
22 | |
23 | ASSERT_TRUE(Read >= 1); |
24 | Read -= 1; |
25 | ASSERT_EQ(aBuf[Read], '\n'); |
26 | aBuf[Read] = 0; |
27 | |
28 | #if defined(CONF_FAMILY_WINDOWS) |
29 | ASSERT_TRUE(Read >= 1); |
30 | Read -= 1; |
31 | ASSERT_EQ(aBuf[Read], '\r'); |
32 | aBuf[Read] = 0; |
33 | #endif |
34 | |
35 | for(int i = 0; i < Read; i++) |
36 | { |
37 | EXPECT_NE(aBuf[i], 0); |
38 | } |
39 | EXPECT_STREQ(aBuf, pExpected); |
40 | } |
41 | |
42 | TEST(Csv, Simple) |
43 | { |
44 | const char *apCols1[] = {"a" , "b" }; |
45 | Expect(NumColumns: 2, ppColumns: apCols1, pExpected: "a,b" ); |
46 | const char *apCols2[] = {"こんにちは" }; |
47 | Expect(NumColumns: 1, ppColumns: apCols2, pExpected: "こんにちは" ); |
48 | const char *apCols3[] = {"я" , "" , "й" }; |
49 | Expect(NumColumns: 3, ppColumns: apCols3, pExpected: "я,,й" ); |
50 | const char *apCols4[] = {"" }; |
51 | Expect(NumColumns: 1, ppColumns: apCols4, pExpected: "" ); |
52 | const char *apCols5[] = {0}; |
53 | Expect(NumColumns: 0, ppColumns: apCols5, pExpected: "" ); |
54 | } |
55 | |
56 | TEST(Csv, LetTheQuotingBegin) |
57 | { |
58 | const char *apCols1[] = {"\"" }; |
59 | Expect(NumColumns: 1, ppColumns: apCols1, pExpected: "\"\"\"\"" ); |
60 | const char *apCols2[] = {"," }; |
61 | Expect(NumColumns: 1, ppColumns: apCols2, pExpected: "\",\"" ); |
62 | const char *apCols3[] = {",," , ",\"\"\"" }; |
63 | Expect(NumColumns: 2, ppColumns: apCols3, pExpected: "\",,\",\",\"\"\"\"\"\"\"" ); |
64 | const char *apCols4[] = {"\"," , " " }; |
65 | Expect(NumColumns: 2, ppColumns: apCols4, pExpected: "\"\"\",\", " ); |
66 | } |
67 | |