Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson committed Oct 28, 2023
1 parent 5090049 commit c528c83
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
9 changes: 9 additions & 0 deletions packages/core/test/_.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ class Explainer {
final image = d.image;
if (image != null) {
attr.add("image=${image.image}");
if (image.alignment != Alignment.center) {
attr.add(_alignment(image.alignment));
}
if (image.fit != BoxFit.scaleDown) {
attr.add('fit=${image.fit?.name}');
}
if (image.repeat != ImageRepeat.noRepeat) {
attr.add('repeat=${image.repeat.name}');
}
}

final borderRadius = d.borderRadius;
Expand Down
84 changes: 74 additions & 10 deletions packages/core/test/style_background_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ void main() {
expect(explained, equals('[RichText:(:Foo (color=#FFFF0000:bar))]'));
});

testWidgets('renders background', (WidgetTester tester) async {
const html = 'Foo <span style="background: #f00">bar</span>';
final explained = await explain(tester, html);
expect(explained, equals('[RichText:(:Foo (color=#FFFF0000:bar))]'));
});

group('renders without erroneous white spaces', () {
testWidgets('before', (WidgetTester tester) async {
const html = 'Foo<span style="background-color: #f00"> bar</span>';
Expand All @@ -87,7 +81,8 @@ void main() {
});
});

testWidgets('resets in continuous SPANs (#155)', (tester) async {
testWidgets('resets in continuous SPANs', (tester) async {
// https://github.com/daohoangson/flutter_widget_from_html/issues/155
const html =
'<span style="color: #ff0; background-color:#00f;">Foo</span>'
'<span style="color: #f00;">bar</span>';
Expand Down Expand Up @@ -176,19 +171,88 @@ void main() {
),
);
});
});

testWidgets('renders background', (WidgetTester tester) async {
const assetName = 'test/images/logo.png';
group('shorthand', () {
const assetName = 'test/images/logo.png';
const assetImage = 'AssetImage(bundle: null, name: "$assetName")';

testWidgets('renders color', (WidgetTester tester) async {
const html = 'Foo <span style="background: #f00">bar</span>';
final explained = await explain(tester, html);
expect(explained, equals('[RichText:(:Foo (color=#FFFF0000:bar))]'));
});

testWidgets('renders image', (WidgetTester tester) async {
const html = '<div style="background: url(asset:$assetName)">Foo</div>';
final explained = await explain(tester, html);
expect(
explained,
equals(
'[Container:image=AssetImage(bundle: null, name: "$assetName"),child='
'[Container:image=$assetImage,child='
'[CssBlock:child=[RichText:(:Foo)]]'
']',
),
);
});

testWidgets('renders position', (WidgetTester tester) async {
const html =
'<div style="background: url(asset:$assetName) right">Foo</div>';
final explained = await explain(tester, html);
expect(
explained,
equals(
'[Container:image=$assetImage,alignment=centerRight,child='
'[CssBlock:child=[RichText:(:Foo)]]'
']',
),
);
});

testWidgets('renders repeat', (WidgetTester tester) async {
const html =
'<div style="background: url(asset:$assetName) repeat">Foo</div>';
final explained = await explain(tester, html);
expect(
explained,
equals(
'[Container:image=$assetImage,repeat=repeat,child='
'[CssBlock:child=[RichText:(:Foo)]]'
']',
),
);
});

testWidgets('renders size', (WidgetTester tester) async {
const html =
'<div style="background: url(asset:$assetName) cover">Foo</div>';
final explained = await explain(tester, html);
expect(
explained,
equals(
'[Container:image=$assetImage,fit=cover,child='
'[CssBlock:child=[RichText:(:Foo)]]'
']',
),
);
});

testWidgets('renders everything', (WidgetTester tester) async {
const html = '<div style="background: #f00 '
'url(asset:$assetName) right repeat cover">Foo</div>';
final explained = await explain(tester, html);
expect(
explained,
equals(
'[Container:color=#FFFF0000,'
'image=$assetImage,'
'alignment=centerRight,'
'fit=cover,'
'repeat=repeat,'
'child=[CssBlock:child=[RichText:(:Foo)]]]',
),
);
});
});
}

0 comments on commit c528c83

Please sign in to comment.