Changeset - r28083:8503c58941bd
[Not reviewed]
master
0 2 0
Peter Nelson - 13 months ago 2023-11-02 23:25:28
peter1138@openttd.org
Codechange: Add unit-test to check if nested widget parts of properly closed.

Properly closed means exactly one EndContainer for every Container widget.
2 files changed with 27 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/tests/test_window_desc.cpp
Show inline comments
 
@@ -47,3 +47,29 @@ TEST_CASE("WindowDesc - ini_key validity
 

	
 
	CHECK((has_widget == has_inikey));
 
}
 

	
 
/**
 
 * Test if a NWidgetTree is properly closed, meaning the number of container-type parts matches the number of
 
 * EndContainer() parts.
 
 * @param nwid_begin Pointer to beginning of nested widget parts.
 
 * @param nwid_end Pointer to ending of nested widget parts.
 
 * @return True iff nested tree is properly closed.
 
 */
 
static bool IsNWidgetTreeClosed(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end)
 
{
 
	int depth = 0;
 
	for (; nwid_begin < nwid_end; ++nwid_begin) {
 
		if (IsContainerWidgetType(nwid_begin->type)) ++depth;
 
		if (nwid_begin->type == WPT_ENDCONTAINER) --depth;
 
	}
 
	return depth == 0;
 
}
 

	
 
TEST_CASE("WindowDesc - NWidgetParts properly closed")
 
{
 
	const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs)));
 

	
 
	INFO(fmt::format("{}:{}", window_desc->file, window_desc->line));
 

	
 
	CHECK(IsNWidgetTreeClosed(window_desc->nwid_begin, window_desc->nwid_end));
 
}
src/widget_type.h
Show inline comments
 
@@ -1294,6 +1294,7 @@ static inline NWidgetPart NWidgetFunctio
 
	return part;
 
}
 

	
 
bool IsContainerWidgetType(WidgetType tp);
 
NWidgetContainer *MakeNWidgets(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, int *biggest_index, NWidgetContainer *container);
 
NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, int *biggest_index, NWidgetStacked **shade_select);
 

	
0 comments (0 inline, 0 general)