File: ref_test.go

package info (click to toggle)
golang-github-gomarkdown-markdown 0.0~git20231115.a660076-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: sh: 26; makefile: 5
file content (97 lines) | stat: -rw-r--r-- 2,146 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
package parser

import (
	"testing"

	"github.com/gomarkdown/markdown/ast"
)

func TestCrossReference(t *testing.T) {
	p := New()

	tests := []struct {
		data []byte
		r    *ast.CrossReference
		fail bool
	}{
		// ok
		{
			data: []byte("(#yes)"),
			r:    &ast.CrossReference{Destination: []byte("yes")},
		},
		{
			data: []byte("(#y:es)"),
			r:    &ast.CrossReference{Destination: []byte("y:es")},
		},
		{
			data: []byte("(#id, random text)"),
			r:    &ast.CrossReference{Destination: []byte("id"), Suffix: []byte("random text")},
		},
		// fails
		{data: []byte("(#y es)"), r: nil, fail: true},
		{data: []byte("(#yes"), r: nil, fail: true},
		{data: []byte("(#y es, random text"), r: nil, fail: true},
	}

	for i, test := range tests {
		_, n := maybeShortRefOrIndex(p, test.data, 0)
		if test.fail && n != nil {
			t.Errorf("test %d, should have failed to parse %s", i, test.data)
		}
	}
}

func TestIndex(t *testing.T) {
	p := New()

	tests := []struct {
		data []byte
		i    *ast.Index
		fail bool
	}{
		// ok
		{
			data: []byte("(!yes)"),
			i:    &ast.Index{Item: []byte("yes")},
		},
		{
			data: []byte("(!y:es)"),
			i:    &ast.Index{Item: []byte("y:es")},
		},
		{
			data: []byte("(!yes, no)"),
			i:    &ast.Index{Item: []byte("yes"), Subitem: []byte("no")},
		},
		{
			data: []byte("(!  yes  , no  )"),
			i:    &ast.Index{Item: []byte("yes"), Subitem: []byte("no")},
		},
		// fails
		{data: []byte("(!yes"), fail: true},
		{data: []byte("(_yes"), fail: true},
	}

	for i, test := range tests {
		_, n := maybeShortRefOrIndex(p, test.data, 0)
		if test.fail && n != nil {
			t.Errorf("test %d, should have failed to parse %s", i, test.data)
			continue
		}
		if test.fail && n == nil {
			// ok
			continue
		}

		idx := n.(*ast.Index)

		if string(test.i.Item) != string(idx.Item) {
			t.Errorf("test %d, got item %s, wanted %s", i, idx.Item, test.i.Item)
		}
		if string(test.i.Subitem) != string(idx.Subitem) {
			t.Errorf("test %d, got item %s, wanted %s", i, idx.Subitem, test.i.Subitem)
		}
		if test.i.Primary != idx.Primary {
			t.Errorf("test %d, got item %t, wanted %t", i, idx.Primary, test.i.Primary)
		}
	}
}