Skip to content

Commit 0f3e3a4

Browse files
committed
test: Add a few more unit tests
1 parent aa800e2 commit 0f3e3a4

File tree

4 files changed

+219
-13
lines changed

4 files changed

+219
-13
lines changed

test/flutter_html_test.dart

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_html/flutter_html.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
testWidgets(
7+
"Check that widget does not fail on empty data",
8+
(tester) async {
9+
await tester.pumpWidget(
10+
MaterialApp(
11+
home: Html(
12+
data: "",
13+
),
14+
),
15+
);
16+
expect(find.text('', findRichText: true), findsOneWidget);
17+
},
18+
);
19+
20+
testWidgets(
21+
"Check that selectable widget does not fail on empty data",
22+
(tester) async {
23+
await tester.pumpWidget(
24+
MaterialApp(
25+
home: SelectableHtml(
26+
data: '',
27+
),
28+
),
29+
);
30+
expect(find.text('', findRichText: true), findsOneWidget);
31+
},
32+
);
33+
34+
testWidgets(
35+
"Check that widget displays given text",
36+
(tester) async {
37+
await tester.pumpWidget(
38+
MaterialApp(
39+
home: Html(
40+
data: "Text",
41+
),
42+
),
43+
);
44+
expect(find.text('Text', findRichText: true), findsOneWidget);
45+
},
46+
);
47+
48+
testWidgets('Check that a simple element is displayed', (tester) async {
49+
await tester.pumpWidget(
50+
MaterialApp(
51+
home: Html(
52+
data: "<p>Text</p>",
53+
),
54+
),
55+
);
56+
expect(find.text('Text', findRichText: true), findsOneWidget);
57+
});
58+
59+
testWidgets('Check that a simple element is hidden when tagsList does not contain it', (tester) async {
60+
await tester.pumpWidget(
61+
MaterialApp(
62+
home: Html(
63+
data: "<p>Text</p>",
64+
tagsList: ['div'], //Anything but `p`
65+
),
66+
),
67+
);
68+
expect(find.text('Text', findRichText: true), findsNothing);
69+
});
70+
71+
testWidgets('Check that a simple element is displayed when it is included in tagsList', (tester) async {
72+
await tester.pumpWidget(
73+
MaterialApp(
74+
home: Html(
75+
data: "<p>Text</p>",
76+
tagsList: ['html', 'body', 'p'],
77+
),
78+
),
79+
);
80+
expect(find.text('Text', findRichText: true), findsOneWidget);
81+
});
82+
83+
testWidgets('Check that a custom element is not displayed', (tester) async {
84+
await tester.pumpWidget(
85+
MaterialApp(
86+
home: Html(
87+
data: "<custom>Text</custom>",
88+
),
89+
),
90+
);
91+
expect(find.text('Text', findRichText: true), findsNothing);
92+
});
93+
94+
testWidgets('Check that a custom element is not displayed', (tester) async {
95+
await tester.pumpWidget(
96+
MaterialApp(
97+
home: Html(
98+
data: "<custom>Text</custom>",
99+
tagsList: Html.tags..add('custom'),
100+
),
101+
),
102+
);
103+
expect(find.text('Text', findRichText: true), findsOneWidget);
104+
});
105+
}

test/style/dimension_test.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ void main() {
2828
expect(lengthPercent.unit, equals(Unit.px));
2929
});
3030

31-
// test("Pass in invalid unit", () {
32-
// expect(() => Length(nonZeroNumber, Unit.percent), throwsAssertionError);
33-
// });
34-
35-
// test("Pass in invalid unit with zero", () {
36-
// expect(() => Length(0, Unit.percent), throwsAssertionError);
37-
// });
38-
39-
// test("Pass in a valid unit", () {
40-
// final lengthPercent = LengthOrPercent(nonZeroNumber, Unit.percent);
41-
// expect(lengthPercent.value, equals(nonZeroNumber));
42-
// expect(lengthPercent.unit, equals(Unit.percent));
43-
// });
31+
test("Pass in invalid unit", () {
32+
expect(() => Length(nonZeroNumber, Unit.percent), throwsAssertionError);
33+
});
34+
35+
test("Pass in invalid unit with zero", () {
36+
expect(() => Length(0, Unit.percent), throwsAssertionError);
37+
});
38+
39+
test("Pass in a valid unit", () {
40+
final lengthPercent = LengthOrPercent(nonZeroNumber, Unit.percent);
41+
expect(lengthPercent.value, equals(nonZeroNumber));
42+
expect(lengthPercent.unit, equals(Unit.percent));
43+
});
4444
}

