๐Ÿ“ฆ amake / orgro

๐Ÿ“„ links_test.dart ยท 86 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86import 'package:flutter_test/flutter_test.dart';
import 'package:org_flutter/org_flutter.dart';
import 'package:orgro/src/pages/document/links.dart';

void main() {
  group('Abbreviations', () {
    group('LINK directives', () {
      test('None present', () {
        final doc = OrgDocument.parse('* foo');
        final found = extractLinkAbbreviations(doc);
        expect(found, isEmpty);
      });
      test('Finds one', () {
        final doc = OrgDocument.parse('''#+LINK: foo bar''');
        final found = extractLinkAbbreviations(doc);
        expect(found, [(linkword: 'foo', format: 'bar')]);
      });
      test('With spaces', () {
        final doc = OrgDocument.parse('''#+LINK: "foo bar" baz''');
        final found = extractLinkAbbreviations(doc);
        expect(found, [(linkword: 'foo bar', format: 'baz')]);
      });
      test('Case-insensitive', () {
        final doc = OrgDocument.parse('''#+link: "foo bar" baz''');
        final found = extractLinkAbbreviations(doc);
        expect(found, [(linkword: 'foo bar', format: 'baz')]);
      });
      test('Multiple', () {
        final doc = OrgDocument.parse('''
#+LINK: foo bar
#+LINK: "baz buzz" bazinga
''');
        final found = extractLinkAbbreviations(doc);
        expect(found, [
          (linkword: 'foo', format: 'bar'),
          (linkword: 'baz buzz', format: 'bazinga'),
        ]);
      });
    });
    group('Extraction', () {
      test('Appending', () {
        final doc = OrgDocument.parse('''#+LINK: foo bar''');
        final link = OrgPlainLink('foo:buzz');
        final result = expandAbbreviatedUrl(doc, link);
        expect(result, Uri.parse('barbuzz'));
      });
      test('Replacing', () {
        final doc = OrgDocument.parse('''#+LINK: foo bar=%s&blah''');
        final link = OrgPlainLink('foo:buzz');
        final result = expandAbbreviatedUrl(doc, link);
        expect(result, Uri.parse('bar=buzz&blah'));
      });
      test('Percent-encoding', () {
        final doc = OrgDocument.parse('''#+LINK: foo bar=%h&blah''');
        final link = OrgPlainLink('foo:ใ‚');
        final result = expandAbbreviatedUrl(doc, link);
        expect(result, Uri.parse('bar=%E3%81%82&blah'));
      });
      test('Missing definition', () {
        final doc = OrgDocument.parse('''#+LINK: foo bar=%h&blah''');
        final link = OrgPlainLink('foof:ใ‚');
        final result = expandAbbreviatedUrl(doc, link);
        expect(result, isNull);
      });
      test('Space in linkword', () {
        final doc = OrgDocument.parse('''#+LINK: "foo bar" baz=%h&blah''');
        final link = OrgPlainLink('foo bar:ใ‚');
        final result = expandAbbreviatedUrl(doc, link);
        expect(result, Uri.parse('baz=%E3%81%82&blah'));
      });
      test('Multiple placeholders (same)', () {
        final doc = OrgDocument.parse('''#+LINK: foo bar=%s&%s''');
        final link = OrgPlainLink('foo:baz');
        final result = expandAbbreviatedUrl(doc, link);
        expect(result, Uri.parse('bar=baz&%s'));
      });
      test('Multiple placeholders (different)', () {
        final doc = OrgDocument.parse('''#+LINK: foo bar=%h&%s''');
        final link = OrgPlainLink('foo:baz');
        final result = expandAbbreviatedUrl(doc, link);
        expect(result, Uri.parse('bar=%h&baz'));
      });
    });
  });
}