1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
| package json_test
|
| import (
| "bytes"
| "io"
| "testing"
|
| "github.com/google/go-cmp/cmp"
|
| "github.com/v2fly/v2ray-core/v5/common"
| . "github.com/v2fly/v2ray-core/v5/infra/conf/json"
| )
|
| func TestReader(t *testing.T) {
| data := []struct {
| input string
| output string
| }{
| {
| `
| content #comment 1
| #comment 2
| content 2`,
| `
| content
|
| content 2`,
| },
| {`content`, `content`},
| {" ", " "},
| {`con/*abcd*/tent`, "content"},
| {`
| text // adlkhdf /*
| //comment adfkj
| text 2*/`, `
| text
|
| text 2*`},
| {`"//"content`, `"//"content`},
| {`abcd'//'abcd`, `abcd'//'abcd`},
| {`"\""`, `"\""`},
| {`\"/*abcd*/\"`, `\"\"`},
| }
|
| for _, testCase := range data {
| reader := &Reader{
| Reader: bytes.NewReader([]byte(testCase.input)),
| }
|
| actual := make([]byte, 1024)
| n, err := reader.Read(actual)
| common.Must(err)
| if r := cmp.Diff(string(actual[:n]), testCase.output); r != "" {
| t.Error(r)
| }
| }
| }
|
| func TestReader1(t *testing.T) {
| type dataStruct struct {
| input string
| output string
| }
|
| bufLen := 8
|
| data := []dataStruct{
| {"loooooooooooooooooooooooooooooooooooooooog", "loooooooooooooooooooooooooooooooooooooooog"},
| {`{"t": "\/testlooooooooooooooooooooooooooooong"}`, `{"t": "\/testlooooooooooooooooooooooooooooong"}`},
| {`{"t": "\/test"}`, `{"t": "\/test"}`},
| {`"\// fake comment"`, `"\// fake comment"`},
| {`"\/\/\/\/\/"`, `"\/\/\/\/\/"`},
| }
|
| for _, testCase := range data {
| reader := &Reader{
| Reader: bytes.NewReader([]byte(testCase.input)),
| }
| target := make([]byte, 0)
| buf := make([]byte, bufLen)
| var n int
| var err error
| for n, err = reader.Read(buf); err == nil; n, err = reader.Read(buf) {
| if n > len(buf) {
| t.Error("n: ", n)
| }
| target = append(target, buf[:n]...)
| buf = make([]byte, bufLen)
| }
| if err != nil && err != io.EOF {
| t.Error("error: ", err)
| }
| if string(target) != testCase.output {
| t.Error("got ", string(target), " want ", testCase.output)
| }
| }
| }
|
|