package udp
import "testing"
// The escaping rule is the whole difference between a template that works on
// the bench and one that fails on the first portable callsign.
func TestRenderEscapesForURLsOnly(t *testing.T) {
fields := map[string]string{
"call": "F4BPO/P", "name": "Jean Pierre", "band": "20m", "az": "137",
}
// UDP: the receiver wants the text as typed.
if got := Render("{call}", fields, false); got != "F4BPO/P" {
t.Errorf("UDP render = %q", got)
}
// URL: a slash in a callsign is a path separator, a space breaks the request.
if got := Render("http://sw/a?c={call}&n={name}", fields, true); got != "http://sw/a?c=F4BPO%2FP&n=Jean+Pierre" {
t.Errorf("URL render = %q", got)
}
}
// A placeholder the trigger does not carry becomes empty, not the literal
// "{freq}" — otherwise the braces go down the wire and a receiver files them.
func TestRenderDropsUnknownFields(t *testing.T) {
if got := Render("{az}{freq}", map[string]string{"az": "137"}, false); got != "137" {
t.Errorf("got %q", got)
}
}
// Text that merely looks like a placeholder must survive: an unclosed brace is
// content, not a broken field.
func TestRenderLeavesLiteralBraces(t *testing.T) {
for _, c := range []struct{ in, want string }{
{"no fields here", "no fields here"},
{"{unclosed", "{unclosed"},
{"a } b", "a } b"},
} {
if got := Render(c.in, map[string]string{}, false); got != c.want {
t.Errorf("Render(%q) = %q, want %q", c.in, got, c.want)
}
}
}
// Field names are matched case- and space-insensitively: an operator typing
// {Band} or { band } means the band.
func TestRenderFieldNamesAreForgiving(t *testing.T) {
f := map[string]string{"band": "20m"}
for _, tmpl := range []string{"{band}", "{Band}", "{ BAND }"} {
if got := Render(tmpl, f, false); got != "20m" {
t.Errorf("Render(%q) = %q, want 20m", tmpl, got)
}
}
}
// Receivers that read a line at a time need a terminator, and a trailing space
// in a text box is invisible — hence a choice rather than something to type.
func TestLineEnding(t *testing.T) {
for in, want := range map[string]string{
"": "", "lf": "\n", "LF": "\n", "crlf": "\r\n", " CRLF ": "\r\n", "nonsense": "",
} {
if got := lineEnding(in); got != want {
t.Errorf("lineEnding(%q) = %q, want %q", in, got, want)
}
}
}