Skip to content

Commit

Permalink
Add more tests for special characters
Browse files Browse the repository at this point in the history
Test behavior when dealing with non-printing characters with and without
2-character escape sequences.
  • Loading branch information
ashmrtn committed Jan 8, 2024
1 parent b9ef405 commit 7833daf
Showing 1 changed file with 196 additions and 0 deletions.
196 changes: 196 additions & 0 deletions json_serialization_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,202 @@ func TestEscapeTabAndCarriageReturnInStrings(t *testing.T) {
assert.Equal(t, expected, converted)
}

// TestShortEscapeSequencesInString tests that strings containing characters
// with 2-character escape sequences according to RFC 8259 section 7 are
// properly encoded as JSON.
func TestShortEscapeSequencesInString(t *testing.T) {
// Expected results for each test are quoted since it's a JSON string.
table := []struct {
input byte
expected []byte
}{
{
input: 0x22, // " character
expected: []byte(`"\""`),
},
{
input: 0x5c, // \ character
expected: []byte(`"\\"`),
},
{
input: 0x08, // backspace character
expected: []byte(`"\b"`),
},
{
input: 0x0c, // form feed character
expected: []byte(`"\f"`),
},
{
input: 0x0a, // line feed character
expected: []byte(`"\n"`),
},
{
input: 0x0d, // carriage return character
expected: []byte(`"\r"`),
},
{
input: 0x09, // tab character
expected: []byte(`"\t"`),
},
}

for _, test := range table {
t.Run(fmt.Sprintf("%02X", test.input), func(t *testing.T) {
stringInput := string(test.input)

serializer := NewJsonSerializationWriter()
err := serializer.WriteStringValue("", &stringInput)
assert.NoError(t, err)

result, err := serializer.GetSerializedContent()
assert.NoError(t, err)

assert.Equal(t, test.expected, result)

assert.True(t, json.Valid(result), "valid JSON")
})
}
}

// TestLongEscapeSequencesInString tests that strings containing characters
// without 2-character escape sequences according to RFC 8259 section 7 are
// properly encoded as JSON.
func TestLongEscapeSequencesInString(t *testing.T) {
// Manually adding these expected results since the code to generate them with
// a loop would be pretty similar to the code to generate the escape sequences
// which could make it susceptible to similar logic errors.
table := []struct {
input byte
expected []byte
}{
{
input: 0x00,
expected: []byte(`"\u0000"`),
},
{
input: 0x01,
expected: []byte(`"\u0001"`),
},
{
input: 0x02,
expected: []byte(`"\u0002"`),
},
{
input: 0x03,
expected: []byte(`"\u0003"`),
},
{
input: 0x04,
expected: []byte(`"\u0004"`),
},
{
input: 0x05,
expected: []byte(`"\u0005"`),
},
{
input: 0x06,
expected: []byte(`"\u0006"`),
},
{
input: 0x07,
expected: []byte(`"\u0007"`),
},
{
input: 0x0b,
expected: []byte(`"\u000B"`),
},
{
input: 0x0e,
expected: []byte(`"\u000E"`),
},
{
input: 0x0f,
expected: []byte(`"\u000F"`),
},
{
input: 0x10,
expected: []byte(`"\u0010"`),
},
{
input: 0x11,
expected: []byte(`"\u0011"`),
},
{
input: 0x12,
expected: []byte(`"\u0012"`),
},
{
input: 0x13,
expected: []byte(`"\u0013"`),
},
{
input: 0x14,
expected: []byte(`"\u0014"`),
},
{
input: 0x15,
expected: []byte(`"\u0015"`),
},
{
input: 0x16,
expected: []byte(`"\u0016"`),
},
{
input: 0x17,
expected: []byte(`"\u0017"`),
},
{
input: 0x18,
expected: []byte(`"\u0018"`),
},
{
input: 0x19,
expected: []byte(`"\u0019"`),
},
{
input: 0x1a,
expected: []byte(`"\u001A"`),
},
{
input: 0x1b,
expected: []byte(`"\u001B"`),
},
{
input: 0x1c,
expected: []byte(`"\u001C"`),
},
{
input: 0x1d,
expected: []byte(`"\u001D"`),
},
{
input: 0x1e,
expected: []byte(`"\u001E"`),
},
{
input: 0x1f,
expected: []byte(`"\u001F"`),
},
}

for _, test := range table {
t.Run(fmt.Sprintf("%02X", test.input), func(t *testing.T) {
stringInput := string(test.input)

serializer := NewJsonSerializationWriter()
err := serializer.WriteStringValue("", &stringInput)
assert.NoError(t, err)

result, err := serializer.GetSerializedContent()
assert.NoError(t, err)

assert.Equal(t, test.expected, result)

assert.True(t, json.Valid(result), "valid JSON")
})
}
}

func TestWriteValuesConcurrently(t *testing.T) {
instances := 100
output := make([][]byte, instances)
Expand Down

0 comments on commit 7833daf

Please sign in to comment.