summaryrefslogtreecommitdiff
path: root/gopher/gophermap/parse_test.go
blob: 51a430e3437f29acf5da9c6687a5033d3e58f0f6 (plain)
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
package gophermap_test

import (
	"bytes"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"tildegit.org/tjp/sliderule/gopher"
	"tildegit.org/tjp/sliderule/gopher/gophermap"
)

func TestParse(t *testing.T) {
	tests := []struct {
		doc   string
		lines gopher.MapDocument
	}{
		{
			doc: `
iI am informational text		localhost	70
icontinued on this line		localhost	70
i		localhost	70
0this is my text file	/file.txt	localhost	70
i		localhost	70
1here's a sub-menu	/sub/	localhost	70
.
`[1:],
			lines: gopher.MapDocument{
				gopher.MapItem{
					Type:     gopher.InfoMessageType,
					Display:  "I am informational text",
					Selector: "",
					Hostname: "localhost",
					Port:     "70",
				},
				gopher.MapItem{
					Type:     gopher.InfoMessageType,
					Display:  "continued on this line",
					Selector: "",
					Hostname: "localhost",
					Port:     "70",
				},
				gopher.MapItem{
					Type:     gopher.InfoMessageType,
					Display:  "",
					Selector: "",
					Hostname: "localhost",
					Port:     "70",
				},
				gopher.MapItem{
					Type:     gopher.TextFileType,
					Display:  "this is my text file",
					Selector: "/file.txt",
					Hostname: "localhost",
					Port:     "70",
				},
				gopher.MapItem{
					Type:     gopher.InfoMessageType,
					Display:  "",
					Selector: "",
					Hostname: "localhost",
					Port:     "70",
				},
				gopher.MapItem{
					Type:     gopher.MenuType,
					Display:  "here's a sub-menu",
					Selector: "/sub/",
					Hostname: "localhost",
					Port:     "70",
				},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.lines[0].Display, func(t *testing.T) {
			text := strings.ReplaceAll(test.doc, "\n", "\r\n")
			doc, err := gophermap.Parse(bytes.NewBufferString(text))
			require.Nil(t, err)

			if assert.Equal(t, len(test.lines), len(doc)) {
				for i, line := range doc {
					expect := test.lines[i]

					assert.Equal(t, expect.Type, line.Type)
					assert.Equal(t, expect.Display, line.Display)
					assert.Equal(t, expect.Selector, line.Selector)
					assert.Equal(t, expect.Hostname, line.Hostname)
					assert.Equal(t, expect.Port, line.Port)
				}
			}
		})
	}
}