1 package jreader
2
3 import (
4 "encoding/json"
5 "errors"
6 "reflect"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestSyntaxError(t *testing.T) {
13 e1 := SyntaxError{Message: "xyz", Offset: 2}
14 assert.Equal(t, "xyz at position 2", e1.Error())
15
16 e2 := SyntaxError{Message: "xyz", Offset: 2, Value: "abc"}
17 assert.Equal(t, `xyz at position 2 ("abc")`, e2.Error())
18 }
19
20 func TestTypeError(t *testing.T) {
21 assert.Equal(t, "expected boolean, got string at position 2",
22 TypeError{Expected: BoolValue, Actual: StringValue, Offset: 2}.Error())
23
24 assert.Equal(t, "expected boolean or null, got string at position 2",
25 TypeError{Expected: BoolValue, Actual: StringValue, Offset: 2, Nullable: true}.Error())
26
27 assert.Equal(t, "expected null, got boolean at position 2",
28 TypeError{Expected: NullValue, Actual: BoolValue, Offset: 2}.Error())
29
30 assert.Equal(t, "expected null, got number at position 2",
31 TypeError{Expected: NullValue, Actual: NumberValue, Offset: 2}.Error())
32
33 assert.Equal(t, "expected null, got string at position 2",
34 TypeError{Expected: NullValue, Actual: StringValue, Offset: 2}.Error())
35
36 assert.Equal(t, "expected null, got array at position 2",
37 TypeError{Expected: NullValue, Actual: ArrayValue, Offset: 2}.Error())
38
39 assert.Equal(t, "expected null, got object at position 2",
40 TypeError{Expected: NullValue, Actual: ObjectValue, Offset: 2}.Error())
41
42 assert.Equal(t, "expected null, got unknown token at position 2",
43 TypeError{Expected: NullValue, Actual: 99, Offset: 2}.Error())
44 }
45
46 func TestToJSONError(t *testing.T) {
47 e1 := SyntaxError{Message: "xyz", Offset: 2}
48 je1 := ToJSONError(e1, nil)
49 assert.Equal(t, &json.SyntaxError{Offset: 2}, je1)
50
51 e2 := TypeError{Expected: NumberValue, Actual: StringValue, Offset: 2}
52 someIntValue := 1000
53 je2 := ToJSONError(e2, someIntValue)
54 assert.Equal(t, &json.UnmarshalTypeError{Value: "number", Offset: 2, Type: reflect.TypeOf(someIntValue)}, je2)
55
56 e3 := errors.New("some other error")
57 assert.Equal(t, e3, ToJSONError(e3, nil))
58 }
59
View as plain text