|
| 1 | +import 'package:fleather/fleather.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('$EmbedRegistry', () { |
| 7 | + test('create fallback', () { |
| 8 | + final registry = EmbedRegistry.fallback(); |
| 9 | + final hr = registry.blockEmbed(EmbeddableObject('hr', inline: false)); |
| 10 | + expect(hr, isA<HorizontalRule>()); |
| 11 | + }); |
| 12 | + |
| 13 | + test('create with configurations', () { |
| 14 | + final registry = EmbedRegistry.withConfigurations([FakeBlockEmbed()]); |
| 15 | + final fakeBlock = |
| 16 | + registry.blockEmbed(EmbeddableObject('fake_block', inline: false)); |
| 17 | + expect(fakeBlock, isA<FakeBlockEmbed>()); |
| 18 | + }); |
| 19 | + |
| 20 | + test('create fallback with configurations', () { |
| 21 | + final registry = |
| 22 | + EmbedRegistry.fallbackWithConfigurations([FakeBlockEmbed()]); |
| 23 | + final fakeBlock = |
| 24 | + registry.blockEmbed(EmbeddableObject('fake_block', inline: false)); |
| 25 | + expect(fakeBlock, isA<FakeBlockEmbed>()); |
| 26 | + final hr = registry.blockEmbed(EmbeddableObject('hr', inline: false)); |
| 27 | + expect(hr, isA<HorizontalRule>()); |
| 28 | + }); |
| 29 | + |
| 30 | + test('cannot register twice the same key', () { |
| 31 | + try { |
| 32 | + final registry = EmbedRegistry.withConfigurations([FakeBlockEmbed(), FakeBlockEmbed()]); |
| 33 | + } on ArgumentError catch (_) { |
| 34 | + return; |
| 35 | + } |
| 36 | + fail('Should throw an argument error'); |
| 37 | + }); |
| 38 | + |
| 39 | + test('spanEmbed - find nothing', () { |
| 40 | + final registry = EmbedRegistry(); |
| 41 | + try { |
| 42 | + registry.spanEmbed(EmbeddableObject('fake_span', inline: true)); |
| 43 | + } on StateError catch (_) { |
| 44 | + return; |
| 45 | + } |
| 46 | + fail('Should throw an assertion error'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('spanEmbed - finds a block embed', () { |
| 50 | + final registry = |
| 51 | + EmbedRegistry.fallbackWithConfigurations([FakeBlockEmbed()]); |
| 52 | + try { |
| 53 | + registry |
| 54 | + .spanEmbed(EmbeddableObject(FakeBlockEmbed().key, inline: true)); |
| 55 | + } on AssertionError catch (_) { |
| 56 | + return; |
| 57 | + } |
| 58 | + fail('Should throw an assertion error'); |
| 59 | + }); |
| 60 | + |
| 61 | + test('blockEmbed - find nothing', () { |
| 62 | + final registry = EmbedRegistry(); |
| 63 | + try { |
| 64 | + registry.blockEmbed(EmbeddableObject('fake_block', inline: false)); |
| 65 | + } on StateError catch (_) { |
| 66 | + return; |
| 67 | + } |
| 68 | + fail('Should throw an assertion error'); |
| 69 | + }); |
| 70 | + |
| 71 | + test('blockEmbed - finds a span embed', () { |
| 72 | + final registry = EmbedRegistry.withConfigurations([FakeSpanEmbed()]); |
| 73 | + try { |
| 74 | + registry |
| 75 | + .blockEmbed(EmbeddableObject(FakeSpanEmbed().key, inline: false)); |
| 76 | + } on AssertionError catch (_) { |
| 77 | + return; |
| 78 | + } |
| 79 | + fail('Should throw an assertion error'); |
| 80 | + }); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +class FakeBlockEmbed extends BlockEmbedConfiguration { |
| 85 | + FakeBlockEmbed() : super(key: 'fake_block'); |
| 86 | + |
| 87 | + @override |
| 88 | + Widget build(BuildContext context, Map<String, dynamic> data) { |
| 89 | + return Text('Span'); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +class FakeSpanEmbed extends SpanEmbedConfiguration { |
| 94 | + FakeSpanEmbed() : super(key: 'fake_span'); |
| 95 | + |
| 96 | + @override |
| 97 | + Widget build(BuildContext context, Map<String, dynamic> data) { |
| 98 | + return Text('Block'); |
| 99 | + } |
| 100 | +} |
0 commit comments