Added unit tests.

This commit is contained in:
Matthias Koefferlein
2020-02-19 01:06:39 +01:00
parent 5853fecf37
commit 7913e7cf82
7 changed files with 125 additions and 28 deletions
+9 -2
View File
@@ -86,7 +86,14 @@ static std::map<std::string, bool> unpack_menu_items_hidden (const std::string &
return kb;
}
static lay::AbstractMenu *new_menu ()
{
return new lay::AbstractMenu (0);
}
Class<lay::AbstractMenu> decl_AbstractMenu ("lay", "AbstractMenu",
// for test purposes mainly:
constructor ("new", &new_menu, "@hide") +
method ("pack_key_binding", &pack_key_binding, gsi::arg ("path_to_keys"),
"@brief Serializes a key binding definition into a single string\n"
"The serialized format is used by the 'key-bindings' config key. "
@@ -119,8 +126,8 @@ Class<lay::AbstractMenu> decl_AbstractMenu ("lay", "AbstractMenu",
"@brief Get the reference to a Action object associated with the given path\n"
"@args path\n"
"\n"
"@param path The path to the item. This must be a valid path.\n"
"@return A reference to a Action object associated with this path\n"
"@param path The path to the item.\n"
"@return A reference to a Action object associated with this path or nil if the path is not valid\n"
) +
method ("items", &lay::AbstractMenu::items,
"@brief Get the subitems for a given submenu\n"
+2 -5
View File
@@ -240,7 +240,7 @@ AbstractMenuItem::set_action (Action *a, bool copy_properties)
{
tl_assert (a != 0);
a->keep_object ();
a->keep ();
if (copy_properties && mp_action->qaction () && a->qaction ()) {
a->qaction ()->setIcon (mp_action->qaction ()->icon ());
@@ -1192,10 +1192,7 @@ const Action *AbstractMenu::action(const std::string &path) const
Action *AbstractMenu::action(const std::string &path)
{
AbstractMenuItem *item = find_item_exact (path);
if (! item) {
throw tl::Exception (tl::to_string (QObject::tr ("Not a valid menu item path: ")) + path);
}
return item->action ();
return item ? item->action () : 0;
}
std::vector<std::string>
+2 -1
View File
@@ -86,7 +86,8 @@ LAYBASIC_PUBLIC std::string pack_menu_items_hidden (const std::vector<std::pair<
*/
class LAYBASIC_PUBLIC Action
: public QObject,
public tl::Object
public tl::Object,
public gsi::ObjectBase
{
Q_OBJECT
@@ -135,3 +135,57 @@ TEST(1)
menu.clear_menu ("n1");
EXPECT_EQ (menu_to_string (menu), "(n2,n1)");
}
TEST(2_ActionReferences)
{
tl::weak_ptr<lay::Action> action (new lay::Action ("title:n1"));
{
lay::AbstractMenu menu (0);
EXPECT_EQ (menu_to_string (menu), "");
EXPECT_EQ (menu.action ("s1.n1") == 0, true);
EXPECT_EQ (menu.action ("s1") == 0, true);
menu.insert_menu ("end", "s1", "submenu1");
menu.insert_menu ("end", "s2", "submenu2");
menu.insert_item ("s1.end", "n1", action.get ());
menu.insert_item ("s2.end", "n1", action.get ());
EXPECT_EQ (menu_to_string (menu), "(s1(s1.n1),s2(s2.n1))");
EXPECT_EQ (menu.action ("s1.n1") == action.get (), true);
EXPECT_EQ (menu.action ("s2.n1") == action.get (), true);
}
// the action is deleted because it's owned by the menu
EXPECT_EQ (action.get () == 0, true);
}
TEST(3_ActionReferences)
{
tl::weak_ptr<lay::Action> action (new lay::Action ("title:n1"));
{
lay::AbstractMenu menu (0);
EXPECT_EQ (menu_to_string (menu), "");
EXPECT_EQ (menu.action ("s1.n1") == 0, true);
EXPECT_EQ (menu.action ("s1") == 0, true);
menu.insert_menu ("end", "s1", "submenu1");
menu.insert_menu ("end", "s2", "submenu2");
menu.insert_item ("s1.end", "n1", action.get ());
menu.insert_item ("s2.end", "n1", action.get ());
EXPECT_EQ (menu_to_string (menu), "(s1(s1.n1),s2(s2.n1))");
menu.delete_item ("s2");
EXPECT_EQ (menu.action ("s1.n1") != 0, true);
EXPECT_EQ (menu.action ("s1.n1") == action.get (), true);
EXPECT_EQ (menu.action ("s2.n1") == 0, true);
}
// the action is deleted because it's owned by the menu
EXPECT_EQ (action.get () == 0, true);
}
+2 -2
View File
@@ -14,9 +14,9 @@ SOURCES = \
layParsedLayerSource.cc \
layRenderer.cc \
laySnap.cc \
layAbstractMenu.cc \
layNetlistBrowserModelTests.cc \
layNetlistBrowserTreeModelTests.cc
layNetlistBrowserTreeModelTests.cc \
layAbstractMenuTests.cc
INCLUDEPATH += $$TL_INC $$LAYBASIC_INC $$DB_INC $$GSI_INC $$OUT_PWD/../laybasic
DEPENDPATH += $$TL_INC $$LAYBASIC_INC $$DB_INC $$GSI_INC $$OUT_PWD/../laybasic