2018-01-05 08:19:35 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsQuoted(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
d1 string
|
|
|
|
e1 bool
|
|
|
|
}{
|
|
|
|
{``, false},
|
|
|
|
{`foo`, false},
|
|
|
|
{`""`, true},
|
|
|
|
{`"a"`, true},
|
|
|
|
{`"bb"`, true},
|
|
|
|
{`"ccc"`, true},
|
|
|
|
{`"aaa" "bbb"`, true},
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
|
|
|
r := IsQuoted(test.d1)
|
|
|
|
if r != test.e1 {
|
|
|
|
t.Errorf("%v: expected (%v) got (%v)", i, test.e1, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStripQuotes(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
d1 string
|
|
|
|
e1 string
|
|
|
|
}{
|
|
|
|
{``, ``},
|
|
|
|
{`a`, `a`},
|
|
|
|
{`bb`, `bb`},
|
|
|
|
{`ccc`, `ccc`},
|
|
|
|
{`dddd`, `dddd`},
|
|
|
|
{`"A"`, `A`},
|
|
|
|
{`"BB"`, `BB`},
|
|
|
|
{`"CCC"`, `CCC`},
|
|
|
|
{`"DDDD"`, `DDDD`},
|
|
|
|
{`"EEEEE"`, `EEEEE`},
|
|
|
|
{`"aaa" "bbb"`, `aaa" "bbb`},
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
|
|
|
r := StripQuotes(test.d1)
|
|
|
|
if r != test.e1 {
|
|
|
|
t.Errorf("%v: expected (%v) got (%v)", i, test.e1, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetTxtParse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
d1 string
|
|
|
|
e1 string
|
|
|
|
e2 []string
|
|
|
|
}{
|
|
|
|
{`foo`, `foo`, []string{`foo`}},
|
|
|
|
{`"foo"`, `foo`, []string{`foo`}},
|
2018-02-16 01:02:50 +08:00
|
|
|
{`"foo bar"`, `foo bar`, []string{`foo bar`}},
|
|
|
|
{`foo bar`, `foo bar`, []string{`foo bar`}},
|
2018-01-05 08:19:35 +08:00
|
|
|
{`"aaa" "bbb"`, `aaa`, []string{`aaa`, `bbb`}},
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
2018-02-16 01:02:50 +08:00
|
|
|
ls := ParseQuotedTxt(test.d1)
|
|
|
|
if ls[0] != test.e1 {
|
|
|
|
t.Errorf("%v: expected Target=(%v) got (%v)", i, test.e1, ls[0])
|
|
|
|
}
|
|
|
|
if len(ls) != len(test.e2) {
|
|
|
|
t.Errorf("%v: expected TxtStrings=(%v) got (%v)", i, test.e2, ls)
|
|
|
|
}
|
|
|
|
for i := range ls {
|
2022-01-25 22:57:20 +08:00
|
|
|
if ls[i] != test.e2[i] {
|
2018-02-16 01:02:50 +08:00
|
|
|
t.Errorf("%v: expected TxtStrings=(%v) got (%v)", i, test.e2, ls)
|
|
|
|
}
|
2018-01-05 08:19:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 22:57:20 +08:00
|
|
|
|
|
|
|
func TestParseQuotedFields(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
d1 string
|
|
|
|
e1 []string
|
|
|
|
}{
|
|
|
|
{`1 2 3`, []string{`1`, `2`, `3`}},
|
|
|
|
{`1 "2" 3`, []string{`1`, `2`, `3`}},
|
|
|
|
{`1 2 "three 3"`, []string{`1`, `2`, `three 3`}},
|
|
|
|
{`0 issue "letsencrypt.org; validationmethods=dns-01; accounturi=https://acme-v02.api.letsencrypt.org/acme/acct/1234"`, []string{`0`, `issue`, `letsencrypt.org; validationmethods=dns-01; accounturi=https://acme-v02.api.letsencrypt.org/acme/acct/1234`}},
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
|
|
|
ls, _ := ParseQuotedFields(test.d1)
|
|
|
|
//fmt.Printf("%v: expected TxtStrings:\nWANT: %v\n GOT: %v\n", i, test.e1, ls)
|
|
|
|
if len(ls) != len(test.e1) {
|
|
|
|
t.Errorf("%v: expected TxtStrings=(%v) got (%v)", i, test.e1, ls)
|
|
|
|
}
|
|
|
|
for i := range ls {
|
|
|
|
if ls[i] != test.e1[i] {
|
|
|
|
t.Errorf("%v: expected TxtStrings=(%v) got (%v)", i, test.e1, ls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|