...

Source file src/github.com/ory/x/assertx/assertx.go

Documentation: github.com/ory/x/assertx

     1  package assertx
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/tidwall/sjson"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func PrettifyJSONPayload(t *testing.T, payload interface{}) string {
    15  	o, err := json.MarshalIndent(payload, "", "  ")
    16  	require.NoError(t, err)
    17  	return string(o)
    18  }
    19  
    20  func EqualAsJSON(t *testing.T, expected, actual interface{}, args ...interface{}) {
    21  	var eb, ab bytes.Buffer
    22  	if len(args) == 0 {
    23  		args = []interface{}{PrettifyJSONPayload(t, actual)}
    24  	}
    25  
    26  	require.NoError(t, json.NewEncoder(&eb).Encode(expected), args...)
    27  	require.NoError(t, json.NewEncoder(&ab).Encode(actual), args...)
    28  	assert.JSONEq(t, eb.String(), ab.String(), args...)
    29  }
    30  
    31  func EqualAsJSONExcept(t *testing.T, expected, actual interface{}, except []string, args ...interface{}) {
    32  	var eb, ab bytes.Buffer
    33  	if len(args) == 0 {
    34  		args = []interface{}{PrettifyJSONPayload(t, actual)}
    35  	}
    36  
    37  	require.NoError(t, json.NewEncoder(&eb).Encode(expected), args...)
    38  	require.NoError(t, json.NewEncoder(&ab).Encode(actual), args...)
    39  
    40  	var err error
    41  	ebs, abs := eb.String(), ab.String()
    42  	for _, k := range except {
    43  		ebs, err = sjson.Delete(ebs, k)
    44  		require.NoError(t, err)
    45  
    46  		abs, err = sjson.Delete(abs, k)
    47  		require.NoError(t, err)
    48  	}
    49  
    50  	assert.JSONEq(t, ebs, abs, args...)
    51  }
    52  

View as plain text