-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09cd270
commit f81115d
Showing
18 changed files
with
1,051 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,29 @@ | ||
|
||
[TestMatchYaml/should_match_struct_yaml - 1] | ||
name: John Doe | ||
age: 30 | ||
email: john.doe@example.com | ||
address: mock-address | ||
time: mock-time | ||
|
||
--- | ||
|
||
[TestMatchYaml/custom_matching_logic - 1] | ||
name: mock-user | ||
email: mock-user@email.com | ||
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 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: "john.doe@example.com", | ||
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: "mock-user@email.com", | ||
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 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 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.