20#include <boost/test/unit_test.hpp>
27#if defined( __linux__ ) && defined( __GLIBCXX__ )
30#define KI_COUNT_THROWS 1
38std::atomic<bool> g_countThrows{
false };
39std::atomic<int> g_throwCount{ 0 };
52 g_throwCount.store( 0, std::memory_order_relaxed );
53 g_countThrows.store(
true, std::memory_order_relaxed );
60 g_countThrows.store(
false, std::memory_order_relaxed );
68 return g_throwCount.load( std::memory_order_relaxed );
74 static bool Supported()
85wxFileName schemaPath(
const wxString& aName )
87 wxFileName schemaFile = wxFileName::DirName( wxString::FromUTF8( QA_SRC_ROOT ) );
88 schemaFile.AppendDir( wxS(
"kicad" ) );
89 schemaFile.AppendDir( wxS(
"pcm" ) );
90 schemaFile.AppendDir( wxS(
"schemas" ) );
91 schemaFile.SetFullName( aName );
96nlohmann::json validPackage()
98 return nlohmann::json{
99 {
"name",
"Test Package" },
100 {
"description",
"short description" },
101 {
"description_full",
"a longer description" },
102 {
"identifier",
"com.example.test" },
103 {
"type",
"plugin" },
104 {
"author", { {
"name",
"Someone" },
105 {
"contact", { {
"web",
"https://example.test" } } } } },
106 {
"license",
"MIT" },
107 {
"resources", { {
"homepage",
"https://example.test" } } },
108 {
"versions", nlohmann::json::array( { { {
"version",
"1.0" },
109 {
"status",
"stable" },
110 {
"kicad_version",
"6.0" } } } ) }
117#ifdef KI_COUNT_THROWS
124extern "C" void __cxa_throw(
void* aException, std::type_info* aTypeInfo,
125 void ( *aDestructor )(
void* ) )
127 if( g_countThrows.load( std::memory_order_relaxed ) )
128 g_throwCount.fetch_add( 1, std::memory_order_relaxed );
130 using THROW_FN = void ( * )(
void*, std::type_info*, void ( * )(
void* ) );
131 static THROW_FN real =
reinterpret_cast<THROW_FN
>( dlsym( RTLD_NEXT,
"__cxa_throw" ) );
133 real( aException, aTypeInfo, aDestructor );
134 __builtin_unreachable();
148 for(
const wxString&
name : { wxS(
"pcm.v1.schema.json" ), wxS(
"pcm.v2.schema.json" ) } )
150 wxFileName schema = schemaPath(
name );
151 BOOST_REQUIRE_MESSAGE( schema.FileExists(), schema.GetFullPath() );
153 THROW_COUNTER counter;
156 if( THROW_COUNTER::Supported() )
158 BOOST_CHECK_MESSAGE( counter.Count() == 0,
159 name <<
" raised " << counter.Count()
160 <<
" exception(s) while building the schema" );
172 nlohmann::json_uri uri(
"#/definitions/Package" );
175 validator.Validate( validPackage(), good, uri );
179 nlohmann::json bad = validPackage();
180 bad[
"author"].erase(
"contact" );
183 validator.Validate( bad, missingContact, uri );
184 BOOST_CHECK( missingContact.
HasErrors() );
194 nlohmann::json schema = nlohmann::json::parse( R
"({
195 "$schema": "http://json-schema.org/draft-07/schema#",
196 "$defs": { "Modern": { "type": "integer" } },
197 "definitions": { "Legacy": { "type": "integer" } }
203 for(
const std::string& fragment : {
"#/$defs/Modern",
"#/definitions/Legacy" } )
205 nlohmann::json_uri uri( fragment );
209 BOOST_CHECK_MESSAGE( !resolved.
HasErrors(),
210 fragment <<
" did not resolve: " << resolved.
FirstError() );
213 validator.validate(
"not an integer", enforced, uri );
214 BOOST_CHECK_MESSAGE( enforced.
HasErrors(), fragment <<
" did not enforce its type" );
Collects JSON-schema validation errors so the caller can inspect them after a validation pass.
wxString FirstError() const
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
nlohmann::json_schema::json_validator validator
BOOST_AUTO_TEST_CASE(PcmSchemasLoadWithoutThrowing)
CreatePCM() builds these on every project open, where a throw is fatal with the CLR loaded.