You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2020 KiCad Developers, see CHANGELOG.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 2
  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, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file
  25. * Test suite for #SCH_SHEET_PATH and #SCH_SHEET_LIST
  26. */
  27. #include <unit_test_utils/unit_test_utils.h>
  28. // Code under test
  29. #include <sch_sheet_path.h>
  30. #include "uuid_test_utils.h"
  31. #include <sch_sheet.h>
  32. #include <sstream>
  33. class TEST_SCH_SHEET_PATH_FIXTURE
  34. {
  35. public:
  36. TEST_SCH_SHEET_PATH_FIXTURE()
  37. {
  38. for( unsigned i = 0; i < 4; ++i )
  39. {
  40. m_sheets.emplace_back( nullptr, wxPoint( i, i ) );
  41. std::ostringstream ss;
  42. ss << "Sheet" << i;
  43. m_sheets[i].GetFields()[SHEETNAME].SetText( ss.str() );
  44. }
  45. // 0->1->2
  46. m_linear.push_back( &m_sheets[0] );
  47. m_linear.push_back( &m_sheets[1] );
  48. m_linear.push_back( &m_sheets[2] );
  49. }
  50. SCH_SHEET_PATH m_empty_path;
  51. /**
  52. * We look at sheet 2 in the hierarchy:
  53. * Sheets: 0 -> 1 -> 2
  54. */
  55. SCH_SHEET_PATH m_linear;
  56. /// handy store of SCH_SHEET objects
  57. std::vector<SCH_SHEET> m_sheets;
  58. };
  59. /**
  60. * Declare the test suite
  61. */
  62. BOOST_FIXTURE_TEST_SUITE( SchSheetPath, TEST_SCH_SHEET_PATH_FIXTURE )
  63. /**
  64. * Check properties of an empty SCH_SHEET_PATH
  65. */
  66. BOOST_AUTO_TEST_CASE( Empty )
  67. {
  68. BOOST_CHECK_EQUAL( m_empty_path.size(), 0 );
  69. BOOST_CHECK_THROW( m_empty_path.at( 0 ), std::out_of_range );
  70. // Sheet paths with no SCH_SCHEET object are illegal.
  71. CHECK_WX_ASSERT( m_empty_path.GetPageNumber() );
  72. // These accessors return nullptr when empty (i.e. they don't crash)
  73. BOOST_CHECK_EQUAL( m_empty_path.Last(), nullptr );
  74. BOOST_CHECK_EQUAL( m_empty_path.LastScreen(), nullptr );
  75. BOOST_CHECK_EQUAL( m_empty_path.PathAsString(), "/" );
  76. BOOST_CHECK_EQUAL( m_empty_path.PathHumanReadable(), "/" );
  77. }
  78. /**
  79. * Check properties of a non-empty SCH_SHEET_PATH
  80. */
  81. BOOST_AUTO_TEST_CASE( NonEmpty )
  82. {
  83. BOOST_CHECK_EQUAL( m_linear.size(), 3 );
  84. BOOST_CHECK_EQUAL( m_linear.at( 0 ), &m_sheets[0] );
  85. BOOST_CHECK_EQUAL( m_linear.at( 1 ), &m_sheets[1] );
  86. BOOST_CHECK_EQUAL( m_linear.at( 2 ), &m_sheets[2] );
  87. BOOST_CHECK_EQUAL( m_linear.Last(), &m_sheets[2] );
  88. BOOST_CHECK_EQUAL( m_linear.LastScreen(), nullptr );
  89. // don't know what the uuids will be, but we know the format: /<8-4-4-4-12>/<8-4-4-4-12>/
  90. BOOST_CHECK_PREDICATE(
  91. KI_TEST::IsUUIDPathWithLevels, ( m_linear.PathAsString().ToStdString() )( 2 ) );
  92. // Sheet0 is the root sheet and isn't in the path
  93. BOOST_CHECK_EQUAL( m_linear.PathHumanReadable(), "/Sheet1/Sheet2/" );
  94. }
  95. BOOST_AUTO_TEST_CASE( Compare )
  96. {
  97. SCH_SHEET_PATH otherEmpty;
  98. BOOST_CHECK( m_empty_path == otherEmpty );
  99. BOOST_CHECK( m_empty_path != m_linear );
  100. }
  101. /**
  102. * Test sheet path page number properties.
  103. */
  104. BOOST_AUTO_TEST_CASE( SheetPathPageProperties )
  105. {
  106. BOOST_CHECK_EQUAL( m_linear.GetPageNumber(), wxEmptyString );
  107. // Add new instance to sheet object.
  108. BOOST_CHECK( m_linear.Last()->AddInstance( m_linear.Path() ) );
  109. m_linear.SetPageNumber( "1" );
  110. BOOST_CHECK_EQUAL( m_linear.GetPageNumber(), "1" );
  111. m_linear.SetPageNumber( "i" );
  112. BOOST_CHECK_EQUAL( m_linear.GetPageNumber(), "i" );
  113. }
  114. BOOST_AUTO_TEST_SUITE_END()