reader_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/v5/common"
  8. . "github.com/v2fly/v2ray-core/v5/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 := 1
  56. data := []dataStruct{
  57. {"loooooooooooooooooooooooooooooooooooooooog", "loooooooooooooooooooooooooooooooooooooooog"},
  58. {`{"t": "\/testlooooooooooooooooooooooooooooong"}`, `{"t": "\/testlooooooooooooooooooooooooooooong"}`},
  59. {`{"t": "\/test"}`, `{"t": "\/test"}`},
  60. {`"\// fake comment"`, `"\// fake comment"`},
  61. {`"\/\/\/\/\/"`, `"\/\/\/\/\/"`},
  62. {`/test/test`, `/test/test`},
  63. }
  64. for _, testCase := range data {
  65. reader := &Reader{
  66. Reader: bytes.NewReader([]byte(testCase.input)),
  67. }
  68. target := make([]byte, 0)
  69. buf := make([]byte, bufLen)
  70. var n int
  71. var err error
  72. for n, err = reader.Read(buf); err == nil; n, err = reader.Read(buf) {
  73. if n > len(buf) {
  74. t.Error("n: ", n)
  75. }
  76. target = append(target, buf[:n]...)
  77. buf = make([]byte, bufLen)
  78. }
  79. if err != nil && err != io.EOF {
  80. t.Error("error: ", err)
  81. }
  82. if string(target) != testCase.output {
  83. t.Error("got ", string(target), " want ", testCase.output)
  84. }
  85. }
  86. }