test/style/fontsize_test.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import 'package:flutter_html/src/style/fontsize.dart';
2+
import 'package:flutter_html/src/style/length.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
test('Check basic FontSize inheritance', () {
7+
final FontSize parent = FontSize(16);
8+
final FontSize? child = null;
9+
10+
final result = FontSize.inherit(parent, child);
11+
12+
expect(result?.value, equals(16));
13+
});
14+
15+
test('Check double null FontSize inheritance', () {
16+
final FontSize? parent = null;
17+
final FontSize? child = null;
18+
19+
final result = FontSize.inherit(parent, child);
20+
21+
expect(result?.value, equals(null));
22+
});
23+
24+
test('Check basic em inheritance', () {
25+
final FontSize? parent = FontSize(16);
26+
final FontSize? child = FontSize(1, Unit.em);
27+
28+
final result = FontSize.inherit(parent, child);
29+
30+
expect(result?.value, equals(16));
31+
});
32+
33+
test('Check factor em inheritance', () {
34+
final FontSize? parent = FontSize(16);
35+
final FontSize? child = FontSize(0.5, Unit.em);
36+
37+
final result = FontSize.inherit(parent, child);
38+
39+
expect(result?.value, equals(8));
40+
});
41+
42+
test('Check basic % inheritance', () {
43+
final FontSize? parent = FontSize(16);
44+
final FontSize? child = FontSize(100, Unit.percent);
45+
46+
final result = FontSize.inherit(parent, child);
47+
48+
expect(result?.value, equals(16));
49+
});
50+
51+
test('Check scaled % inheritance', () {
52+
final FontSize? parent = FontSize(16);
53+
final FontSize? child = FontSize(50, Unit.percent);
54+
55+
final result = FontSize.inherit(parent, child);
56+
57+
expect(result?.value, equals(8));
58+
});
59+
}

test/utils_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:flutter_html/flutter_html.dart';
2+
import 'package:flutter_html/src/css_parser.dart';
3+
import 'package:flutter_html/src/utils.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
6+
/// Tests the file lib/src/utils.dart
7+
8+
void main() {
9+
test('Tests that namedColors returns a valid color', () {
10+
expect(ExpressionMapping.namedColorToColor('red'), equals(ExpressionMapping.stringToColor(namedColors['Red']!)));
11+
expect(namedColors['Red'], equals('#FF0000'));
12+
});
13+
14+
test('CustomBorderSide does not allow negative width', () {
15+
expect(() => CustomBorderSide(width: -5), throwsAssertionError);
16+
expect(CustomBorderSide(width: 0), TypeMatcher<CustomBorderSide>());
17+
expect(CustomBorderSide(width: 5), TypeMatcher<CustomBorderSide>());
18+
});
19+
20+
const originalString = 'Hello';
21+
const uppercaseString = 'HELLO';
22+
const lowercaseString = 'hello';
23+
24+
test('TextTransformUtil returns self if transform is null', () {
25+
expect(originalString.transformed(null), equals(originalString));
26+
});
27+
28+
test('TextTransformUtil uppercases correctly', () {
29+
expect(originalString.transformed(TextTransform.uppercase), equals(uppercaseString));
30+
});
31+
32+
test('TextTransformUtil lowercases correctly', () {
33+
expect(originalString.transformed(TextTransform.lowercase), equals(lowercaseString));
34+
});
35+
36+
const originalLongString = 'Hello, world! pub.dev';
37+
const capitalizedLongString = 'Hello, World! Pub.Dev';
38+
39+
test('TextTransformUtil capitalizs correctly', () {
40+
expect(originalLongString.transformed(TextTransform.capitalize), equals(capitalizedLongString));
41+
});
42+
}

0 commit comments

Comments
 (0)