msgUtil_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package meta
  15. import (
  16. "bytes"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestConstructJsonArray(t *testing.T) {
  21. type args struct {
  22. jsonByteItems []fileContent
  23. }
  24. var buf1 bytes.Buffer
  25. buf1.Write([]byte("[]"))
  26. var buf2 bytes.Buffer
  27. buf2.Write([]byte(`[{"key": "key1"}]`))
  28. var buf3 bytes.Buffer
  29. buf3.Write([]byte(`[{"key1": "value1"},{"key2": "value2"}]`))
  30. tests := []struct {
  31. name string
  32. args args
  33. want bytes.Buffer
  34. }{
  35. {
  36. name: "no value",
  37. args: args{
  38. jsonByteItems: nil,
  39. },
  40. want: buf1,
  41. },
  42. {
  43. name: "one value",
  44. args: args{
  45. jsonByteItems: []fileContent{
  46. []byte(`{"key": "key1"}`),
  47. },
  48. },
  49. want: buf2,
  50. },
  51. {
  52. name: "two value",
  53. args: args{
  54. jsonByteItems: []fileContent{
  55. []byte(`{"key1": "value1"}`),
  56. []byte(`{"key2": "value2"}`),
  57. },
  58. },
  59. want: buf3,
  60. },
  61. }
  62. for _, tt := range tests {
  63. t.Run(tt.name, func(t *testing.T) {
  64. if got := ConstructJsonArray(tt.args.jsonByteItems); !reflect.DeepEqual(got, tt.want) {
  65. t.Errorf("ConstructJsonArray() = %v, want %v", got, tt.want)
  66. }
  67. })
  68. }
  69. }