Skip to content

Commit d4af155

Browse files
committed
feat: add tests for PieChart sectionOffset
Signed-off-by: Jonas Klock <jonas.klock@exxeta.com>
1 parent 8336cc6 commit d4af155

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

test/chart/pie_chart/pie_chart_data_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,47 @@ void main() {
274274
expect(mid.toRadius, 50);
275275
});
276276
});
277+
278+
group('PieChartSectionData', () {
279+
test('equality', () {
280+
final a =
281+
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 8);
282+
final b =
283+
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 8);
284+
expect(a == b, true);
285+
286+
expect(
287+
a ==
288+
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 0),
289+
false,
290+
);
291+
});
292+
293+
test('copyWith', () {
294+
final original = PieChartSectionData(value: 10, color: Colors.red);
295+
expect(original.sectionOffset, 0);
296+
297+
final withOffset = original.copyWith(sectionOffset: 12);
298+
expect(withOffset.sectionOffset, 12);
299+
expect(withOffset.value, 10);
300+
301+
expect(original.copyWith() == original, true);
302+
});
303+
304+
test('lerp', () {
305+
final a =
306+
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 0);
307+
final b =
308+
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 20);
309+
310+
final atZero = PieChartSectionData.lerp(a, b, 0);
311+
expect(atZero.sectionOffset, 0);
312+
313+
final atOne = PieChartSectionData.lerp(a, b, 1);
314+
expect(atOne.sectionOffset, 20);
315+
316+
final mid = PieChartSectionData.lerp(a, b, 0.5);
317+
expect(mid.sectionOffset, 10);
318+
});
319+
});
277320
}

test/chart/pie_chart/pie_chart_painter_test.dart

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,69 @@ void main() {
369369
);
370370
expect(results[3]['paint_style'] as PaintingStyle, PaintingStyle.fill);
371371
});
372+
373+
test('test 3 - sectionOffset translates section along center angle', () {
374+
const viewSize = Size(200, 200);
375+
const centerRadius = 10.0;
376+
const offset = 20.0;
377+
378+
final section0 = PieChartSectionData(
379+
color: MockData.color1,
380+
value: 1,
381+
sectionOffset: offset,
382+
);
383+
final section1 = PieChartSectionData(
384+
color: MockData.color2,
385+
value: 1,
386+
);
387+
final data = PieChartData(
388+
sectionsSpace: 0,
389+
sections: [section0, section1],
390+
);
391+
392+
final pieChartPainter = PieChartPainter();
393+
final holder =
394+
PaintHolder<PieChartData>(data, data, TextScaler.noScaling);
395+
396+
final mockCanvasWrapper = MockCanvasWrapper();
397+
when(mockCanvasWrapper.size).thenAnswer((_) => viewSize);
398+
when(mockCanvasWrapper.canvas).thenReturn(MockCanvas());
399+
final drawnPaths = <Path>[];
400+
when(mockCanvasWrapper.drawPath(captureAny, captureAny)).thenAnswer(
401+
(inv) => drawnPaths.add(inv.positionalArguments[0] as Path),
402+
);
403+
404+
pieChartPainter.drawSections(
405+
mockCanvasWrapper,
406+
[180, 180],
407+
centerRadius,
408+
holder,
409+
);
410+
411+
expect(drawnPaths.length, 2);
412+
413+
const section0Center = Offset(100, 120);
414+
final expectedPath0 = pieChartPainter.generateSectionPath(
415+
section0,
416+
0,
417+
0,
418+
180,
419+
section0Center,
420+
centerRadius,
421+
);
422+
expect(HelperMethods.equalsPaths(drawnPaths[0], expectedPath0), true);
423+
424+
const section1Center = Offset(100, 100);
425+
final expectedPath1 = pieChartPainter.generateSectionPath(
426+
section1,
427+
0,
428+
180,
429+
180,
430+
section1Center,
431+
centerRadius,
432+
);
433+
expect(HelperMethods.equalsPaths(drawnPaths[1], expectedPath1), true);
434+
});
372435
});
373436

374437
group('generateSectionPath()', () {

0 commit comments

Comments
 (0)