KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_commit.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
20#include <boost/test/unit_test.hpp>
21#include <commit.h>
22#include <undo_redo_container.h>
23#include <eda_item.h>
24
25// Minimal EDA_ITEM for testing
26namespace
27{
28class TEST_EDA_ITEM : public EDA_ITEM
29{
30public:
31 TEST_EDA_ITEM( KICAD_T aType ) : EDA_ITEM( aType ) {}
32
33 wxString GetClass() const override { return wxT( "TEST_EDA_ITEM" ); }
34
35 EDA_ITEM* Clone() const override { return new TEST_EDA_ITEM( Type() ); }
36};
37} // namespace
38
39
40// Simple COMMIT implementation for testing
41class TEST_COMMIT : public COMMIT
42{
43public:
44 void Push( const wxString&, int ) override {}
45 void Revert() override {}
46 void RevertToCheckpoint(int aCheckpoint) override {}
47 EDA_ITEM* ResolveItem( KIID& aID ) override { return nullptr; }
48
49protected:
50 EDA_ITEM* undoLevelItem( EDA_ITEM* aItem ) const override { return aItem; }
51 EDA_ITEM* makeImage( EDA_ITEM* aItem ) const override { return aItem->Clone(); }
52};
53
54
56
57
58BOOST_AUTO_TEST_CASE( StageAndStatus )
59{
60 TEST_COMMIT commit;
61 TEST_EDA_ITEM itemAdd( PCB_T );
62 TEST_EDA_ITEM itemRemove( PCB_T );
63 TEST_EDA_ITEM itemModify( PCB_T );
64
65 commit.Add( &itemAdd );
66 BOOST_CHECK_EQUAL( commit.GetStatus( &itemAdd ), CHT_ADD );
67
68 commit.Remove( &itemRemove );
69 BOOST_CHECK_EQUAL( commit.GetStatus( &itemRemove ), CHT_REMOVE );
70
71 commit.Modify( &itemModify );
72 TEST_EDA_ITEM* copy = static_cast<TEST_EDA_ITEM*>( itemModify.Clone() );
73 commit.Modified( &itemModify, copy );
74 BOOST_CHECK_EQUAL( commit.GetStatus( &itemModify ), CHT_MODIFY );
75}
76
77
78BOOST_AUTO_TEST_CASE( StageContainers )
79{
80 TEST_COMMIT commit;
81 TEST_EDA_ITEM a( PCB_T );
82 TEST_EDA_ITEM b( PCB_T );
83 std::vector<EDA_ITEM*> items = { &a, &b };
84
85 commit.Stage( items, CHT_ADD );
86
87 BOOST_CHECK_EQUAL( commit.GetStatus( &a ), CHT_ADD );
88 BOOST_CHECK_EQUAL( commit.GetStatus( &b ), CHT_ADD );
89}
90
91
92BOOST_AUTO_TEST_CASE( StagePickedItemsList )
93{
94 TEST_COMMIT commit;
95 TEST_EDA_ITEM newItem( PCB_T );
96 TEST_EDA_ITEM modItem( PCB_T );
97 TEST_EDA_ITEM* modCopy = static_cast<TEST_EDA_ITEM*>( modItem.Clone() );
98
100 ITEM_PICKER p1( nullptr, &newItem, UNDO_REDO::NEWITEM );
101 list.PushItem( p1 );
102
103 ITEM_PICKER p2( nullptr, &modItem, UNDO_REDO::CHANGED );
104 p2.SetLink( modCopy );
105 list.PushItem( p2 );
106
107 commit.Stage( list );
108
109 BOOST_CHECK_EQUAL( commit.GetStatus( &newItem ), CHT_ADD );
110 BOOST_CHECK_EQUAL( commit.GetStatus( &modItem ), CHT_MODIFY );
111}
112
113
114BOOST_AUTO_TEST_CASE( UnstageRemovesNewItem )
115{
116 TEST_COMMIT commit;
117 TEST_EDA_ITEM* item = new TEST_EDA_ITEM( PCB_T );
118 item->SetFlags( IS_NEW );
119
120 commit.Add( item );
121 commit.Unstage( item, nullptr );
122
123 BOOST_CHECK( commit.Empty() );
124}
125
126
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition commit.h:86
void Unstage(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition commit.cpp:177
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition commit.cpp:224
bool Empty() const
Definition commit.h:145
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
COMMIT()
Definition commit.cpp:30
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:235
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Add a change of the item aItem of type aChangeType to the change list.
Definition commit.cpp:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition eda_item.cpp:278
void SetLink(EDA_ITEM *aItem)
Definition kiid.h:46
A holder to handle information on schematic or board items.
void Revert() override
Revert the commit by restoring the modified items state.
void RevertToCheckpoint(int aCheckpoint) override
EDA_ITEM * makeImage(EDA_ITEM *aItem) const override
EDA_ITEM * undoLevelItem(EDA_ITEM *aItem) const override
void Push(const wxString &, int) override
Execute the changes.
EDA_ITEM * ResolveItem(KIID &aID) override
Search for an item in this commit that matches the provided KIID.
@ CHT_MODIFY
Definition commit.h:40
@ CHT_REMOVE
Definition commit.h:39
@ CHT_ADD
Definition commit.h:38
#define IS_NEW
New item, just created.
std::unique_ptr< typename std::remove_const< T >::type > Clone(const T &aItem)
Definition pns_item.h:348
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(StageAndStatus)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:70
@ PCB_T
Definition typeinfo.h:74