kicad/qa/tests/eeschema/lib_field_test_utils.h

118 lines
3.4 KiB
C
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019 KiCad Developers, see AUTHORS.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file
2024-04-12 21:00:41 +00:00
* Test utils (e.g. print helpers and test predicates for SCH_FIELD objects
*/
#ifndef QA_EESCHEMA_LIB_FIELD_TEST_UTILS__H
#define QA_EESCHEMA_LIB_FIELD_TEST_UTILS__H
2021-02-06 22:20:31 +00:00
#include <qa_utils/wx_utils/unit_test_utils.h>
#include <template_fieldnames.h>
namespace BOOST_TEST_PRINT_NAMESPACE_OPEN
{
template <>
2024-04-12 21:00:41 +00:00
struct print_log_value<SCH_FIELD>
{
2024-04-12 21:00:41 +00:00
inline void operator()( std::ostream& os, SCH_FIELD const& f )
{
2024-04-12 21:00:41 +00:00
os << "SCH_FIELD[ " << f.GetCanonicalName() << " ]";
}
};
template <>
2024-04-12 21:00:41 +00:00
struct print_log_value<std::vector<SCH_FIELD>>
{
2024-04-12 21:00:41 +00:00
inline void operator()( std::ostream& os, std::vector<SCH_FIELD> const& f )
{
2024-04-12 21:00:41 +00:00
os << "SCH_FIELDS[ " << f.size() << " ]";
}
};
}
BOOST_TEST_PRINT_NAMESPACE_CLOSE
namespace KI_TEST
{
/**
* Predicate to check a field name is as expected
2024-04-12 21:00:41 +00:00
* @param aField SCH_FIELD to check the name
* @param aExpectedName the expected field name
* @param aExpectedId the expected field id
2024-04-12 21:00:41 +00:00
* @return true if match
*/
2024-04-12 21:00:41 +00:00
bool FieldNameIdMatches( const SCH_FIELD& aField, const std::string& aExpectedName, int aExpectedId )
{
bool ok = true;
const auto gotName = aField.GetCanonicalName();
if( gotName != aExpectedName )
{
BOOST_TEST_INFO( "Field name: got '" << gotName << "', expected '" << aExpectedName );
ok = false;
}
const int gotId = aField.GetId();
if( gotId != aExpectedId )
{
BOOST_TEST_INFO( "Field ID: got '" << gotId << "', expected '" << aExpectedId );
ok = false;
}
return ok;
}
/**
2024-04-12 21:00:41 +00:00
* Predicate to check that the mandatory fields look sensible
*/
2024-04-12 21:00:41 +00:00
bool AreDefaultFieldsCorrect( const std::vector<SCH_FIELD>& aFields )
{
const unsigned expectedCount = MANDATORY_FIELD_T::MANDATORY_FIELDS;
2024-04-12 21:00:41 +00:00
if( aFields.size() < expectedCount )
{
2024-04-12 21:00:41 +00:00
BOOST_TEST_INFO( "Expected at least " << expectedCount << " fields, got " << aFields.size() );
return false;
}
bool ok = true;
ok &= FieldNameIdMatches( aFields[0], "Reference", MANDATORY_FIELD_T::REFERENCE_FIELD );
ok &= FieldNameIdMatches( aFields[1], "Value", MANDATORY_FIELD_T::VALUE_FIELD );
ok &= FieldNameIdMatches( aFields[2], "Footprint", MANDATORY_FIELD_T::FOOTPRINT_FIELD );
ok &= FieldNameIdMatches( aFields[3], "Datasheet", MANDATORY_FIELD_T::DATASHEET_FIELD );
return ok;
}
} // namespace KI_TEST
2021-02-06 22:20:31 +00:00
#endif // QA_EESCHEMA_LIB_FIELD_TEST_UTILS__H