reader_test.go 1.9 KB

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