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