KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue23023_http_fields.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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
32
33#include <boost/test/unit_test.hpp>
34
35#include <algorithm>
36#include <tuple>
37
38#include <wx/arrstr.h> // for MSVC to see std::vector<wxString> is exported from wx
39
40#include <json_common.h>
43
44
45BOOST_AUTO_TEST_SUITE( Issue23023HttpFields )
46
47
48static const HTTP_LIB_PART::field_type* findField( const HTTP_LIB_PART& aPart, const std::string& aName )
49{
50 for( const auto& [name, properties] : aPart.fields )
51 {
52 if( name == aName )
53 return &properties;
54 }
55
56 return nullptr;
57}
58
59
61BOOST_AUTO_TEST_CASE( DetailRecordPopulatesFields )
62{
63 const nlohmann::json detail = R"({
64 "id": "1",
65 "name": "RC0603FR-0710KL",
66 "symbolIdStr": "Device:R",
67 "exclude_from_bom": "False",
68 "exclude_from_board": "False",
69 "exclude_from_sim": "True",
70 "fields": {
71 "footprint": { "value": "Resistor_SMD:R_0603_1608Metric", "visible": "False" },
72 "datasheet": { "value": "www.kicad.org", "visible": "False" },
73 "value": { "value": "10k" },
74 "reference": { "value": "R" }
75 }
76 })"_json;
77
78 HTTP_LIB_PART part;
79
80 BOOST_CHECK( setPartExtendedData( detail, part ) );
81
82 BOOST_CHECK_EQUAL( part.symbolIdStr, "Device:R" );
85
86 BOOST_REQUIRE_EQUAL( part.fields.size(), 4u );
87
88 const HTTP_LIB_PART::field_type* value = findField( part, "value" );
89 BOOST_REQUIRE( value );
90 BOOST_CHECK_EQUAL( std::get<0>( *value ), "10k" );
91
92 // A field with no explicit "visible" key must default to visible.
93 BOOST_CHECK_EQUAL( std::get<1>( *value ), true );
94
95 const HTTP_LIB_PART::field_type* footprint = findField( part, "footprint" );
96 BOOST_REQUIRE( footprint );
97 BOOST_CHECK_EQUAL( std::get<0>( *footprint ), "Resistor_SMD:R_0603_1608Metric" );
98 BOOST_CHECK_EQUAL( std::get<1>( *footprint ), false );
99}
100
101
104BOOST_AUTO_TEST_CASE( ListingRecordReportsNotDetailed )
105{
106 const nlohmann::json listing = R"({
107 "id": "1",
108 "name": "RC0603FR-0710KL",
109 "description": "10 kOhms resistor"
110 })"_json;
111
112 HTTP_LIB_PART part;
113
114 BOOST_CHECK( !setPartExtendedData( listing, part ) );
115 BOOST_CHECK( part.fields.empty() );
116}
117
118
121BOOST_AUTO_TEST_CASE( NativeBooleanFlagsAreAccepted )
122{
123 const nlohmann::json detail = R"({
124 "id": "1",
125 "symbolIdStr": "Device:R",
126 "exclude_from_bom": true,
127 "fields": {
128 "mpn": { "value": "RC0603FR-0710KL", "visible": false }
129 }
130 })"_json;
131
132 HTTP_LIB_PART part;
133
134 BOOST_CHECK_NO_THROW( setPartExtendedData( detail, part ) );
136
137 const HTTP_LIB_PART::field_type* mpn = findField( part, "mpn" );
138 BOOST_REQUIRE( mpn );
139 BOOST_CHECK_EQUAL( std::get<1>( *mpn ), false );
140}
141
142
const char * name
bool setPartExtendedData(const nlohmann::json &aPartJson, HTTP_LIB_PART &aPart)
Parse the exclusion flags and field content from a part's JSON record into aPart.
STL namespace.
std::string symbolIdStr
std::tuple< std::string, bool > field_type
Field content keyed by name, holding the text value and its schematic visibility.
std::vector< std::pair< std::string, field_type > > fields
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
static const HTTP_LIB_PART::field_type * findField(const HTTP_LIB_PART &aPart, const std::string &aName)
Issue #23023.
BOOST_AUTO_TEST_CASE(DetailRecordPopulatesFields)
A detail record with a fields object populates every field and reports itself as detailed.
BOOST_CHECK_EQUAL(result, "25.4")