-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: implement matchYAML #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,051
−127
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b9152ce
feat: implement matchYAML
gkampitakis 1437428
add support for
gkampitakis 9abc0b2
feat: add unit tests
gkampitakis 972ae49
chore: update README.md
gkampitakis 4c42a47
fix: documentation for path, newline insertion, yaml encoding
gkampitakis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| [](https://pkg.go.dev/github.com/gkampitakis/go-snaps) | ||
|
|
||
| <p align="center"> | ||
| <b>Jest-like snapshot testing in Golang</b> | ||
| <b>Jest-like snapshot testing in Go</b> | ||
| </p> | ||
|
|
||
| <br> | ||
|
|
@@ -18,12 +18,13 @@ | |
|
|
||
| - [Installation](#installation) | ||
| - [MatchSnapshot](#matchsnapshot) | ||
| - [MatchStandaloneSnapshot](#matchstandalonesnapshot) `New` | ||
| - [MatchJSON](#matchjson) | ||
| - [Matchers](#matchers) | ||
| - [match.Any](#matchany) | ||
| - [match.Custom](#matchcustom) | ||
| - [match.Type\[ExpectedType\]](#matchtype) | ||
| - [MatchStandaloneSnapshot](#matchstandalonesnapshot) | ||
| - [MatchYAML](#matchyaml) `New` | ||
| - [Matchers](#matchers) | ||
| - [match.Any](#matchany) | ||
| - [match.Custom](#matchcustom) | ||
| - [match.Type\[ExpectedType\]](#matchtype) | ||
| - [Configuration](#configuration) | ||
| - [Update Snapshots](#update-snapshots) | ||
| - [Clean obsolete Snapshots](#clean-obsolete-snapshots) | ||
|
|
@@ -85,6 +86,29 @@ name is the test file name with extension `.snap`. | |
| So for example if your test is called `test_simple.go` when you run your tests, a snapshot file | ||
| will be created at `./__snapshots__/test_simple.snaps`. | ||
|
|
||
| ## MatchStandaloneSnapshot | ||
|
|
||
| `MatchStandaloneSnapshot` will create snapshots on separate files as opposed to `MatchSnapshot` which adds multiple snapshots inside the same file. | ||
|
|
||
| _Combined with `snaps.Ext` you can have proper syntax highlighting and better readability_ | ||
|
|
||
| ```go | ||
| // test_simple.go | ||
|
|
||
| func TestSimple(t *testing.T) { | ||
| snaps.MatchStandaloneSnapshot(t, "Hello World") | ||
| // or create an html snapshot file | ||
| snaps.WithConfig(snaps.Ext(".html")). | ||
| MatchStandaloneSnapshot(t, "<html><body><h1>Hello World</h1></body></html>") | ||
| } | ||
| ``` | ||
|
|
||
| `go-snaps` saves the snapshots in `__snapshots__` directory and the file | ||
| name is the test file name with extension `.snap`. | ||
|
|
||
| So for example if your test is called `test_simple.go` when you run your tests, a snapshot file | ||
| will be created at `./__snapshots__/TestSimple_1.snaps`. | ||
|
|
||
| ## MatchJSON | ||
|
|
||
| `MatchJSON` can be used to capture data that can represent a valid json. | ||
|
|
@@ -107,21 +131,49 @@ func TestJSON(t *testing.T) { | |
|
|
||
| JSON will be saved in snapshot in pretty format for more readability and deterministic diffs. | ||
|
|
||
| ## MatchYAML | ||
|
|
||
| `MatchYAML` can be used to capture data that can represent a valid yaml. | ||
|
|
||
| You can pass a valid json in form of `string` or `[]byte` or whatever value can be passed | ||
| successfully on `yaml.Marshal`. | ||
|
|
||
| ```go | ||
| func TestYAML(t *testing.T) { | ||
| type User struct { | ||
| Age int | ||
| Email string | ||
| } | ||
|
|
||
| snaps.MatchYAML(t, "user: \"mock-user\"\nage: 10\nemail: [email protected]") | ||
| snaps.MatchYAML(t, []byte("user: \"mock-user\"\nage: 10\nemail: [email protected]")) | ||
| snaps.MatchYAML(t, User{10, "mock-email"}) | ||
| } | ||
| ``` | ||
|
|
||
| ### Matchers | ||
|
|
||
| `MatchJSON`'s third argument can accept a list of matchers. Matchers are functions that can act | ||
| `MatchJSON`'s and `MatchYAML`'s third argument can accept a list of matchers. Matchers are functions that can act | ||
| as property matchers and test values. | ||
|
|
||
| You can pass the path of the property you want to match and test. | ||
|
|
||
| _More information about the supported path syntax from [gjson](https://github.com/tidwall/gjson/blob/v1.17.0/SYNTAX.md)._ | ||
|
|
||
| Currently `go-snaps` has three build in matchers | ||
|
|
||
| - `match.Any` | ||
| - `match.Custom` | ||
| - `match.Type[ExpectedType]` | ||
|
|
||
| _Open to feedback for building more matchers or you can build you own [example](./examples/matchJSON_test.go#L16)._ | ||
|
|
||
| #### Path Syntax | ||
|
|
||
| For JSON go-snaps utilises gjson. | ||
|
|
||
| _More information about the supported path syntax from [gjson](https://github.com/tidwall/gjson/blob/v1.17.0/SYNTAX.md)._ | ||
|
|
||
| As for YAML go-snaps utilises [github.com/goccy/go-yaml#5-use-yamlpath](https://github.com/goccy/go-yaml#5-use-yamlpath). | ||
|
|
||
| #### match.Any | ||
|
|
||
| Any matcher acts as a placeholder for any value. It replaces any targeted path with a | ||
|
|
@@ -196,29 +248,6 @@ match.Type[string]("user.info"). | |
|
|
||
| You can see more [examples](./examples/matchJSON_test.go#L96). | ||
|
|
||
| ## MatchStandaloneSnapshot | ||
|
|
||
| `MatchStandaloneSnapshot` will create snapshots on separate files as opposed to `MatchSnapshot` which adds multiple snapshots inside the same file. | ||
|
|
||
| _Combined with `snaps.Ext` you can have proper syntax highlighting and better readability_ | ||
|
|
||
| ```go | ||
| // test_simple.go | ||
|
|
||
| func TestSimple(t *testing.T) { | ||
| snaps.MatchStandaloneSnapshot(t, "Hello World") | ||
| // or create an html snapshot file | ||
| snaps.WithConfig(snaps.Ext(".html")). | ||
| MatchStandaloneSnapshot(t, "<html><body><h1>Hello World</h1></body></html>") | ||
| } | ||
| ``` | ||
|
|
||
| `go-snaps` saves the snapshots in `__snapshots__` directory and the file | ||
| name is the test file name with extension `.snap`. | ||
|
|
||
| So for example if your test is called `test_simple.go` when you run your tests, a snapshot file | ||
| will be created at `./__snapshots__/TestSimple_1.snaps`. | ||
|
|
||
| ## Configuration | ||
|
|
||
| `go-snaps` allows passing configuration for overriding | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
|
|
||
| [TestMatchYaml/should_match_struct_yaml - 1] | ||
| name: John Doe | ||
| age: 30 | ||
| email: [email protected] | ||
| address: mock-address | ||
| time: mock-time | ||
|
|
||
| --- | ||
|
|
||
| [TestMatchYaml/custom_matching_logic - 1] | ||
| name: mock-user | ||
| email: [email protected] | ||
| keys: | ||
| - 1 | ||
| - 2 | ||
| - 3 | ||
| - 4 | ||
| - 5 | ||
|
|
||
| --- | ||
|
|
||
| [TestMatchYaml/type_matcher - 1] | ||
| data: <Type:uint64> | ||
|
|
||
| --- | ||
|
|
||
| [TestMatchYaml/type_matcher - 2] | ||
| metadata: <Type:map[string]interface {}> | ||
|
|
||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package examples | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/gkampitakis/go-snaps/match" | ||
| "github.com/gkampitakis/go-snaps/snaps" | ||
| ) | ||
|
|
||
| func TestMatchYaml(t *testing.T) { | ||
| t.Run("should match struct yaml", func(t *testing.T) { | ||
| type User struct { | ||
| Name string `yaml:"name"` | ||
| Age int `yaml:"age"` | ||
| Email string `yaml:"email"` | ||
| Address string `yaml:"address"` | ||
| Time time.Time `yaml:"time"` | ||
| } | ||
|
|
||
| snaps.MatchYAML(t, User{ | ||
| Name: "John Doe", | ||
| Age: 30, | ||
| Email: "[email protected]", | ||
| Address: "123 Main St", | ||
| Time: time.Now(), | ||
| }, match.Any("$.time").Placeholder("mock-time"), match.Any("$.address").Placeholder("mock-address")) | ||
| }) | ||
|
|
||
| t.Run("custom matching logic", func(t *testing.T) { | ||
| type User struct { | ||
| Name string `json:"name"` | ||
| Email string `json:"email"` | ||
| Keys []int `json:"keys"` | ||
| } | ||
|
|
||
| u := User{ | ||
| Name: "mock-user", | ||
| Email: "[email protected]", | ||
| Keys: []int{1, 2, 3, 4, 5}, | ||
| } | ||
|
|
||
| snaps.MatchYAML(t, u, match.Custom("$.keys", func(val any) (any, error) { | ||
| keys, ok := val.([]any) | ||
| if !ok { | ||
| return nil, fmt.Errorf("expected []any but got %T", val) | ||
| } | ||
|
|
||
| if len(keys) > 5 { | ||
| return nil, fmt.Errorf("expected less than 5 keys") | ||
| } | ||
|
|
||
| return val, nil | ||
| })) | ||
| }) | ||
|
|
||
| t.Run("type matcher", func(t *testing.T) { | ||
| snaps.MatchYAML(t, "data: 10", match.Type[uint64]("$.data")) | ||
|
|
||
| snaps.MatchYAML( | ||
| t, | ||
| "metadata:\n timestamp: 1687108093142", | ||
| match.Type[map[string]any]("$.metadata"), | ||
| ) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,20 @@ | ||
| module github.com/gkampitakis/go-snaps | ||
|
|
||
| go 1.21 | ||
| go 1.22 | ||
|
|
||
| require ( | ||
| github.com/gkampitakis/ciinfo v0.3.0 | ||
| github.com/gkampitakis/ciinfo v0.3.1 | ||
| github.com/gkampitakis/go-diff v1.3.2 | ||
| github.com/goccy/go-yaml v1.15.13 | ||
| github.com/kr/pretty v0.3.1 | ||
| github.com/maruel/natural v1.1.1 | ||
| github.com/tidwall/gjson v1.17.0 | ||
| github.com/tidwall/gjson v1.18.0 | ||
| github.com/tidwall/pretty v1.2.1 | ||
| github.com/tidwall/sjson v1.2.5 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/kr/text v0.2.0 // indirect | ||
| github.com/rogpeppe/go-internal v1.12.0 // indirect | ||
| github.com/rogpeppe/go-internal v1.13.1 // indirect | ||
| github.com/tidwall/match v1.1.1 // indirect | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.