Skip to content

Commit

Permalink
chore: add examples for MatchJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Oct 30, 2022
1 parent 864d5ba commit 33b8ac0
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 105 deletions.
41 changes: 41 additions & 0 deletions examples/__snapshots__/matchJSON_test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

[TestMatchJSON/should_make_a_json_object_snapshot - 1]
{
"mock-0": "value",
"mock-1": 2,
"mock-2": {
"Msg": "Hello World"
},
"mock-3": 10.4
}
---

[TestMatchJSON/should_create_a_prettyJSON_snap - 1]
{
"age": 10,
"email": "mock@email.com",
"user": "mock-user"
}
---

[TestMatchJSON/should_create_a_prettyJSON_snap - 2]
{
"age": 10,
"email": "mock@email.com",
"user": "mock-user"
}
---

[TestMatchJSON/should_marshal_struct - 1]
{
"email": "mock@email.com",
"keys": [
1,
2,
3,
4,
5
],
"name": "mock-name"
}
---
92 changes: 92 additions & 0 deletions examples/__snapshots__/matchSnapshot_test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

[TestMatchSnapshot/should_make_an_int_snapshot - 1]
int(5)
---

[TestMatchSnapshot/should_make_a_string_snapshot - 1]
string snapshot
---

[TestMatchSnapshot/should_make_a_map_snapshot - 1]
map[string]interface {}{
"mock-0": "value",
"mock-1": int(2),
"mock-2": func() {...},
"mock-3": float32(10.399999618530273),
}
---

[TestMatchSnapshot/should_make_multiple_entries_in_snapshot - 1]
int(5)
int(10)
int(20)
int(25)
---

[TestMatchSnapshot/should_make_create_multiple_snapshot - 1]
int(1000)
---

[TestMatchSnapshot/should_make_create_multiple_snapshot - 2]
another snapshot
---

[TestMatchSnapshot/should_make_create_multiple_snapshot - 3]
{
"user": "gkampitakis",
"id": 1234567,
"data": [ ]
}
---

[TestMatchSnapshotTable/string - 1]
input
---

[TestMatchSnapshotTable/integer - 1]
int(10)
---

[TestMatchSnapshotTable/map - 1]
map[string]interface {}{
"test": func() {...},
}
---

[TestMatchSnapshot/nest/more/one_more_nested_test - 1]
it's okay
---

[TestMatchSnapshotTable/buffer - 1]
&bytes.Buffer{
buf: {0x42, 0x75, 0x66, 0x66, 0x65, 0x72, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67},
off: 0,
lastRead: 0,
}
---

[TestMatchSnapshot/.* - 1]
ignore regex patterns on names
---

[TestSimpleTable/string - 1]
input
---

[TestSimpleTable/integer - 1]
int(10)
---

[TestSimpleTable/map - 1]
map[string]interface {}{
"test": func() {...},
}
---

[TestSimpleTable/buffer - 1]
&bytes.Buffer{
buf: {0x42, 0x75, 0x66, 0x66, 0x65, 0x72, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67},
off: 0,
lastRead: 0,
}
---
70 changes: 0 additions & 70 deletions examples/__snapshots__/simple_test.snap

This file was deleted.

42 changes: 42 additions & 0 deletions examples/matchJSON_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package examples

import (
"testing"

"github.com/gkampitakis/go-snaps/snaps"
)

func TestMatchJSON(t *testing.T) {
t.Run("should make a json object snapshot", func(t *testing.T) {
m := map[string]interface{}{
"mock-0": "value",
"mock-1": 2,
"mock-2": struct{ Msg string }{"Hello World"},
"mock-3": float32(10.4),
}

snaps.MatchJSON(t, m)
})

t.Run("should create a prettyJSON snap", func(t *testing.T) {
value := `{"user":"mock-user","age":10,"email":"mock@email.com"}`
snaps.MatchJSON(t, value)
snaps.MatchJSON(t, []byte(value))
})

t.Run("should marshal struct", func(t *testing.T) {
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Keys []int `json:"keys"`
}

u := User{
Name: "mock-name",
Email: "mock@email.com",
Keys: []int{1, 2, 3, 4, 5},
}

snaps.MatchJSON(t, u)
})
}
2 changes: 1 addition & 1 deletion examples/simple_test.go → examples/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestMain(t *testing.M) {
os.Exit(v)
}

