KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_csv.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <io/csv.h>
23
24#include <wx/sstream.h>
25
26
30template <typename T>
31std::ostream& boost_test_print_type( std::ostream& os, const std::vector<std::vector<T>>& aTable )
32{
33 os << "TABLE[ " << std::endl;
34 for( size_t i = 0; i < aTable.size(); ++i )
35 {
36 const auto& row = aTable[i];
37 os << " Row " << i << " [ ";
38 for( size_t j = 0; j < row.size(); ++j )
39 {
40 os << row[j];
41 if( j < row.size() - 1 )
42 os << ", ";
43 }
44 os << "] " << std::endl;
45 }
46 os << " ]" << std::endl;
47 return os;
48}
49
50
51static bool TableDataEqual( const std::vector<std::vector<wxString>>& aExpected,
52 const std::vector<std::vector<wxString>>& aActual )
53{
54 if( aExpected.size() != aActual.size() )
55 {
56 BOOST_TEST_MESSAGE( "Row count mismatch: " << aExpected.size() << " != " << aActual.size() );
57 return false;
58 }
59
60 for( size_t i = 0; i < aExpected.size(); ++i )
61 {
62 BOOST_TEST_INFO_SCOPE( "Row " << i );
63 if( aExpected[i].size() != aActual[i].size() )
64 return false;
65
66 for( size_t j = 0; j < aExpected[i].size(); ++j )
67 {
68 if( aExpected[i][j] != aActual[i][j] )
69 return false;
70 }
71 }
72
73 return true;
74}
75
76
77BOOST_AUTO_TEST_SUITE( CsvTests )
78
79
81{
82 wxString m_name;
83 std::vector<std::vector<wxString>> m_rows;
84 wxString m_expected;
85};
86
87
88BOOST_AUTO_TEST_CASE( BasicRoundTrips )
89{
90 // clang-format off
91 static const std::vector<CsvRoundTripCase> testCases = {
92 {
93 "Basic CSV, Double Quoted, Backslash escaped",
94 {
95 { "Head 1", "Head 2", "Head, \"3\"" },
96 { "Row 1 Col 1", "Row 1 Col 2", "Row 1 Col 3" }
97 },
98 "\"Head 1\",\"Head 2\",\"Head, \"\"3\"\"\"\n"
99 "\"Row 1 Col 1\",\"Row 1 Col 2\",\"Row 1 Col 3\"\n",
100 },
101 };
102 // clang-format on
103
104 for( const auto& testCase : testCases )
105 {
106 BOOST_TEST_INFO_SCOPE( testCase.m_name );
107
108 wxStringOutputStream os;
109 CSV_WRITER writer( os );
110 writer.WriteLines( testCase.m_rows );
111
112 BOOST_CHECK_EQUAL( os.GetString(), testCase.m_expected );
113
114 std::vector<std::vector<wxString>> readRows;
115
116 bool result = AutoDecodeCSV( os.GetString(), readRows );
117 BOOST_CHECK( result );
118 BOOST_CHECK_PREDICATE( TableDataEqual, ( testCase.m_rows )( readRows ) );
119 }
120}
121
122
124{
125 wxString m_name;
126 wxString m_input;
127 std::vector<std::vector<wxString>> m_expectedRows;
128};
129
130
132{
133 // clang-format off
134 static const std::vector<CsvDecodeCase> testCases = {
135 {
136 "Basic TSV, Double Quoted",
137 "\"Head 1\"\t\"Head 2\"\t\"Head, 3\"\n"
138 "\"Row 1 Col 1\"\t\"Row 1 Col 2\"\t\"Row 1 Col 3\"\n",
139 {
140 { "Head 1", "Head 2", "Head, 3" },
141 { "Row 1 Col 1", "Row 1 Col 2", "Row 1 Col 3" }
142 },
143 }
144 };
145 // clang-format on
146
147 for( const auto& testCase : testCases )
148 {
149 BOOST_TEST_INFO_SCOPE( testCase.m_name );
150
151 std::vector<std::vector<wxString>> readRows;
152
153 bool result = AutoDecodeCSV( testCase.m_input, readRows );
154 BOOST_CHECK( result );
155 BOOST_CHECK_PREDICATE( TableDataEqual, ( testCase.m_expectedRows )( readRows ) );
156 }
157}
158
159
Definition: csv.h:33
void WriteLines(const std::vector< std::vector< wxString > > &aRows)
Write a vector of rows to the stream.
Definition: csv.cpp:18
bool AutoDecodeCSV(const wxString &aInput, std::vector< std::vector< wxString > > &aData)
Try to guess the format of a T/CSV file and decode it into aData.
Definition: csv.cpp:66
std::vector< std::vector< wxString > > m_expectedRows
Definition: test_csv.cpp:127
wxString m_name
Definition: test_csv.cpp:125
wxString m_input
Definition: test_csv.cpp:126
wxString m_expected
Definition: test_csv.cpp:84
std::vector< std::vector< wxString > > m_rows
Definition: test_csv.cpp:83
wxString m_name
Definition: test_csv.cpp:82
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK_EQUAL(ret, c.m_exp_result)
BOOST_AUTO_TEST_CASE(BasicRoundTrips)
Definition: test_csv.cpp:88
static bool TableDataEqual(const std::vector< std::vector< wxString > > &aExpected, const std::vector< std::vector< wxString > > &aActual)
Definition: test_csv.cpp:51
std::ostream & boost_test_print_type(std::ostream &os, const std::vector< std::vector< T > > &aTable)
Define a stream function for logging this type.
Definition: test_csv.cpp:31
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_PREDICATE(ArePolylineEndPointsNearCircle,(chain)(c.m_geom.m_center_point)(radius)(accuracy+epsilon))
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")