Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions internal/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ func TestStart_FastPath_InvalidJSON(t *testing.T) {
assert.Len(t, errs, 1, "Expected one error message")
}

func TestStart_EmptyInput(t *testing.T) {
tests := []struct {
name string
input string
}{
{"empty", ""},
{"whitespace", " "},
{"newlines", "\n\t\n"},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
parser := jsonx.NewJsonParser(strings.NewReader(tc.input), false)

var errs []string
opts := engine.Options{
WriteOut: func(s string) {},
WriteErr: func(s string) { errs = append(errs, s) },
}
exitCode := engine.Start(parser, []string{"."}, opts)

assert.Equal(t, 1, exitCode)
assert.Len(t, errs, 1)
})
}
}

func TestStart_EscapeSequences(t *testing.T) {
input := `{"emoji": "\ud83d\ude80"}`
parser := jsonx.NewJsonParser(strings.NewReader(input), false)
Expand Down
9 changes: 5 additions & 4 deletions internal/jsonx/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ func (p *JsonParser) Parse() (node *Node, err error) {
err = p.errorSnippet(fmt.Sprintf("%v", r))
}
}()
if p.count > 0 {
p.skipWhitespace()
}
p.skipWhitespace()
if p.eof {
if p.count == 0 {
return nil, fmt.Errorf("empty input")
}
return nil, io.EOF
}
node = p.parseValue(true)
Expand All @@ -79,7 +80,7 @@ func (p *JsonParser) Recover() *Node {
}

end := p.end - 1
if p.data[end-1] == '\n' {
if end > 0 && p.data[end-1] == '\n' {
end-- // Trim trailing newline.
}

Expand Down
3 changes: 3 additions & 0 deletions internal/jsonx/jsonx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func TestJsonParser_Parse_error(t *testing.T) {
tests := []struct {
input string
}{
{``},
{` `},
{"\n\t\n"},
{`"abc`},
{`truth`},
{`1e`},
Expand Down