https://github.com/orsinium-labs/testo
Go testing framework for matching a JSON payload against a pattern
https://github.com/orsinium-labs/testo
go golang json pattern-matching test testing testing-framework
Last synced: 6 months ago
JSON representation
Go testing framework for matching a JSON payload against a pattern
- Host: GitHub
- URL: https://github.com/orsinium-labs/testo
- Owner: orsinium-labs
- License: mit
- Created: 2025-11-20T16:17:55.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-11-22T08:47:11.000Z (7 months ago)
- Last Synced: 2025-11-22T10:28:29.388Z (7 months ago)
- Topics: go, golang, json, pattern-matching, test, testing, testing-framework
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# testo
Go testing framework for matching a JSON payload against a pattern.
The pattern is a subset of JSON with some additional keywords that is then converted into a [valdo](github.com/orsinium-labs/valdo) validator.
## Installation
```bash
go get github.com/orsinium-labs/testo
```
## Usage
```go
func TestMyCoolAPI(t *testing.T) {
body := `{"name": "aragorn", "age": 87}`
testo.Assert(t, body, `{"name": "aragorn", "age": int}`)
}
```
The `body` can be bytes, string, or `io.Reader` (for example, an HTTP response body).
## Syntax
The pattern syntax is a suparset JSON with a few additional features.
Keywords:
* `true`, `false`, `null`: same as in JSON.
* `any`: a value of any type.
* `string`: any string of any length.
* `int`: an integer number.
* `uint`: a non-negative integer number.
* `float`: a floating point number.
* `bool`: a boolean value (`true` or `false`).
* `object`: any object.
* `array`: any JSON array.
* `strings`: array of strings (including empty array).
* `ints`: array of integer numberss (including empty array).
* `uints`: array of unsigned integer numbers (including empty array).
* `floats`: array of floating point numbers (including empty array).
* `bools`: array of boolean values (including empty array).
* `objects`: array of objects (including empty array).
If a property name starts with `^`, it's interpreted as a regular expression. For example, the following pattern defines an object with non-empty unsigned integer numbers as keys and integer values:
```json
{"^[0-9]+$": int}
```
So, if you want to assert only one property of an object:
```json
{
"name": "Aragorn",
"^.+$": any,
}
```
An object can have any number or properties and regex properties in any combination.