github.com/imannamdari/v2ray-core/v5@v5.0.5/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/imannamdari/v2ray-core/v5/common" 11 . "github.com/imannamdari/v2ray-core/v5/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 := 1 66 67 data := []dataStruct{ 68 {"loooooooooooooooooooooooooooooooooooooooog", "loooooooooooooooooooooooooooooooooooooooog"}, 69 {`{"t": "\/testlooooooooooooooooooooooooooooong"}`, `{"t": "\/testlooooooooooooooooooooooooooooong"}`}, 70 {`{"t": "\/test"}`, `{"t": "\/test"}`}, 71 {`"\// fake comment"`, `"\// fake comment"`}, 72 {`"\/\/\/\/\/"`, `"\/\/\/\/\/"`}, 73 {`/test/test`, `/test/test`}, 74 } 75 76 for _, testCase := range data { 77 reader := &Reader{ 78 Reader: bytes.NewReader([]byte(testCase.input)), 79 } 80 target := make([]byte, 0) 81 buf := make([]byte, bufLen) 82 var n int 83 var err error 84 for n, err = reader.Read(buf); err == nil; n, err = reader.Read(buf) { 85 if n > len(buf) { 86 t.Error("n: ", n) 87 } 88 target = append(target, buf[:n]...) 89 buf = make([]byte, bufLen) 90 } 91 if err != nil && err != io.EOF { 92 t.Error("error: ", err) 93 } 94 if string(target) != testCase.output { 95 t.Error("got ", string(target), " want ", testCase.output) 96 } 97 } 98 }