@@ -2,15 +2,22 @@ package jsonc
22
33import (
44 "bytes"
5+ "io"
6+ "reflect"
57 "testing"
8+ "testing/iotest"
69)
710
8- func ts (b []byte ) * Decoder { return & Decoder {r : bytes .NewBuffer (b )} }
11+ func ts (b []byte ) * Decoder { return & Decoder {r : bytes .NewBuffer (b )} }
12+ func tsErr (b []byte ) * Decoder { return & Decoder {r : iotest .DataErrReader (bytes .NewBuffer (b ))} }
913
1014var (
1115 validSingle = []byte (`{"foo": // this is a single line comment\n"bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com" }` )
1216 invalidSingle = []byte (`{"foo": // this is a single line comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com" }` )
1317
18+ validSingleESC = []byte ("{\" foo\" : // this is a single line comment\n \" bar foo\" , \" true\" : false, \" number\" : 42, \" object\" : { \" test\" : \" done\" }, \" array\" : [1, 2, 3], \" url\" : \" https://github.com\" }" )
19+ invalidSingleESC = []byte ("{\" foo\" : // this is a single line comment\" bar foo\" , \" true\" : false, \" number\" : 42, \" object\" : { \" test\" : \" done\" }, \" array\" : [1, 2, 3], \" url\" : \" https://github.com\" }" )
20+
1421 validBlock = []byte (`{"foo": /* this is a block comment */ "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com" }` )
1522 invalidBlock = []byte (`{"foo": /* this is a block comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com" }` )
1623)
@@ -42,6 +49,20 @@ func Test_Decoder_Read(t *testing.T) {
4249 want : 0 ,
4350 wantErr : true ,
4451 },
52+ {
53+ name : "Valid single line comment (escaped json)" ,
54+ d : ts (validSingleESC ),
55+ args : args {p : make ([]byte , len (validSingleESC ))},
56+ want : 110 , // (163(total) - 34(comments) - 19(spaces))
57+ wantErr : false ,
58+ },
59+ {
60+ name : "Invalid single line comment (escaped json)" ,
61+ d : ts (invalidSingleESC ),
62+ args : args {p : make ([]byte , len (invalidSingleESC ))},
63+ want : 0 ,
64+ wantErr : true ,
65+ },
4566 {
4667 name : "Valid block comment" ,
4768 d : ts (validBlock ),
@@ -56,6 +77,13 @@ func Test_Decoder_Read(t *testing.T) {
5677 want : 0 ,
5778 wantErr : true ,
5879 },
80+ {
81+ name : "Invalid Read" ,
82+ d : tsErr (validBlock ),
83+ args : args {p : make ([]byte , len (validBlock ))},
84+ want : 0 ,
85+ wantErr : true ,
86+ },
5987 }
6088
6189 for _ , tt := range tests {
@@ -71,3 +99,101 @@ func Test_Decoder_Read(t *testing.T) {
7199 })
72100 }
73101}
102+
103+ func TestNewDecoder (t * testing.T ) {
104+ type args struct {
105+ r io.Reader
106+ }
107+ tests := []struct {
108+ name string
109+ args args
110+ want * Decoder
111+ }{
112+ {
113+ name : "Valid Decoder" ,
114+ args : args {r : nil },
115+ want : & Decoder {},
116+ },
117+ }
118+ for _ , tt := range tests {
119+ t .Run (tt .name , func (t * testing.T ) {
120+ if got := NewDecoder (tt .args .r ); ! reflect .DeepEqual (got , tt .want ) {
121+ t .Errorf ("NewDecoder() = %v, want %v" , got , tt .want )
122+ }
123+ })
124+ }
125+ }
126+
127+ func TestDecodeBytes (t * testing.T ) {
128+ type args struct {
129+ p []byte
130+ }
131+ tests := []struct {
132+ name string
133+ args args
134+ want int
135+ wantErr bool
136+ }{
137+ {
138+ name : "Valid input" ,
139+ args : args {p : []byte (string (validBlock ))},
140+ want : 110 ,
141+ wantErr : false ,
142+ },
143+ {
144+ name : "Invalid input" ,
145+ args : args {p : []byte (string (invalidBlock ))},
146+ want : 0 ,
147+ wantErr : true ,
148+ },
149+ }
150+ for _ , tt := range tests {
151+ t .Run (tt .name , func (t * testing.T ) {
152+ got , err := DecodeBytes (tt .args .p )
153+ if (err != nil ) != tt .wantErr {
154+ t .Errorf ("DecodeBytes() error = %v, wantErr %v" , err , tt .wantErr )
155+ return
156+ }
157+ if got != tt .want {
158+ t .Errorf ("DecodeBytes() = %v, want %v" , got , tt .want )
159+ }
160+ })
161+ }
162+ }
163+
164+ func TestDecodeString (t * testing.T ) {
165+ type args struct {
166+ s string
167+ }
168+ tests := []struct {
169+ name string
170+ args args
171+ want string
172+ wantErr bool
173+ }{
174+ {
175+ name : "Valid input" ,
176+ args : args {s : string (validBlock )},
177+ want : `{"foo":"bar foo","true":false,"number":42,"object":{"test":"done"},"array":[1,2,3],"url":"https://github.com"}` ,
178+ wantErr : false ,
179+ },
180+ {
181+ name : "Invalid input" ,
182+ args : args {s : string (invalidBlock )},
183+ want : "" ,
184+ wantErr : true ,
185+ },
186+ }
187+ for _ , tt := range tests {
188+ t .Run (tt .name , func (t * testing.T ) {
189+ got , err := DecodeString (tt .args .s )
190+ if (err != nil ) != tt .wantErr {
191+ t .Errorf ("DecodeString() error = %v, wantErr %v" , err , tt .wantErr )
192+ return
193+ }
194+ if got != tt .want {
195+ t .Errorf ("DecodeString() = %v, want %v" , got , tt .want )
196+ }
197+ })
198+ }
199+ }
0 commit comments