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. "v2ray.com/core/common"
  8. . "v2ray.com/core/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. {`content`, `content`},
  24. {" ", " "},
  25. {`con/*abcd*/tent`, "content"},
  26. {`
  27. text // adlkhdf /*
  28. //comment adfkj
  29. text 2*/`, `
  30. text
  31. text 2*`},
  32. {`"//"content`, `"//"content`},
  33. {`abcd'//'abcd`, `abcd'//'abcd`},
  34. {`"\""`, `"\""`},
  35. {`\"/*abcd*/\"`, `\"\"`},
  36. }
  37. for _, testCase := range data {
  38. reader := &Reader{
  39. Reader: bytes.NewReader([]byte(testCase.input)),
  40. }
  41. actual := make([]byte, 1024)
  42. n, err := reader.Read(actual)
  43. common.Must(err)
  44. if r := cmp.Diff(string(actual[:n]), testCase.output); r != "" {
  45. t.Error(r)
  46. }
  47. }
  48. }
  49. func TestReader1(t *testing.T) {
  50. type dataStruct struct {
  51. input string
  52. output string
  53. }
  54. bufLen := 8
  55. data := []dataStruct{
  56. {"loooooooooooooooooooooooooooooooooooooooog", "loooooooooooooooooooooooooooooooooooooooog"},
  57. {`{"t": "\/testlooooooooooooooooooooooooooooong"}`, `{"t": "\/testlooooooooooooooooooooooooooooong"}`},
  58. {`{"t": "\/test"}`, `{"t": "\/test"}`},
  59. {`"\// fake comment"`, `"\// fake comment"`},
  60. {`"\/\/\/\/\/"`, `"\/\/\/\/\/"`},
  61. }
  62. for _, testCase := range data {
  63. reader := &Reader{
  64. Reader: bytes.NewReader([]byte(testCase.input)),
  65. }
  66. target := make([]byte, 0)
  67. buf := make([]byte, bufLen)
  68. var n int
  69. var err error
  70. for n, err = reader.Read(buf); err == nil; n, err = reader.Read(buf) {
  71. if n > len(buf) {
  72. t.Error("n: ", n)
  73. }
  74. target = append(target, buf[:n]...)
  75. buf = make([]byte, bufLen)
  76. }
  77. if err != nil && err != io.EOF {
  78. t.Error("error: ", err)
  79. }
  80. if string(target) != testCase.output {
  81. t.Error("got ", string(target), " want ", testCase.output)
  82. }
  83. }
  84. }