Skip to content

Commit

Permalink
test: add tests for ByteStr and ByteStrEscape, reduce tests duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Apr 5, 2022
1 parent 85e2464 commit 685fc6e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 92 deletions.
2 changes: 1 addition & 1 deletion dec_skip_cases_test.go
Expand Up @@ -319,7 +319,7 @@ var testStrings = append([]string{
"\"\\ueeee\"", // valid
"\"\\uFFFF\"", // valid
`"ab\n` + "\x00" + `"`, // invalid
`"\n0123456"`,
`"\n0123456"`, // valid
}, func() (r []string) {
// Generate tests for invalid space sequences.
for i := byte(0); i <= ' '; i++ {
Expand Down
135 changes: 75 additions & 60 deletions enc_str_test.go
Expand Up @@ -2,6 +2,7 @@ package jx

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -17,66 +18,47 @@ func TestEncoder_StringEscape(t *testing.T) {
require.Equal(t, expected, string(s.Bytes()))
}

func TestEncoder_String(t *testing.T) {
func TestEncoder_Str(t *testing.T) {
testCases := []struct {
name, input, expect string
input string
}{
{`Empty`, ``, `""`},
{`Simple`, `abcd`, `"abcd"`},
{``},
{`abcd`},
{
`Escape`,
`abcd\nH\tel\tl\ro\\World\r` + "\n\rHello\r\tHi",
`"abcd\\nH\\tel\\tl\\ro\\\\World\\r\n\rHello\r\tHi"`,
},
{"\x00"},
{"\x00 "},
{`"hello, world!"`},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
e := GetEncoder()
e.Str(tt.input)
require.Equal(t, tt.expect, string(e.Bytes()))
requireCompat(t, e.Bytes(), tt.input)
t.Run("Decode", func(t *testing.T) {
i := GetDecoder()
i.ResetBytes(e.Bytes())
s, err := i.Str()
require.NoError(t, err)
require.Equal(t, tt.input, s)
})
for i, tt := range testCases {
tt := tt
t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) {
for _, enc := range []struct {
name string
enc func(e *Encoder, input string)
}{
{"Str", (*Encoder).Str},
{"Bytes", func(e *Encoder, input string) {
e.ByteStr([]byte(tt.input))
}},
} {
enc := enc
t.Run(enc.name, func(t *testing.T) {
e := GetEncoder()
enc.enc(e, tt.input)
requireCompat(t, e.Bytes(), tt.input)
t.Run("Decode", func(t *testing.T) {
i := GetDecoder()
i.ResetBytes(e.Bytes())
s, err := i.Str()
require.NoError(t, err)
require.Equal(t, tt.input, s)
})
})
}
})
}
t.Run("StrEscapeFast", func(t *testing.T) {
e := GetEncoder()
e.StrEscape("Foo")
require.Equal(t, `"Foo"`, e.String())
})
t.Run("StrEscapeBad", func(t *testing.T) {
e := GetEncoder()
e.StrEscape("\uFFFD")
require.Equal(t, `"�"`, e.String())
v, err := DecodeBytes(e.Bytes()).Str()
require.NoError(t, err)
require.Equal(t, "�", v)
})
t.Run("BadUnicode", func(t *testing.T) {
e := GetEncoder()
e.StrEscape("a\xc5z")
require.Equal(t, `"a\ufffdz"`, e.String())
v, err := DecodeBytes(e.Bytes()).Str()
require.NoError(t, err)
require.Equal(t, "a�z", v)
})
t.Run("Emoji", func(t *testing.T) {
e := GetEncoder()
e.Str(string([]byte{240, 159, 144, 152}))
v, err := DecodeBytes(e.Bytes()).Str()
require.NoError(t, err)
require.Equal(t, "🐘", v)
})
t.Run("BadUnicodeAfterSafeEscape", func(t *testing.T) {
e := GetEncoder()
e.StrEscape("<f\xed\xa0\x80")
require.Equal(t, `"\u003cf\ufffd\ufffd\ufffd"`, e.String())
})
t.Run("Quotes", func(t *testing.T) {
const (
v = "\"/\""
Expand All @@ -85,14 +67,6 @@ func TestEncoder_String(t *testing.T) {
e.Str(v)
requireCompat(t, e.Bytes(), v)
})
t.Run("QuotesEscape", func(t *testing.T) {
const (
v = "\"/\""
)
var e Encoder
e.StrEscape(v)
requireCompat(t, e.Bytes(), v)
})
t.Run("QuotesObj", func(t *testing.T) {
const (
k = "k"
Expand All @@ -112,3 +86,44 @@ func TestEncoder_String(t *testing.T) {
requireCompat(t, e.Bytes(), map[string]string{k: v})
})
}

func TestEncoder_StrEscape(t *testing.T) {
testCases := []struct {
input, expect string
}{
{"Foo", `"Foo"`},
{"\uFFFD", `"�"`},
{"a\xc5z", `"a\ufffdz"`},
{"<f\xed\xa0\x80", `"\u003cf\ufffd\ufffd\ufffd"`},
}
for i, tt := range testCases {
tt := tt
t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) {
for _, enc := range []struct {
name string
enc func(e *Encoder, input string)
}{
{"Str", (*Encoder).StrEscape},
{"Bytes", func(e *Encoder, input string) {
e.ByteStrEscape([]byte(tt.input))
}},
} {
enc := enc
t.Run(enc.name, func(t *testing.T) {
e := GetEncoder()
enc.enc(e, tt.input)
require.Equal(t, tt.expect, string(e.Bytes()))
requireCompat(t, e.Bytes(), tt.input)
})
}
})
}
t.Run("QuotesEscape", func(t *testing.T) {
const (
v = "\"/\""
)
var e Encoder
e.StrEscape(v)
requireCompat(t, e.Bytes(), v)
})
}
31 changes: 0 additions & 31 deletions string_test.go
@@ -1,12 +1,10 @@
package jx

import (
"encoding/hex"
"encoding/json"
"testing"

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

func TestReadString(t *testing.T) {
Expand Down Expand Up @@ -81,32 +79,3 @@ func testReadString(t *testing.T, input string, expectValue string, expectError
return
}
}

func TestEncoder_Str(t *testing.T) {
for _, tt := range []struct {
Name string
Input string
}{
{Name: "\\x00", Input: "\x00"},
{Name: "\\x00TrailingSpace", Input: "\x00 "},
{Name: "Hello", Input: `"hello, world!"`},
} {
t.Run(tt.Name, func(t *testing.T) {
s := GetEncoder()
t.Logf("%v", []rune(tt.Input))

s.Str(tt.Input)
t.Logf("%v", []rune(s.String()))

// Check `encoding/json` compatibility.
var gotStd string
require.NoError(t, json.Unmarshal(s.Bytes(), &gotStd))
require.Equal(t, tt.Input, gotStd)

i := DecodeBytes(s.Bytes())
got, err := i.Str()
require.NoError(t, err)
require.Equal(t, tt.Input, got, "%s\n%s", s, hex.Dump(s.Bytes()))
})
}
}

0 comments on commit 685fc6e

Please sign in to comment.