Skip to content

Commit

Permalink
[text] Add some more test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
DHowett committed Mar 21, 2017
1 parent 8506660 commit c5eac84
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 31 deletions.
12 changes: 6 additions & 6 deletions invalid_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
var InvalidTextPlists []string = []string{
"(/",
"{/",
"(/",
"<*I>",
"{0=(/",
"(((/",
Expand All @@ -17,17 +16,18 @@ var InvalidTextPlists []string = []string{
"/",
"{0=((((/",
"({/",
"(<*I5>,<*I5>,<*I5>,<" +
"*I5>,*I16777215>,<*I" +
"268435455>,<*I429496" +
"7295>,<*I18446744073" +
"709551615>,)",
"(<*I5>,<*I5>,<*I5>,<*I5>,*I16777215>,<*I268435455>,<*I4294967295>,<*I18446744073709551615>,)",
"{0=(((/",
"(<*I>",
"<>",
"((((/",
"((/",
"(<>",
"{¬=A;}", // that character should be in quotes for goth GNUStep and OpenStep
`{"A"A;}`, // there should be an = between "A" and A
`{"A"=A}`, // there should be a ; at the end of the dictionary
"<*F>", // invalid GNUstep type F
"<EQ>", // invalid data that isn't hex
}

func TestInvalidTextPlists(t *testing.T) {
Expand Down
108 changes: 83 additions & 25 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plist
import (
"bytes"
"io/ioutil"
"reflect"
"testing"
)

Expand Down Expand Up @@ -37,31 +38,88 @@ func BenchmarkGNUStepParse(b *testing.B) {
}
}

func TestTextCommentDecode(t *testing.T) {
var testData = `{
A=1 /* A is 1 because it is the first letter */;
B=2; // B is 2 because comment-to-end-of-line.
C=3;
S = /not/a/comment/;
S2 = /not*a/*comm*en/t;
}`
type D struct {
A, B, C int
S string
S2 string
}
actual := D{1, 2, 3, "/not/a/comment/", "/not*a/*comm*en/t"}
var parsed D
buf := bytes.NewReader([]byte(testData))
decoder := NewDecoder(buf)
err := decoder.Decode(&parsed)
if err != nil {
t.Error(err.Error())
}
var textTestCases = []TestData{
{
Name: "Comments",
Data: struct {
A, B, C int
S, S2 string
}{
1, 2, 3,
"/not/a/comment/", "/not*a/*comm*en/t",
},
Expected: map[int][]byte{
OpenStepFormat: []byte(`{
A=1 /* A is 1 because it is the first letter */;
B=2; // B is 2 because comment-to-end-of-line.
C=3;
S = /not/a/comment/;
S2 = /not*a/*comm*en/t;
}`),
},
},
{
Name: "Escapes",
Data: struct {
A, B, V, F, T, R, N, Hex1, Unicode1, Unicode2, Octal1 string
}{
"\a", "\b", "\v", "\f", "\t", "\r", "\n", "\u00ab", "\u00ac", "\u00ad", "\033",
},
Expected: map[int][]byte{
OpenStepFormat: []byte(`{
A="\a";
B="\b";
V="\v";
F="\f";
T="\t";
R="\r";
N="\n";
Hex1="\xAB";
Unicode1="\u00AC";
Unicode2="\U00AD";
Octal1="\033";
}`),
},
},
{
Name: "Empty Strings in Arrays",
Data: []string{"A"},
Expected: map[int][]byte{
OpenStepFormat: []byte(`(A,,,"",)`),
},
},
}

func TestTextDecode(t *testing.T) {
for _, test := range textTestCases {
actual := test.Data
testData := reflect.ValueOf(actual)
if !testData.IsValid() || isEmptyInterface(testData) {
continue
}
if testData.Kind() == reflect.Ptr || testData.Kind() == reflect.Interface {
testData = testData.Elem()
}
actual = testData.Interface()

parsed := reflect.New(testData.Type()).Interface()
buf := bytes.NewReader(test.Expected[OpenStepFormat])
decoder := NewDecoder(buf)
err := decoder.Decode(parsed)
if err != nil {
t.Error(err.Error())
}

vt := reflect.ValueOf(parsed)
if vt.Kind() == reflect.Ptr || vt.Kind() == reflect.Interface {
vt = vt.Elem()
parsed = vt.Interface()
}

if actual != parsed {
t.Logf("Expected: %#v", actual)
t.Logf("Received: %#v", parsed)
t.Fail()
if !reflect.DeepEqual(actual, parsed) {
t.Logf("Expected: %#v", actual)
t.Logf("Received: %#v", parsed)
t.Fail()
}
}
}

0 comments on commit c5eac84

Please sign in to comment.