KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_jumper_groups.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/tools/old/interface.hpp>
22
23#include <vector>
24
25#include <string_utils.h>
26#include <jumper_group.h>
27#include <lib_symbol.h>
28
31
32
36std::ostream& boost_test_print_type( std::ostream& aStream, const JUMPER_GROUP_SET& aGroups )
37{
38 if( aGroups.IsEmpty() )
39 return aStream << "<no groups>";
40
41
42 std::vector<JUMPER_GROUP> groups = aGroups.GetAll();
43
44 aStream << groups.size() << " group(s): ";
45 for( size_t ii = 0; ii < groups.size(); ++ii )
46 {
47 if( ii > 0 )
48 aStream << " ";
49
50 aStream << "(";
51
52 for( const wxString& name : groups[ii].GetNames() )
53 aStream << "\"" << name << "\" ";
54
55 aStream << ")";
56 }
57
58 return aStream;
59}
60
61
62BOOST_AUTO_TEST_SUITE( ParseJumperGroupSexprs )
63
64
66{
67 std::string input;
68 std::vector<std::set<wxString>> expected_groups;
69};
70
71
72BOOST_AUTO_TEST_CASE( WellFormedJumperPinGroupsAreParsed )
73{
74 const std::vector<PARSED_JUMPER_TEST_CASE> valid_jumper_groups = {
75 // Nothing at all
76 // I.e. just `(jumper_pin_groups)`
77 {
78 "",
79 {},
80 },
81 // Empty groups list
82 {
83 "()",
84 {},
85 },
86 // A group whose only name is blank contributes nothing
87 {
88 "(\"\")",
89 {},
90 },
91 // A single group with a single pin
92 {
93 "(\"1\")",
94 { { "1" } },
95 },
96 // A single group with multiple pins
97 {
98 "(\"1\" \"2\" \"3\")",
99 { { "1", "2", "3" } },
100 },
101 // Multiple groups with a single pin each
102 {
103 "(\"1\") (\"2\") (\"3\")",
104 { { "1" }, { "2" }, { "3" } },
105 },
106 // Multiple groups with multiple pins each
107 {
108 "(\"1\" \"2\") (\"3\" \"4\") (\"5\" \"6\")",
109 { { "1", "2" }, { "3", "4" }, { "5", "6" } },
110 },
111 };
112
113 for( const PARSED_JUMPER_TEST_CASE& test_case : valid_jumper_groups )
114 {
115 BOOST_TEST_CONTEXT( "Testing well-formed jumper pin group: " << test_case.input )
116 {
117 std::string good = "(symbol \"Good\" (jumper_pin_groups " + test_case.input + "))";
118
119 std::vector<LIB_SYMBOL*> loaded = SCH_IO_KICAD_SEXPR::ParseLibSymbols( good, "good jumper groups" );
120
121 BOOST_REQUIRE_EQUAL( loaded.size(), 1u );
122 LIB_SYMBOL* const symbol = loaded.front();
123 BOOST_REQUIRE( symbol );
124
125 // Build the expected groups from the test case data
126 JUMPER_GROUP_SET expected_groups;
127 for( const std::set<wxString>& groupNames : test_case.expected_groups )
128 expected_groups.Add( std::move( groupNames ) );
129
130 // BOOST_TEST == doesn't print nicely
131 BOOST_CHECK_EQUAL( symbol->JumperPinGroups(), expected_groups );
132 }
133 }
134}
135
136
137BOOST_AUTO_TEST_CASE( MalformedJumperPinGroupsAreRejected )
138{
139 const std::vector<std::string> throwing_jumper_groups = {
140 // A literal string must be rejected, even if empty
141 "\"\"",
142 "\"A\"", // Sentry KICAD-1A3H
143 // Literal non-strings
144 "42",
145 "42.42",
146 // Groups of non-strings
147 "(42)",
148 "(42.42)",
149 "(42 42)",
150 "((42 42))",
151 "(bad_keyword 42)",
152 };
153
154 for( const std::string& bad_jumper_group : throwing_jumper_groups )
155 {
156 BOOST_TEST_CONTEXT( "Testing malformed jumper pin group: " << bad_jumper_group )
157 {
158 std::string bad = "(symbol \"Bad\" (jumper_pin_groups " + bad_jumper_group + "))";
159
160 BOOST_CHECK_THROW( SCH_IO_KICAD_SEXPR::ParseLibSymbols( bad, "bad jumper groups" ), IO_ERROR );
161 }
162 }
163}
164
165
const char * name
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Represents an (ordered) list of JUMPER_GROUP objects.
void Add(JUMPER_GROUP aGroup)
Add a JUMPER_GROUP to the set.
const std::vector< JUMPER_GROUP > & GetAll() const
bool IsEmpty() const
Define a library symbol object.
Definition lib_symbol.h:114
JUMPER_GROUP_SET & JumperPinGroups()
Each jumper pin group is a set of pin numbers that should be treated as internally connected.
Definition lib_symbol.h:880
static std::vector< LIB_SYMBOL * > ParseLibSymbols(std::string &aSymbolText, std::string aSource, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
std::vector< std::set< wxString > > expected_groups
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::ostream & boost_test_print_type(std::ostream &aStream, const JUMPER_GROUP_SET &aGroups)
Boost.Test print helper for jumper group collections.
BOOST_AUTO_TEST_CASE(WellFormedJumperPinGroupsAreParsed)
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_CHECK_EQUAL(result, "25.4")