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 along
17 * with this program. If not, see <http://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// Simple COMMIT implementation for testing
37class TEST_COMMIT : public COMMIT
38{
39public:
40 void Push( const wxString&, int ) override {}
41 void Revert() override {}
42
43private:
44 EDA_ITEM* parentObject( EDA_ITEM* aItem ) const override { return aItem; }
45 EDA_ITEM* makeImage( EDA_ITEM* aItem ) const override { return aItem->Clone(); }
46};
47
49
50BOOST_AUTO_TEST_CASE( StageAndStatus )
51{
52 TEST_COMMIT commit;
53 TEST_EDA_ITEM itemAdd( PCB_T );
54 TEST_EDA_ITEM itemRemove( PCB_T );
55 TEST_EDA_ITEM itemModify( PCB_T );
56
57 commit.Add( &itemAdd );
58 BOOST_CHECK_EQUAL( commit.GetStatus( &itemAdd ), CHT_ADD );
59
60 commit.Remove( &itemRemove );
61 BOOST_CHECK_EQUAL( commit.GetStatus( &itemRemove ), CHT_REMOVE );
62
63 commit.Modify( &itemModify );
64 TEST_EDA_ITEM* copy = static_cast<TEST_EDA_ITEM*>( itemModify.Clone() );
65 commit.Modified( &itemModify, copy );
66 BOOST_CHECK_EQUAL( commit.GetStatus( &itemModify ), CHT_MODIFY );
67}
68
69BOOST_AUTO_TEST_CASE( StageContainers )
70{
71 TEST_COMMIT commit;
74 std::vector<EDA_ITEM*> items = { &a, &b };
75
76 commit.Stage( items, CHT_ADD );
77
78 BOOST_CHECK_EQUAL( commit.GetStatus( &a ), CHT_ADD );
79 BOOST_CHECK_EQUAL( commit.GetStatus( &b ), CHT_ADD );
80}
81
82BOOST_AUTO_TEST_CASE( StagePickedItemsList )
83{
84 TEST_COMMIT commit;
85 TEST_EDA_ITEM newItem( PCB_T );
86 TEST_EDA_ITEM modItem( PCB_T );
87 TEST_EDA_ITEM* modCopy = static_cast<TEST_EDA_ITEM*>( modItem.Clone() );
88
90 ITEM_PICKER p1( nullptr, &newItem, UNDO_REDO::NEWITEM );
91 list.PushItem( p1 );
92
93 ITEM_PICKER p2( nullptr, &modItem, UNDO_REDO::CHANGED );
94 p2.SetLink( modCopy );
95 list.PushItem( p2 );
96
97 commit.Stage( list );
98
99 BOOST_CHECK_EQUAL( commit.GetStatus( &newItem ), CHT_ADD );
100 BOOST_CHECK_EQUAL( commit.GetStatus( &modItem ), CHT_MODIFY );
101}
102
103BOOST_AUTO_TEST_CASE( UnstageRemovesNewItem )
104{
105 TEST_COMMIT commit;
106 TEST_EDA_ITEM* item = new TEST_EDA_ITEM( PCB_T );
107 item->SetFlags( IS_NEW );
108
109 commit.Add( item );
110 commit.Unstage( item, nullptr );
111
112 BOOST_CHECK( commit.Empty() );
113}
114
116
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:73
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition: commit.h:91
void Unstage(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition: commit.cpp:122
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.h:118
bool Empty() const
Definition: commit.h:152
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:107
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition: commit.h:79
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition: commit.cpp:142
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:46
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:142
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:118
void SetLink(EDA_ITEM *aItem)
A holder to handle information on schematic or board items.
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
void Revert() override
Revert the commit by restoring the modified items state.
Definition: test_commit.cpp:41
EDA_ITEM * parentObject(EDA_ITEM *aItem) const override
Definition: test_commit.cpp:44
EDA_ITEM * makeImage(EDA_ITEM *aItem) const override
Definition: test_commit.cpp:45
void Push(const wxString &, int) override
Execute the changes.
Definition: test_commit.cpp:40
TEST_EDA_ITEM(KICAD_T aType)
Definition: test_commit.cpp:29
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: test_commit.cpp:33
wxString GetClass() const override
Return the class name.
Definition: test_commit.cpp:31
@ CHT_MODIFY
Definition: commit.h:45
@ CHT_REMOVE
Definition: commit.h:44
@ CHT_ADD
Definition: commit.h:43
#define IS_NEW
New item, just created.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK_EQUAL(ret, c.m_exp_result)
BOOST_AUTO_TEST_CASE(StageAndStatus)
Definition: test_commit.cpp:50
BOOST_AUTO_TEST_SUITE_END()
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_T
Definition: typeinfo.h:82