You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.3 KiB
69 lines
2.3 KiB
import 'package:flutter_linkify/flutter_linkify.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:meshcore_open/helpers/link_handler.dart';
|
|
|
|
// #1289: a meshcore:// link in a message is tappable. The stock URL
|
|
// linkifier only finds http(s) and www, so before this the link was plain
|
|
// text.
|
|
|
|
const _link = 'meshcore://channel/add?name=%23example';
|
|
|
|
/// The same chain, in the same order, LinkHandler gives Linkify.
|
|
List<LinkifyElement> _parse(String text) {
|
|
const options = LinkifyOptions(humanize: false, defaultToHttps: false);
|
|
var elements = <LinkifyElement>[TextElement(text)];
|
|
for (final linkifier in const <Linkifier>[
|
|
MeshcoreLinkifier(),
|
|
UrlLinkifier(),
|
|
EmailLinkifier(),
|
|
]) {
|
|
elements = linkifier.parse(elements, options);
|
|
}
|
|
return elements;
|
|
}
|
|
|
|
void main() {
|
|
test('finds a meshcore:// link in a sentence', () {
|
|
final elements = _parse('join $_link now');
|
|
final links = elements.whereType<UrlElement>().toList();
|
|
expect(links.map((e) => e.url), [_link]);
|
|
expect(elements.first, isA<TextElement>());
|
|
expect((elements.first as TextElement).text, 'join ');
|
|
expect((elements.last as TextElement).text, ' now');
|
|
});
|
|
|
|
test('leaves a full stop after the link out of it', () {
|
|
final elements = _parse('Join $_link.');
|
|
expect(elements.whereType<UrlElement>().single.url, _link);
|
|
expect((elements.last as TextElement).text, '.');
|
|
});
|
|
|
|
test('finds several links and still finds web links', () {
|
|
final elements = _parse(
|
|
'$_link and https://offband.org and meshcore://contact/add?name=B',
|
|
);
|
|
expect(elements.whereType<UrlElement>().map((e) => e.url), [
|
|
_link,
|
|
'https://offband.org',
|
|
'meshcore://contact/add?name=B',
|
|
]);
|
|
});
|
|
|
|
test('leaves a closing bracket around the link out of it', () {
|
|
final elements = _parse('(see $_link).');
|
|
expect(elements.whereType<UrlElement>().single.url, _link);
|
|
expect((elements.last as TextElement).text, ').');
|
|
});
|
|
|
|
test('keeps a bracket the link itself opened', () {
|
|
const named = 'meshcore://channel/add?name=%23a(b)';
|
|
expect(_parse('join $named').whereType<UrlElement>().single.url, named);
|
|
});
|
|
|
|
test('plain text is left alone', () {
|
|
final elements = _parse('no links here');
|
|
expect(elements, hasLength(1));
|
|
expect((elements.single as TextElement).text, 'no links here');
|
|
});
|
|
}
|