pax_global_header00006660000000000000000000000064147377631060014530gustar00rootroot0000000000000052 comment=f59850696995f0a381ed64d53c1ba343a3f066d0 go-testifyjson-1.3.0/000077500000000000000000000000001473776310600145155ustar00rootroot00000000000000go-testifyjson-1.3.0/LICENSE000066400000000000000000000027431473776310600155300ustar00rootroot00000000000000 Copyright (c) 2022, Cristian Maglie. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. go-testifyjson-1.3.0/README.md000066400000000000000000000017641473776310600160040ustar00rootroot00000000000000# `go.bug.st/testifyjson/requirejson` - unit-test JSON output in golang. Package testifyjson is a collection of utilities and helper function for unit testing JSON output in golang. It is based on the excellent libraries `github.com/itchyny/gojq` and `github.com/stretchr/testify`. It provides an interface similar to `testify` but with the powerful methods available in `gojq`. ## Examples ```go import ( "testing" "go.bug.st/testifyjson/requirejson" ) func TestJSONQuery(t *testing.T) { in := []byte(` { "id" : 1, "list" : [ 10, 20, 30 ], "emptylist" : [] } `) requirejson.Query(t, in, ".list", "[10, 20, 30]") requirejson.Query(t, in, ".list.[1]", "20") requirejson.Contains(t, in, `{ "list": [ 30 ] }`) requirejson.NotContains(t, in, `{ "list": [ 50 ] }`) in2 := []byte(`[ ]`) requirejson.Empty(t, in2) requirejson.Len(t, in2, 0) in3 := []byte(`[ 10, 20, 30 ]`) requirejson.NotEmpty(t, in3) requirejson.Len(t, in3, 3) } ``` go-testifyjson-1.3.0/doc.go000066400000000000000000000002731473776310600156130ustar00rootroot00000000000000// // Copyright 2022 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package testifyjson go-testifyjson-1.3.0/go.mod000066400000000000000000000004751473776310600156310ustar00rootroot00000000000000module go.bug.st/testifyjson go 1.19 require ( github.com/itchyny/gojq v0.12.8 github.com/stretchr/testify v1.8.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/itchyny/timefmt-go v0.1.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) go-testifyjson-1.3.0/go.sum000066400000000000000000000044751473776310600156620ustar00rootroot00000000000000github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/itchyny/gojq v0.12.8 h1:Zxcwq8w4IeR8JJYEtoG2MWJZUv0RGY6QqJcO1cqV8+A= github.com/itchyny/gojq v0.12.8/go.mod h1:gE2kZ9fVRU0+JAksaTzjIlgnCa2akU+a1V0WXgJQN5c= github.com/itchyny/timefmt-go v0.1.3 h1:7M3LGVDsqcd0VZH2U+x393obrzZisp7C0uEe921iRkU= github.com/itchyny/timefmt-go v0.1.3/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= go-testifyjson-1.3.0/requirejson/000077500000000000000000000000001473776310600170635ustar00rootroot00000000000000go-testifyjson-1.3.0/requirejson/json.go000066400000000000000000000155301473776310600203670ustar00rootroot00000000000000// // Copyright 2022 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package requirejson import ( "encoding/json" "fmt" "os" "testing" "github.com/itchyny/gojq" "github.com/stretchr/testify/require" ) // JQObject is an object that abstract operations on JSON data. type JQObject struct { t *testing.T data interface{} } func (obj *JQObject) String() string { r, err := json.Marshal(obj.data) require.NoError(obj.t, err) return string(r) } // Parse creates a new JQObect from the given jsonData. // If jsonData is not a valid json the test fails. func Parse(t *testing.T, jsonData []byte, msgAndArgs ...interface{}) *JQObject { var data interface{} require.NoError(t, json.Unmarshal(jsonData, &data), msgAndArgs...) return &JQObject{t: t, data: data} } // ParseFromFile creates a new JQObect from the given file path. // If the file is not a valid json the test fails. func ParseFromFile(t *testing.T, filePath string, msgAndArgs ...interface{}) *JQObject { var data interface{} jsonData, err := os.ReadFile(filePath) require.NoError(t, err, msgAndArgs...) require.NoError(t, json.Unmarshal(jsonData, &data), msgAndArgs...) return &JQObject{t: t, data: data} } // Query performs a query on the given JQObject and returns the first result. If the query // produces no result the test will fail. func (obj *JQObject) Query(jqQuery string) *JQObject { q, err := gojq.Parse(jqQuery) require.NoError(obj.t, err) iter := q.Run(obj.data) data, ok := iter.Next() require.True(obj.t, ok, "The json query produced no results") return &JQObject{t: obj.t, data: data} } // MustEqual tests if the JQObject equals jsonExpected. If the check // is not successful the test will fail. If msgAndArgs are provided they // will be used to explain the error. func (obj *JQObject) MustEqual(jsonExpected string, msgAndArgs ...interface{}) { jsonActual, err := json.Marshal(obj.data) require.NoError(obj.t, err) require.JSONEq(obj.t, jsonExpected, string(jsonActual), msgAndArgs...) } // MustContain tests if jsonExpected is a subset of the JQObject. If the check // is not successful the test will fail. If msgAndArgs are provided they // will be used to explain the error. func (obj *JQObject) MustContain(jsonExpected string, msgAndArgs ...interface{}) { v := obj.Query("contains(" + jsonExpected + ")").data require.IsType(obj.t, true, v) if !v.(bool) { msg := fmt.Sprintf("json data does not contain: %s", jsonExpected) require.FailNow(obj.t, msg, msgAndArgs...) } } // MustNotContain tests if the JQObject does not contain jsonExpected. If the check // is not successful the test will fail. If msgAndArgs are provided they // will be used to explain the error. func (obj *JQObject) MustNotContain(jsonExpected string, msgAndArgs ...interface{}) { v := obj.Query("contains(" + jsonExpected + ")").data require.IsType(obj.t, true, v) if v.(bool) { msg := fmt.Sprintf("json data contains: %s", jsonExpected) require.FailNow(obj.t, msg, msgAndArgs...) } } // LengthMustEqualTo tests if the size of JQObject match expectedLen. If the check // is not successful the test will fail. If msgAndArgs are provided they // will be used to explain the error. func (obj *JQObject) LengthMustEqualTo(expectedLen int, msgAndArgs ...interface{}) { v := obj.Query("length").data require.IsType(obj.t, expectedLen, v) if v.(int) != expectedLen { msg := fmt.Sprintf("json data length does not match: expected=%d, actual=%d", expectedLen, v.(int)) require.FailNow(obj.t, msg, msgAndArgs...) } } // MustBeEmpty test if the size of JQObject is 0. If the check // is not successful the test will fail. If msgAndArgs are provided they // will be used to explain the error. func (obj *JQObject) MustBeEmpty(msgAndArgs ...interface{}) { v := obj.Query("length").data require.IsType(obj.t, 0, v) if v.(int) != 0 { require.FailNow(obj.t, "json data is not empty", msgAndArgs...) } } // MustNotBeEmpty test if the size of JQObject is not 0. If the check // is not successful the test will fail. If msgAndArgs are provided they // will be used to explain the error. func (obj *JQObject) MustNotBeEmpty(msgAndArgs ...interface{}) { v := obj.Query("length").data require.IsType(obj.t, 0, v) if v.(int) == 0 { require.FailNow(obj.t, "json data is empty", msgAndArgs...) } } // IsTrue test if the JQObject is `true`. func (obj *JQObject) IsTrue(msgAndArgs ...interface{}) { obj.MustEqual("true", msgAndArgs...) } // IsFalse test if the JQObject is `false`. func (obj *JQObject) IsFalse(msgAndArgs ...interface{}) { obj.MustEqual("false", msgAndArgs...) } // ArrayMustContain checks if the give JQObject contains the given element. func (obj *JQObject) ArrayMustContain(jsonElement string, msgAndArgs ...interface{}) { obj.Query(`any(. == ` + jsonElement + `)`).IsTrue(msgAndArgs...) } // Query performs a test on a given json output. A jq-like query is performed // on the given jsonData and the result is compared with jsonExpected. // If the output doesn't match the test fails. If msgAndArgs are provided they // will be used to explain the error. func Query(t *testing.T, jsonData []byte, jqQuery string, jsonExpected string, msgAndArgs ...interface{}) { data := Parse(t, jsonData) v := data.Query(jqQuery) v.MustEqual(jsonExpected, msgAndArgs...) } // Contains check if the json object is a subset of the jsonData. // If the output doesn't match the test fails. If msgAndArgs are provided they // will be used to explain the error. func Contains(t *testing.T, jsonData []byte, jsonObject string, msgAndArgs ...interface{}) { Parse(t, jsonData).MustContain(jsonObject, msgAndArgs...) } // NotContains check if the json object is NOT a subset of the jsonData. // If the output match the test fails. If msgAndArgs are provided they // will be used to explain the error. func NotContains(t *testing.T, jsonData []byte, jsonObject string, msgAndArgs ...interface{}) { Parse(t, jsonData).MustNotContain(jsonObject, msgAndArgs...) } // Len check if the size of the json object match the given value. // If the lenght doesn't match the test fails. If msgAndArgs are provided they // will be used to explain the error. func Len(t *testing.T, jsonData []byte, expectedLen int, msgAndArgs ...interface{}) { Parse(t, jsonData).LengthMustEqualTo(expectedLen, msgAndArgs...) } // Empty check if the size of the json object is zero. // If the lenght is not zero the test fails. If msgAndArgs are provided they // will be used to explain the error. func Empty(t *testing.T, jsonData []byte, msgAndArgs ...interface{}) { Parse(t, jsonData).MustBeEmpty(msgAndArgs...) } // NotEmpty check if the size of the json object is not zero. // If the lenght is zero the test fails. If msgAndArgs are provided they // will be used to explain the error. func NotEmpty(t *testing.T, jsonData []byte, msgAndArgs ...interface{}) { Parse(t, jsonData).MustNotBeEmpty(msgAndArgs...) } go-testifyjson-1.3.0/requirejson/json_test.go000066400000000000000000000036151473776310600214270ustar00rootroot00000000000000// // Copyright 2022 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package requirejson_test import ( "os" "testing" "github.com/stretchr/testify/require" "go.bug.st/testifyjson/requirejson" ) func TestJSONFlowParadigm(t *testing.T) { in := requirejson.Parse(t, []byte(` { "id" : 1, "list" : [ 10, 20, 30 ], "emptylist" : [] } `)) in.Query(".list").MustEqual("[10, 20, 30]") in.Query(".list.[1]").MustEqual("20") in.MustContain(`{ "list": [ 30 ] }`) in.MustNotContain(`{ "list": [ 50 ] }`) in.Query(".list | length").MustEqual("3") in2 := requirejson.Parse(t, []byte(`[ ]`)) in2.MustBeEmpty() in2.LengthMustEqualTo(0) in3 := requirejson.Parse(t, []byte(`[ 10, 20, 30 ]`)) in3.MustNotBeEmpty() in3.LengthMustEqualTo(3) } func TestJSONAssertions(t *testing.T) { in := []byte(` { "id" : 1, "list" : [ 10, 20, 30 ], "emptylist" : [] } `) requirejson.Query(t, in, ".list", "[10, 20, 30]") requirejson.Query(t, in, ".list.[1]", "20") requirejson.Contains(t, in, `{ "list": [ 30 ] }`) requirejson.NotContains(t, in, `{ "list": [ 50 ] }`) in2 := []byte(`[ ]`) requirejson.Empty(t, in2) requirejson.Len(t, in2, 0) in3 := []byte(`[ 10, 20, 30 ]`) requirejson.NotEmpty(t, in3) requirejson.Len(t, in3, 3) requirejson.Query(t, in, ".list | length", "3") requirejson.Parse(t, in).Query(".list").ArrayMustContain("20") } func TestParseFromFile(t *testing.T) { in := []byte(` { "id" : 1, "list" : [ 10, 20, 30 ] } `) tmpDir := t.TempDir() testFile, err := os.CreateTemp(tmpDir, "test.json") require.NoError(t, err) _, err = testFile.Write(in) require.NoError(t, err) require.NoError(t, testFile.Close()) obj := requirejson.ParseFromFile(t, testFile.Name()) obj.Query(".id").MustEqual("1") obj.Query(".list").ArrayMustContain("20") obj.Query(".list | length").MustEqual("3") }