values_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package json
  2. import (
  3. "reflect"
  4. "testing"
  5. "gogs.baozhida.cn/zoie/OAuth-core/config/source"
  6. )
  7. func TestValues(t *testing.T) {
  8. emptyStr := ""
  9. testData := []struct {
  10. csdata []byte
  11. path []string
  12. accepter interface{}
  13. value interface{}
  14. }{
  15. {
  16. []byte(`{"foo": "bar", "baz": {"bar": "cat"}}`),
  17. []string{"foo"},
  18. emptyStr,
  19. "bar",
  20. },
  21. {
  22. []byte(`{"foo": "bar", "baz": {"bar": "cat"}}`),
  23. []string{"baz", "bar"},
  24. emptyStr,
  25. "cat",
  26. },
  27. }
  28. for idx, test := range testData {
  29. values, err := newValues(&source.ChangeSet{
  30. Data: test.csdata,
  31. })
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. err = values.Get(test.path...).Scan(&test.accepter)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if test.accepter != test.value {
  40. t.Fatalf("No.%d Expected %v got %v for path %v", idx, test.value, test.accepter, test.path)
  41. }
  42. }
  43. }
  44. func TestStructArray(t *testing.T) {
  45. type T struct {
  46. Foo string
  47. }
  48. emptyTSlice := []T{}
  49. testData := []struct {
  50. csdata []byte
  51. accepter []T
  52. value []T
  53. }{
  54. {
  55. []byte(`[{"foo": "bar"}]`),
  56. emptyTSlice,
  57. []T{{Foo: "bar"}},
  58. },
  59. }
  60. for idx, test := range testData {
  61. values, err := newValues(&source.ChangeSet{
  62. Data: test.csdata,
  63. })
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. err = values.Get().Scan(&test.accepter)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if !reflect.DeepEqual(test.accepter, test.value) {
  72. t.Fatalf("No.%d Expected %v got %v", idx, test.value, test.accepter)
  73. }
  74. }
  75. }