func TestSimple(t *testing.T) {
func TestMatchSnapshot(t *testing.T) {
t.Run("should make an int snapshot", func(t *testing.T) {
snaps.MatchSnapshot(t, 5)
})
Expand Down
4 changes: 2 additions & 2 deletions internal/colors/colors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
func TestPrintColors(t *testing.T) {
t.Run("string utils", func(t *testing.T) {
t.Run("hasNewlineSuffix", func(t *testing.T) {
test.Equal(t, false, hasNewlineSuffix("hello \n world"))
test.Equal(t, true, hasNewlineSuffix("hello \n world\n"))
test.False(t, hasNewlineSuffix("hello \n world"))
test.True(t, hasNewlineSuffix("hello \n world\n"))
})

t.Run("trimSuffix", func(t *testing.T) {
Expand Down
24 changes: 24 additions & 0 deletions internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,27 @@ func CreateTempFile(t *testing.T, data string) string {

return path
}

func True(t *testing.T, val bool) {
t.Helper()

if !val {
t.Error("expected true but got false")
}
}

func False(t *testing.T, val bool) {
t.Helper()

if val {
t.Error("expected false but got true")
}
}

func Nil(t *testing.T, val interface{}) {
v := reflect.ValueOf(val)

if val != nil && !v.IsNil() {
t.Errorf("expected nil but got %v", val)
}
}
6 changes: 3 additions & 3 deletions snaps/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestExamineSnaps(t *testing.T) {
obsolete, err := examineSnaps(tests, used, "", shouldUpdate)

test.Equal(t, []string{}, obsolete)
test.Equal(t, err, nil)
test.Nil(t, err)
})

t.Run("should report two obsolete snapshots and not change content", func(t *testing.T) {
Expand All @@ -199,7 +199,7 @@ func TestExamineSnaps(t *testing.T) {
content2 := getFileContent(t, used[1])

test.Equal(t, []string{"TestDir1_3/TestSimple - 2", "TestDir2_2/TestSimple - 1"}, obsolete)
test.Equal(t, err, nil)
test.Nil(t, err)

// Content of snaps is not changed
test.Equal(t, mockSnap1, content1)
Expand Down Expand Up @@ -251,7 +251,7 @@ string hello world 2 2 1
},
obsolete,
)
test.Equal(t, err, nil)
test.Nil(t, err)

// Content of snaps is not changed
test.Equal(t, expected1, content1)
Expand Down
10 changes: 5 additions & 5 deletions snaps/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func TestStringUtils(t *testing.T) {
})

t.Run("isSingleLine", func(t *testing.T) {
test.Equal(t, true, isSingleline("hello world"))
test.Equal(t, true, isSingleline("hello world\n"))
test.Equal(t, false, isSingleline(`hello
test.True(t, isSingleline("hello world"))
test.True(t, isSingleline("hello world\n"))
test.False(t, isSingleline(`hello
world
`))
test.Equal(t, false, isSingleline("hello \n world\n"))
test.Equal(t, false, isSingleline("hello \n world"))
test.False(t, isSingleline("hello \n world\n"))
test.False(t, isSingleline("hello \n world"))
})
}

Expand Down
4 changes: 2 additions & 2 deletions snaps/matchJSON_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestMatchJSON(t *testing.T) {
MatchJSON(mockT, tc.input)

snap, err := getPrevSnapshot("mock-name - 1", snapPath)
test.Equal(t, nil, err)
test.Nil(t, err)
test.Equal(t, expected, snap)
test.Equal(t, 1, testEvents.items[added])
})
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestMatchJSON(t *testing.T) {
MatchJSON(mockT, "{\"value\":\"bye world\"}")

snap, err := snapshotFileToString(snapPath)
test.Equal(t, nil, err)
test.Nil(t, err)
test.Equal(t, "\n[mock-name - 1]\n{\n \"value\": \"bye world\"\n}\n---\n", snap)
test.Equal(t, 1, testEvents.items[updated])
})
Expand Down
6 changes: 3 additions & 3 deletions snaps/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func setupSnapshot(t *testing.T, file string, ci bool, update ...bool) string {

_, err := os.Stat(snapPath)
// This is for checking we are starting with a clean state testing
test.Equal(t, true, errors.Is(err, os.ErrNotExist))
test.True(t, errors.Is(err, os.ErrNotExist))

return snapPath
}
Expand All @@ -77,7 +77,7 @@ func TestMatchSnapshot(t *testing.T) {
MatchSnapshot(mockT, 10, "hello world")

snap, err := snapshotFileToString(snapPath)
test.Equal(t, nil, err)
test.Nil(t, err)
test.Equal(t, "\n[mock-name - 1]\nint(10)\nhello world\n---\n", snap)
test.Equal(t, 1, testEvents.items[added])
})
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestMatchSnapshot(t *testing.T) {
MatchSnapshot(mockT, 100, "bye world")

snap, err := snapshotFileToString(snapPath)
test.Equal(t, nil, err)
test.Nil(t, err)
test.Equal(t, "\n[mock-name - 1]\nint(100)\nbye world\n---\n", snap)
test.Equal(t, 1, testEvents.items[updated])
})
Expand Down
Loading

0 comments on commit 33b8ac0

Please sign in to comment.