json.go
json.go - Overview
This file provides utility functions for comparing JSON responses in tests. It includes functions for checking if a string is a valid JSON, comparing JSON lists, normalizing JSON for comparison, and converting strings to arrays.
Detailed Documentation
IsJSONResponse(s string) bool
- Purpose: Checks if a given string is a valid JSON string.
- Parameters:
s
:string
- The string to check.
- Returns:
bool
- True if the string is a valid JSON, false otherwise.
AssertJSONEqualList(t *testing.T, expected []string, actual string)
- Purpose: Asserts that the actual JSON string is equal to one of the expected JSON strings in the provided list.
- Parameters:
t
:*testing.T
- Testing object for reporting errors.expected
:[]string
- A list of expected JSON strings.actual
:string
- The actual JSON string to compare.
- Returns: None. It calls
t.Errorf
if the actual JSON doesn't match any of the expected ones.
compareJSONs(t *testing.T, expected, actual string) bool
- Purpose: Compares two JSON strings for equality after unmarshaling and normalizing them.
- Parameters:
t
:*testing.T
- Testing object for error reporting.expected
:string
- The expected JSON string.actual
:string
- The actual JSON string.
- Returns:
bool
- True if the normalized JSONs are deeply equal, false otherwise.
NormalizeJSON(v interface{}) interface{}
- Purpose: Recursively normalizes a JSON value by converting maps and slices to normalized maps and slices.
- Parameters:
v
:interface{}
- The JSON value to normalize (can be a map, slice, or primitive).
- Returns:
interface{}
- The normalized JSON value.
CompareJSON(t *testing.T, expected, actual string)
- Purpose: Compares two JSON strings for equality after unmarshaling them into maps.
- Parameters:
t
:*testing.T
- Testing object for error reporting.expected
:string
- The expected JSON string.actual
:string
- The actual JSON string.
- Returns: None. It calls
t.Fail
if the JSONs are not equal.
ConvertToArray(input string) []string
- Purpose: Converts a string representation of an array (e.g.,
"[element1, element2]"
) into a string slice. - Parameters:
input
:string
- The string to convert.
- Returns:
[]string
- The resulting string slice.
IsJSONString(s string) bool
- Purpose: Determines if a string is a valid JSON string.
- Parameters:
s
:string
- The string to check.
- Returns:
bool
- True if the string is a valid JSON string, false otherwise.
ArraysArePermutations[T comparable](a, b []T) bool
- Purpose: Checks if two arrays are permutations of each other (i.e., contain the same elements regardless of order).
- Parameters:
a
:[]T
- The first array.b
:[]T
- The second array.
- Returns:
bool
- True if the arrays are permutations of each other, false otherwise.