File: builder_test.go

package info (click to toggle)
pat 0.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,296 kB
  • sloc: javascript: 3,891; sh: 124; makefile: 11
file content (122 lines) | stat: -rw-r--r-- 3,776 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package forms

import (
	"bufio"
	"bytes"
	"testing"
	"time"

	"github.com/la5nta/pat/cfg"
)

// mockLocatorProvider implements LocatorProvider for testing
type mockLocatorProvider string

func (m mockLocatorProvider) Locator() string { return string(m) }

func TestInsertionTagReplacer(t *testing.T) {
	m := &Manager{config: Config{
		MyCall:          "LA5NTA",
		AppVersion:      "Pat v1.0.0 (test)",
		GPSd:            cfg.GPSdConfig{Addr: gpsMockAddr},
		LocatorProvider: mockLocatorProvider("JO29PJ"),
	}}
	location = time.FixedZone("UTC+1", 1*60*60)
	now = func() time.Time { return time.Date(1988, 3, 21, 0, 0, 0, 0, location).In(time.UTC) }
	tests := map[string]string{
		"<ProgramVersion>": "Pat v1.0.0 (test)",
		"<Callsign>":       "LA5NTA",
		"<MsgSender>":      "LA5NTA",

		"<DateTime>":  "1988-03-21 00:00:00",
		"<UDateTime>": "1988-03-20 23:00:00Z",
		"<Date>":      "1988-03-21",
		"<UDate>":     "1988-03-20Z",
		"<UDTG>":      "202300Z MAR 1988",
		"<Time>":      "00:00:00",
		"<UTime>":     "23:00:00Z",
		"<Day>":       "Monday",
		"<UDay>":      "Sunday",

		"<GPS>":                "59-24.83N 005-16.08E",
		"<GPS_DECIMAL>":        "59.4138N 5.2680E",
		"<GPS_SIGNED_DECIMAL>": "59.4138 5.2680",
		"<GridSquare>":         "JO29PJ",
		"<Latitude>":           "59.4138",
		"<Longitude>":          "5.2680",
		"<GPSValid>":           "YES",
		"<GPSLatitude>":        "59.4138",
		"<GPSLongitude>":       "5.2680",
		"<FormFolder>":         "/api/forms/some-folder",
	}
	for in, expect := range tests {
		t.Run(in, func(t *testing.T) {
			if out := insertionTagReplacer(m, nil, "some-folder/template.txt", "<", ">")(in); out != expect {
				t.Errorf("Expected %q, got %q", expect, out)
			}
		})
	}
}

func TestBuildXML(t *testing.T) {
	location = time.FixedZone("UTC+1", 1*60*60)
	now = func() time.Time { return time.Date(1988, 3, 21, 0, 0, 0, 0, location).In(time.UTC) }
	b := messageBuilder{
		FormsMgr: &Manager{config: Config{
			MyCall:          "LA5NTA",
			AppVersion:      "v1.0.0",
			GPSd:            cfg.GPSdConfig{Addr: gpsMockAddr},
			LocatorProvider: mockLocatorProvider("JO29PJ"),
		}},
		Template: Template{DisplayFormPath: "viewer.html", ReplyTemplatePath: "reply.txt"},
		FormValues: map[string]string{
			"var1": "foo",
			"var2": "bar",
			"var3": "  baz \t\n", // Leading and trailing whitespace trimmed
		},
	}
	expect := []byte(`
        <?xml version="1.0" encoding="UTF-8"?>
        <RMS_Express_Form>
          <form_parameters>
            <xml_file_version>1.0</xml_file_version>
            <rms_express_version>v1.0.0</rms_express_version>
            <submission_datetime>19880320230000</submission_datetime>
            <senders_callsign>LA5NTA</senders_callsign>
            <grid_square>JO29PJ</grid_square>
            <display_form>viewer.html</display_form>
            <reply_template>reply.txt</reply_template>
          </form_parameters>
          <variables>
            <var1>foo</var1>
            <var2>bar</var2>
            <var3>baz</var3>
          </variables>
        </RMS_Express_Form>
	`)
	if got := b.buildXML(); !xmlEqual(t, got, expect) {
		t.Errorf("Got unexpected XML:\n%s\n", string(got))
	}
}

// xmlEqual compares two byte slices line by line, ignoring leading/trailing whitespace.
func xmlEqual(t *testing.T, a, b []byte) bool {
	lines := func(xml []byte) (slice [][]byte) {
		s := bufio.NewScanner(bytes.NewReader(xml))
		for s.Scan() {
			l := bytes.TrimSpace(s.Bytes())
			if len(l) > 0 {
				slice = append(slice, l)
			}
		}
		return slice
	}
	aLines, bLines := lines(a), lines(b)
	for i := 0; i < len(aLines) && i < len(bLines); i++ {
		if !bytes.Equal(aLines[i], bLines[i]) {
			t.Errorf("%q != %q", aLines[i], bLines[i])
			return false
		}
	}
	return len(aLines) == len(bLines)
}