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 <json_common.h>
41
42
43BOOST_AUTO_TEST_SUITE( Issue23023HttpFields )
44
45
46static const HTTP_LIB_PART::field_type* findField( const HTTP_LIB_PART& aPart, const std::string& aName )
47{
48 for( const auto& [name, properties] : aPart.fields )
49 {
50 if( name == aName )
51 return &properties;
52 }
53
54 return nullptr;
55}
56
57
59BOOST_AUTO_TEST_CASE( DetailRecordPopulatesFields )
60{
61 const nlohmann::json detail = R"({
62 "id": "1",
63 "name": "RC0603FR-0710KL",
64 "symbolIdStr": "Device:R",
65 "exclude_from_bom": "False",
66 "exclude_from_board": "False",
67 "exclude_from_sim": "True",
68 "fields": {
69 "footprint": { "value": "Resistor_SMD:R_0603_1608Metric", "visible": "False" },
70 "datasheet": { "value": "www.kicad.org", "visible": "False" },
71 "value": { "value": "10k" },
72 "reference": { "value": "R" }
73 }
74 })"_json;
75
76 HTTP_LIB_PART part;
77
78 BOOST_CHECK( setPartExtendedData( detail, part ) );
79
80 BOOST_CHECK_EQUAL( part.symbolIdStr, "Device:R" );
83
84 BOOST_REQUIRE_EQUAL( part.fields.size(), 4u );
85
86 const HTTP_LIB_PART::field_type* value = findField( part, "value" );
87 BOOST_REQUIRE( value );
88 BOOST_CHECK_EQUAL( std::get<0>( *value ), "10k" );
89
90 // A field with no explicit "visible" key must default to visible.
91 BOOST_CHECK_EQUAL( std::get<1>( *value ), true );
92
93 const HTTP_LIB_PART::field_type* footprint = findField( part, "footprint" );
94 BOOST_REQUIRE( footprint );
95 BOOST_CHECK_EQUAL( std::get<0>( *footprint ), "Resistor_SMD:R_0603_1608Metric" );
96 BOOST_CHECK_EQUAL( std::get<1>( *footprint ), false );
97}
98
99
102BOOST_AUTO_TEST_CASE( ListingRecordReportsNotDetailed )
103{
104 const nlohmann::json listing = R"({
105 "id": "1",
106 "name": "RC0603FR-0710KL",
107 "description": "10 kOhms resistor"
108 })"_json;
109
110 HTTP_LIB_PART part;
111
112 BOOST_CHECK( !setPartExtendedData( listing, part ) );
113 BOOST_CHECK( part.fields.empty() );
114}
115
116
119BOOST_AUTO_TEST_CASE( NativeBooleanFlagsAreAccepted )
120{
121 const nlohmann::json detail = R"({
122 "id": "1",
123 "symbolIdStr": "Device:R",
124 "exclude_from_bom": true,
125 "fields": {
126 "mpn": { "value": "RC0603FR-0710KL", "visible": false }
127 }
128 })"_json;
129
130 HTTP_LIB_PART part;
131
132 BOOST_CHECK_NO_THROW( setPartExtendedData( detail, part ) );
134
135 const HTTP_LIB_PART::field_type* mpn = findField( part, "mpn" );
136 BOOST_REQUIRE( mpn );
137 BOOST_CHECK_EQUAL( std::get<1>( *mpn ), false );
138}
139
140
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")