Skip to content

Commit

Permalink
fix: change order of params for syncregistry
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Jan 22, 2024
1 parent 9394bc9 commit 3f95f7d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion snaps/matchJSON.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func matchJSON(c *config, t testingT, input any, matchers ...match.JSONMatcher)
t.Helper()

snapPath, snapPathRel := snapshotPath(c)
testID := testsRegistry.getTestID(t.Name(), snapPath)
testID := testsRegistry.getTestID(snapPath, t.Name())

j, err := validateJSON(input)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion snaps/matchSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func matchSnapshot(c *config, t testingT, values ...any) {
}

snapPath, snapPathRel := snapshotPath(c)
testID := testsRegistry.getTestID(t.Name(), snapPath)
testID := testsRegistry.getTestID(snapPath, t.Name())
snapshot := takeSnapshot(values)
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
if errors.Is(err, errSnapNotFound) {
Expand Down
8 changes: 4 additions & 4 deletions snaps/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ type syncRegistry struct {

// Returns the id of the test in the snapshot
// Form [<test-name> - <occurrence>]
func (s *syncRegistry) getTestID(tName, snapPath string) string {
func (s *syncRegistry) getTestID(snapPath, testName string) string {
s.Lock()

if _, exists := s.values[snapPath]; !exists {
s.values[snapPath] = make(map[string]int)
}

s.values[snapPath][tName]++
c := s.values[snapPath][tName]
s.values[snapPath][testName]++
c := s.values[snapPath][testName]
s.Unlock()

return fmt.Sprintf("[%s - %d]", tName, c)
return fmt.Sprintf("[%s - %d]", testName, c)
}

func newRegistry() *syncRegistry {
Expand Down
8 changes: 4 additions & 4 deletions snaps/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gkampitakis/go-snaps/internal/test"
)

func TestTestID(t *testing.T) {
func TestSyncRegistry(t *testing.T) {
t.Run("should increment id on each call [concurrent safe]", func(t *testing.T) {
wg := sync.WaitGroup{}
registry := newRegistry()
Expand All @@ -17,15 +17,15 @@ func TestTestID(t *testing.T) {
wg.Add(1)

go func() {
registry.getTestID("test", "/file")
registry.getTestID("/file", "test")
wg.Done()
}()
}

wg.Wait()

test.Equal(t, "[test - 6]", registry.getTestID("test", "/file"))
test.Equal(t, "[test-v2 - 1]", registry.getTestID("test-v2", "/file"))
test.Equal(t, "[test - 6]", registry.getTestID("/file", "test"))
test.Equal(t, "[test-v2 - 1]", registry.getTestID("/file", "test-v2"))
})
}

Expand Down

0 comments on commit 3f95f7d

Please sign in to comment.