|
4 | 4 | "encoding/json" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
| 7 | + "math" |
7 | 8 | "reflect" |
8 | 9 | "strings" |
9 | 10 | "testing" |
@@ -883,6 +884,54 @@ func TestJSONQ_Count_expecting_int_from_objects(t *testing.T) { |
883 | 884 | assertJSON(t, out, expected, "Count expecting a int number of total item of an array of grouped objects") |
884 | 885 | } |
885 | 886 |
|
| 887 | +func TestJSONQ_Out_expecting_result(t *testing.T) { |
| 888 | + type item struct { |
| 889 | + ID int `json:"id"` |
| 890 | + Name string `json:"name"` |
| 891 | + Price int `json:"price"` |
| 892 | + } |
| 893 | + exptItm := item{ |
| 894 | + ID: 1, |
| 895 | + Name: "MacBook Pro 13 inch retina", |
| 896 | + Price: 1350, |
| 897 | + } |
| 898 | + itm := item{} |
| 899 | + jq := New().JSONString(jsonStr). |
| 900 | + From("vendor.items.[0]") |
| 901 | + jq.Out(&itm) |
| 902 | + assertInterface(t, exptItm, itm, "failed to get Out result") |
| 903 | +} |
| 904 | + |
| 905 | +func TestJSONQ_Out_expecting_decoding_error(t *testing.T) { |
| 906 | + type item struct { |
| 907 | + ID bool `json:"id"` |
| 908 | + Name string `json:"name"` |
| 909 | + Price int `json:"price"` |
| 910 | + } |
| 911 | + itm := item{} |
| 912 | + jq := New().JSONString(jsonStr). |
| 913 | + From("vendor.items.[0]") |
| 914 | + jq.Out(&itm) |
| 915 | + if jq.Error() == nil { |
| 916 | + t.Errorf("failed to get Out decoding error: %v", jq.Error()) |
| 917 | + } |
| 918 | +} |
| 919 | + |
| 920 | +func TestJSONQ_Out_expecting_encoding_error(t *testing.T) { |
| 921 | + type item struct { |
| 922 | + ID bool `json:"id"` |
| 923 | + Name string `json:"name"` |
| 924 | + Price int `json:"price"` |
| 925 | + } |
| 926 | + itm := item{} |
| 927 | + jq := New() |
| 928 | + jq.jsonContent = math.Inf(1) |
| 929 | + jq.Out(&itm) |
| 930 | + if jq.Error() == nil { |
| 931 | + t.Errorf("failed to get Out encoding error: %v", jq.Error()) |
| 932 | + } |
| 933 | +} |
| 934 | + |
886 | 935 | func TestJSONQ_Sum_of_array_numeric_values(t *testing.T) { |
887 | 936 | jq := New().JSONString(jsonStr). |
888 | 937 | From("vendor.prices") |
|
0 commit comments