(t *testing.T)
| 819 | } |
| 820 | |
| 821 | func TestInjectJSONProperties(t *testing.T) { |
| 822 | newKV := KVPair{ |
| 823 | Key: "newval", |
| 824 | Val: 123, |
| 825 | } |
| 826 | |
| 827 | tests := []struct { |
| 828 | input string |
| 829 | expectedOutput string |
| 830 | expectedErr string |
| 831 | }{ |
| 832 | { |
| 833 | input: ``, |
| 834 | expectedErr: `not a JSON object`, |
| 835 | }, |
| 836 | { |
| 837 | input: `null`, |
| 838 | expectedErr: `not a JSON object`, |
| 839 | }, |
| 840 | { |
| 841 | input: "123", |
| 842 | expectedErr: `not a JSON object`, |
| 843 | }, |
| 844 | { |
| 845 | input: "{}", |
| 846 | expectedOutput: `{"newval":123}`, |
| 847 | }, |
| 848 | { |
| 849 | input: `{"key":"val"}`, |
| 850 | expectedOutput: `{"key":"val","newval":123}`, |
| 851 | }, |
| 852 | { |
| 853 | input: `{"newval":"old"}`, |
| 854 | expectedOutput: `{"newval":"old","newval":123}`, |
| 855 | }, |
| 856 | { |
| 857 | input: ` {"key":"val"} `, |
| 858 | expectedOutput: `{"key":"val","newval":123}`, |
| 859 | }, |
| 860 | } |
| 861 | |
| 862 | for _, test := range tests { |
| 863 | t.Run(test.input, func(tt *testing.T) { |
| 864 | output, err := InjectJSONProperties([]byte(test.input), newKV) |
| 865 | if test.expectedErr != "" { |
| 866 | require.Errorf(tt, err, test.expectedErr, "expected error did not match") |
| 867 | return |
| 868 | } else { |
| 869 | require.NoError(tt, err, "unexpected error") |
| 870 | } |
| 871 | |
| 872 | assert.Equal(tt, test.expectedOutput, string(output)) |
| 873 | assert.NoError(tt, JSONUnmarshal(output, &map[string]interface{}{}), "produced invalid JSON") |
| 874 | }) |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | func TestInjectJSONPropertiesDiffTypes(t *testing.T) { |
nothing calls this directly
no test coverage detected