Skip to content

Commit

Permalink
feat: implement matchStandaloneJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Dec 25, 2024
1 parent f81115d commit 159bbef
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 8 deletions.
112 changes: 112 additions & 0 deletions snaps/matchStandaloneJSON.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package snaps

import (
"errors"
"fmt"
"strings"

"github.com/gkampitakis/go-snaps/internal/colors"
"github.com/gkampitakis/go-snaps/match"
)

func (c *Config) MatchStandaloneJSON(t testingT, input any, matchers ...match.JSONMatcher) {
t.Helper()

c.extension = ".json"

matchStandaloneJSON(c, t, input, matchers...)
}

func MatchStandaloneJSON(t testingT, input any, matchers ...match.JSONMatcher) {
t.Helper()

c := defaultConfig
c.extension = ".json"

matchStandaloneJSON(&c, t, input, matchers...)
}

func matchStandaloneJSON(c *Config, t testingT, input any, matchers ...match.JSONMatcher) {
t.Helper()

genericPathSnap, genericSnapPathRel := snapshotPath(c, t.Name(), true)
snapPath, snapPathRel := standaloneTestsRegistry.getTestID(genericPathSnap, genericSnapPathRel)
t.Cleanup(func() {
standaloneTestsRegistry.reset(genericPathSnap)
})

j, err := validateJSON(input)
if err != nil {
handleError(t, err)
return
}

j, matchersErrors := applyJSONMatchers(j, matchers...)
if len(matchersErrors) > 0 {
s := strings.Builder{}

for _, err := range matchersErrors {
colors.Fprint(
&s,
colors.Red,
fmt.Sprintf(
"\n%smatch.%s(\"%s\") - %s",
errorSymbol,
err.Matcher,
err.Path,
err.Reason,
),
)
}

handleError(t, s.String())
return
}

snapshot := takeJSONSnapshot(j)
prevSnapshot, err := getPrevStandaloneSnapshot(snapPath)
if errors.Is(err, errSnapNotFound) {
if isCI {
handleError(t, err)
return
}

err := upsertStandaloneSnapshot(snapshot, snapPath)
if err != nil {
handleError(t, err)
return
}

t.Log(addedMsg)
testEvents.register(added)
return
}
if err != nil {
handleError(t, err)
return
}

diff := prettyDiff(
prevSnapshot,
snapshot,
snapPathRel,
1,
)
if diff == "" {
testEvents.register(passed)
return
}

if !shouldUpdate(c.update) {
handleError(t, diff)
return
}

if err = upsertStandaloneSnapshot(snapshot, snapPath); err != nil {
handleError(t, err)
return
}

t.Log(updatedMsg)
testEvents.register(updated)
}
16 changes: 8 additions & 8 deletions snaps/matchStandaloneSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

/*
MatchStandaloneSnapshot verifies the value matches the most recent snap file
MatchStandaloneSnapshot verifies the input matches the most recent snap file
MatchStandaloneSnapshot(t, "Hello World")
Expand All @@ -16,14 +16,14 @@ MatchStandaloneSnapshot creates one snapshot file per call.
You can call MatchStandaloneSnapshot multiple times inside a test.
It will create multiple snapshot files at `__snapshots__` folder by default.
*/
func (c *Config) MatchStandaloneSnapshot(t testingT, value any) {
func (c *Config) MatchStandaloneSnapshot(t testingT, input any) {
t.Helper()

matchStandaloneSnapshot(c, t, value)
matchStandaloneSnapshot(c, t, input)
}

/*
MatchStandaloneSnapshot verifies the value matches the most recent snap file
MatchStandaloneSnapshot verifies the input matches the most recent snap file
MatchStandaloneSnapshot(t, "Hello World")
Expand All @@ -32,13 +32,13 @@ MatchStandaloneSnapshot creates one snapshot file per call.
You can call MatchStandaloneSnapshot multiple times inside a test.
It will create multiple snapshot files at `__snapshots__` folder by default.
*/
func MatchStandaloneSnapshot(t testingT, value any) {
func MatchStandaloneSnapshot(t testingT, input any) {
t.Helper()

matchStandaloneSnapshot(&defaultConfig, t, value)
matchStandaloneSnapshot(&defaultConfig, t, input)
}

func matchStandaloneSnapshot(c *Config, t testingT, value any) {
func matchStandaloneSnapshot(c *Config, t testingT, input any) {
t.Helper()

genericPathSnap, genericSnapPathRel := snapshotPath(c, t.Name(), true)
Expand All @@ -47,7 +47,7 @@ func matchStandaloneSnapshot(c *Config, t testingT, value any) {
standaloneTestsRegistry.reset(genericPathSnap)
})

snapshot := pretty.Sprint(value)
snapshot := pretty.Sprint(input)
prevSnapshot, err := getPrevStandaloneSnapshot(snapPath)
if errors.Is(err, errSnapNotFound) {
if isCI {
Expand Down

0 comments on commit 159bbef

Please sign in to comment.