Skip to content

Commit fbfad15

Browse files
Add comprehensive tests for PlotWidget to improve code coverage
Co-authored-by: highperformancecoder <3075825+highperformancecoder@users.noreply.github.com>
1 parent df6ab96 commit fbfad15

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed

test/testPlotWidget.cc

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,252 @@ namespace minsky
8686
// width change will be proportionate - just check the new height is about right
8787
EXPECT_NEAR(legendHeight+20,newLegendHeight,2);
8888
}
89+
90+
TEST_F(PlotWidgetTest, constructor)
91+
{
92+
// Test that constructor initializes with expected default values
93+
PlotWidget widget;
94+
EXPECT_EQ(150, widget.iWidth());
95+
EXPECT_EQ(150, widget.iHeight());
96+
EXPECT_EQ(10u, widget.nxTicks);
97+
EXPECT_EQ(10u, widget.nyTicks);
98+
EXPECT_EQ(2, widget.fontScale);
99+
EXPECT_TRUE(widget.leadingMarker);
100+
EXPECT_TRUE(widget.grid);
101+
EXPECT_EQ(0.1, widget.legendLeft);
102+
EXPECT_EQ(1u, widget.numLines());
103+
}
104+
105+
TEST_F(PlotWidgetTest, numLines)
106+
{
107+
// Test numLines getter/setter
108+
EXPECT_EQ(1u, numLines());
109+
110+
numLines(3);
111+
EXPECT_EQ(3u, numLines());
112+
113+
numLines(5);
114+
EXPECT_EQ(5u, numLines());
115+
116+
// Check that yvars and xvars are resized appropriately
117+
EXPECT_EQ(10u, yvars.size()); // 2*numLines
118+
EXPECT_EQ(5u, xvars.size());
119+
}
120+
121+
TEST_F(PlotWidgetTest, barWidth)
122+
{
123+
// Test barWidth getter/setter
124+
double width = barWidth(0.5);
125+
EXPECT_EQ(0.5, width);
126+
EXPECT_EQ(0.5, barWidth());
127+
128+
barWidth(1.0);
129+
EXPECT_EQ(1.0, barWidth());
130+
}
131+
132+
TEST_F(PlotWidgetTest, labels)
133+
{
134+
// Test xlabel, ylabel, y1label shadowed methods
135+
EXPECT_EQ("", xlabel());
136+
xlabel("X Axis");
137+
EXPECT_EQ("X Axis", xlabel());
138+
139+
EXPECT_EQ("", ylabel());
140+
ylabel("Y Axis");
141+
EXPECT_EQ("Y Axis", ylabel());
142+
143+
EXPECT_EQ("", y1label());
144+
y1label("Y1 Axis");
145+
EXPECT_EQ("Y1 Axis", y1label());
146+
}
147+
148+
TEST_F(PlotWidgetTest, title)
149+
{
150+
// Test title field
151+
EXPECT_EQ("", title);
152+
title = "Test Plot";
153+
EXPECT_EQ("Test Plot", title);
154+
}
155+
156+
TEST_F(PlotWidgetTest, resize)
157+
{
158+
// Test resize method with LassoBox
159+
LassoBox box;
160+
box.x0 = 0;
161+
box.y0 = 0;
162+
box.x1 = 300;
163+
box.y1 = 200;
164+
165+
resize(box);
166+
167+
EXPECT_EQ(300.0f, iWidth());
168+
EXPECT_EQ(200.0f, iHeight());
169+
EXPECT_EQ(150.0f, x()); // Center x = (0 + 300) / 2
170+
EXPECT_EQ(100.0f, y()); // Center y = (0 + 200) / 2
171+
}
172+
173+
TEST_F(PlotWidgetTest, clickTypeOnItem)
174+
{
175+
// Test clickType returns onItem when clicking inside the widget
176+
const double z = Item::zoomFactor();
177+
EXPECT_EQ(ClickType::onItem, clickType(0, 0)); // Center
178+
EXPECT_EQ(ClickType::onItem, clickType(10, 10));
179+
}
180+
181+
TEST_F(PlotWidgetTest, clickTypeOutside)
182+
{
183+
// Test clickType returns outside when clicking far outside
184+
const double z = Item::zoomFactor();
185+
const double w = 0.5 * iWidth() * z;
186+
const double h = 0.5 * iHeight() * z;
187+
EXPECT_EQ(ClickType::outside, clickType(x() + w + 100, y() + h + 100));
188+
}
189+
190+
TEST_F(PlotWidgetTest, disconnectAllVars)
191+
{
192+
// Test disconnectAllVars clears all variable connections
193+
auto var = std::make_shared<VariableValue>();
194+
var->init("test", 1.0);
195+
196+
connectVar(var, PlotWidget::nBoundsPorts); // Connect to first y port
197+
EXPECT_FALSE(yvars.empty());
198+
199+
disconnectAllVars();
200+
EXPECT_TRUE(xvars.empty());
201+
EXPECT_TRUE(yvars.empty());
202+
}
203+
204+
TEST_F(PlotWidgetTest, startPen)
205+
{
206+
// Test startPen calculation
207+
numLines(2);
208+
209+
// Add some variables to yvars
210+
auto var1 = std::make_shared<VariableValue>();
211+
var1->init("var1", 1.0);
212+
auto var2 = std::make_shared<VariableValue>();
213+
var2->init("var2", 2.0);
214+
215+
connectVar(var1, PlotWidget::nBoundsPorts); // port 6, first y port
216+
connectVar(var2, PlotWidget::nBoundsPorts); // port 6, first y port
217+
218+
// startPen(0) should be 0
219+
EXPECT_EQ(0u, startPen(0));
220+
// startPen(1) should be the number of vars in yvars[0]
221+
EXPECT_GE(startPen(1), 1u);
222+
}
223+
224+
TEST_F(PlotWidgetTest, connectVarBounds)
225+
{
226+
// Test connecting variables to bound ports
227+
auto xminVar = std::make_shared<VariableValue>();
228+
xminVar->init("xmin", 0.0);
229+
connectVar(xminVar, 0);
230+
231+
auto xmaxVar = std::make_shared<VariableValue>();
232+
xmaxVar->init("xmax", 10.0);
233+
connectVar(xmaxVar, 1);
234+
235+
auto yminVar = std::make_shared<VariableValue>();
236+
yminVar->init("ymin", -1.0);
237+
connectVar(yminVar, 2);
238+
239+
auto ymaxVar = std::make_shared<VariableValue>();
240+
ymaxVar->init("ymax", 1.0);
241+
connectVar(ymaxVar, 3);
242+
243+
auto y1minVar = std::make_shared<VariableValue>();
244+
y1minVar->init("y1min", -2.0);
245+
connectVar(y1minVar, 4);
246+
247+
auto y1maxVar = std::make_shared<VariableValue>();
248+
y1maxVar->init("y1max", 2.0);
249+
connectVar(y1maxVar, 5);
250+
251+
// Verify the variables are connected (can't directly test private members,
252+
// but we can verify the connection didn't crash)
253+
EXPECT_TRUE(true);
254+
}
255+
256+
TEST_F(PlotWidgetTest, plotType)
257+
{
258+
// Test plotType field
259+
EXPECT_EQ(PlotType::automatic, plotType);
260+
plotType = PlotType::line;
261+
EXPECT_EQ(PlotType::line, plotType);
262+
plotType = PlotType::bar;
263+
EXPECT_EQ(PlotType::bar, plotType);
264+
}
265+
266+
TEST_F(PlotWidgetTest, boundsValues)
267+
{
268+
// Test settable bounds values
269+
EXPECT_TRUE(std::isnan(xmin));
270+
EXPECT_TRUE(std::isnan(xmax));
271+
EXPECT_TRUE(std::isnan(ymin));
272+
EXPECT_TRUE(std::isnan(ymax));
273+
EXPECT_TRUE(std::isnan(y1min));
274+
EXPECT_TRUE(std::isnan(y1max));
275+
276+
xmin = 0.0;
277+
xmax = 100.0;
278+
ymin = -10.0;
279+
ymax = 10.0;
280+
y1min = -5.0;
281+
y1max = 5.0;
282+
283+
EXPECT_EQ(0.0, xmin);
284+
EXPECT_EQ(100.0, xmax);
285+
EXPECT_EQ(-10.0, ymin);
286+
EXPECT_EQ(10.0, ymax);
287+
EXPECT_EQ(-5.0, y1min);
288+
EXPECT_EQ(5.0, y1max);
289+
}
290+
291+
TEST_F(PlotWidgetTest, displaySettings)
292+
{
293+
// Test display settings
294+
EXPECT_EQ(3u, displayNTicks);
295+
EXPECT_EQ(3.0, displayFontSize);
296+
297+
displayNTicks = 5;
298+
displayFontSize = 4.5;
299+
300+
EXPECT_EQ(5u, displayNTicks);
301+
EXPECT_EQ(4.5, displayFontSize);
302+
}
303+
304+
TEST_F(PlotWidgetTest, markers)
305+
{
306+
// Test horizontal and vertical markers
307+
EXPECT_TRUE(horizontalMarkers.empty());
308+
EXPECT_TRUE(verticalMarkers.empty());
309+
310+
horizontalMarkers.push_back("marker1");
311+
verticalMarkers.push_back("marker2");
312+
313+
EXPECT_EQ(1u, horizontalMarkers.size());
314+
EXPECT_EQ(1u, verticalMarkers.size());
315+
EXPECT_EQ("marker1", horizontalMarkers[0]);
316+
EXPECT_EQ("marker2", verticalMarkers[0]);
317+
}
318+
319+
TEST_F(PlotWidgetTest, penLabels)
320+
{
321+
// Test labelPen functionality
322+
EXPECT_TRUE(penLabels.empty());
323+
324+
labelPen(0, "Pen 0");
325+
labelPen(1, "Pen 1");
326+
327+
EXPECT_EQ(2u, penLabels.size());
328+
EXPECT_EQ("Pen 0", penLabels[0]);
329+
EXPECT_EQ("Pen 1", penLabels[1]);
330+
331+
// Test that labelPen can expand the vector
332+
labelPen(5, "Pen 5");
333+
EXPECT_EQ(6u, penLabels.size());
334+
EXPECT_EQ("Pen 5", penLabels[5]);
335+
}
89336
}
90337
}

0 commit comments

Comments
 (0)