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
26class TEST_EDA_ITEM : public EDA_ITEM
27{
28public:
29 TEST_EDA_ITEM( KICAD_T aType ) : EDA_ITEM( aType ) {}
30
31 wxString GetClass() const override { return wxT( "TEST_EDA_ITEM" ); }
32
33 EDA_ITEM* Clone() const override { return new TEST_EDA_ITEM( Type() ); }
34};
35
36
37// Simple COMMIT implementation for testing
38class TEST_COMMIT : public COMMIT
39{
40public:
41 void Push( const wxString&, int ) override {}
42 void Revert() override {}
43 void RevertToCheckpoint(int aCheckpoint) override {}
44 EDA_ITEM* ResolveItem( KIID& aID ) override { return nullptr; }
45
46protected:
47 EDA_ITEM* undoLevelItem( EDA_ITEM* aItem ) const override { return aItem; }
48 EDA_ITEM* makeImage( EDA_ITEM* aItem ) const override { return aItem->Clone(); }
49};
50
51
53
54
55BOOST_AUTO_TEST_CASE( StageAndStatus )
56{
57 TEST_COMMIT commit;
58 TEST_EDA_ITEM itemAdd( PCB_T );
59 TEST_EDA_ITEM itemRemove( PCB_T );
60 TEST_EDA_ITEM itemModify( PCB_T );
61
62 commit.Add( &itemAdd );
63 BOOST_CHECK_EQUAL( commit.GetStatus( &itemAdd ), CHT_ADD );
64
65 commit.Remove( &itemRemove );
66 BOOST_CHECK_EQUAL( commit.GetStatus( &itemRemove ), CHT_REMOVE );
67
68 commit.Modify( &itemModify );
69 TEST_EDA_ITEM* copy = static_cast<TEST_EDA_ITEM*>( itemModify.Clone() );
70 commit.Modified( &itemModify, copy );
71 BOOST_CHECK_EQUAL( commit.GetStatus( &itemModify ), CHT_MODIFY );
72}
73
74
75BOOST_AUTO_TEST_CASE( StageContainers )
76{
77 TEST_COMMIT commit;
80 std::vector<EDA_ITEM*> items = { &a, &b };
81
82 commit.Stage( items, CHT_ADD );
83
84 BOOST_CHECK_EQUAL( commit.GetStatus( &a ), CHT_ADD );
85 BOOST_CHECK_EQUAL( commit.GetStatus( &b ), CHT_ADD );
86}
87
88
89BOOST_AUTO_TEST_CASE( StagePickedItemsList )
90{
91 TEST_COMMIT commit;
92 TEST_EDA_ITEM newItem( PCB_T );
93 TEST_EDA_ITEM modItem( PCB_T );
94 TEST_EDA_ITEM* modCopy = static_cast<TEST_EDA_ITEM*>( modItem.Clone() );
95
97 ITEM_PICKER p1( nullptr, &newItem, UNDO_REDO::NEWITEM );
98 list.PushItem( p1 );
99
100 ITEM_PICKER p2( nullptr, &modItem, UNDO_REDO::CHANGED );
101 p2.SetLink( modCopy );
102 list.PushItem( p2 );
103
104 commit.Stage( list );
105
106 BOOST_CHECK_EQUAL( commit.GetStatus( &newItem ), CHT_ADD );
107 BOOST_CHECK_EQUAL( commit.GetStatus( &modItem ), CHT_MODIFY );
108}
109
110
111BOOST_AUTO_TEST_CASE( UnstageRemovesNewItem )
112{
113 TEST_COMMIT commit;
114 TEST_EDA_ITEM* item = new TEST_EDA_ITEM( PCB_T );
115 item->SetFlags( IS_NEW );
116
117 commit.Add( item );
118 commit.Unstage( item, nullptr );
119
120 BOOST_CHECK( commit.Empty() );
121}
122
123
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
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:158
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition eda_item.cpp:278
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:84
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.
TEST_EDA_ITEM(KICAD_T aType)
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
wxString GetClass() const override
Return the class name.
@ 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.
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