provider_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package util
  13. import (
  14. "encoding/json"
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestParameterTagsToJSONString(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. tags map[string]string
  22. expected string
  23. wantErr bool
  24. }{
  25. {
  26. name: "Valid tags",
  27. tags: map[string]string{
  28. "key1": "value1",
  29. "key2": "value2",
  30. },
  31. expected: `{"key1":"value1","key2":"value2"}`,
  32. wantErr: false,
  33. },
  34. {
  35. name: "Empty tags",
  36. tags: map[string]string{},
  37. expected: `{}`,
  38. wantErr: false,
  39. },
  40. {
  41. name: "Nil tags",
  42. tags: nil,
  43. wantErr: false,
  44. expected: "null",
  45. },
  46. }
  47. for _, tt := range tests {
  48. t.Run(tt.name, func(t *testing.T) {
  49. result, err := ParameterTagsToJSONString(tt.tags)
  50. if tt.wantErr {
  51. assert.Error(t, err)
  52. } else {
  53. assert.NoError(t, err)
  54. var resultMap map[string]string
  55. err := json.Unmarshal([]byte(result), &resultMap)
  56. assert.NoError(t, err)
  57. assert.Equal(t, tt.expected, result)
  58. }
  59. })
  60. }
  61. }