Skip to content

Commit

Permalink
test passing candidate sources to the results
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Dec 31, 2023
1 parent c14dd2b commit f403f17
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
2 changes: 1 addition & 1 deletion common/candidate_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ type PathSetIn struct {
}

type CandidateSource interface {
WhereSet(somePath string) PathSetIn
WhereSet(somePath string) *PathSetIn
}
13 changes: 13 additions & 0 deletions common/nix_candidate_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package common

type NixCandidateSource struct {
fs Filesystem
}

func NewNixCandidateSource(fs Filesystem) CandidateSource {
return &NixCandidateSource{fs}
}

func (s *NixCandidateSource) WhereSet(somePath string) *PathSetIn {
return nil
}
46 changes: 33 additions & 13 deletions common/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@ type positionLookup multimap.MultiMap[string, int]
func duplicatePredicate(a, b int) bool { return true }

type ResultsCalculator struct {
fs Filesystem
source ValueSource
pathsLookup positionLookup
fs Filesystem
source ValueSource
pathsLookup positionLookup
candidateSource CandidateSource
}

func NewResultsCalculator(fs Filesystem, source ValueSource) *ResultsCalculator {
func NewCustomResultsCalculator(fs Filesystem, source ValueSource, candidateSource CandidateSource) *ResultsCalculator {
return &ResultsCalculator{
fs: fs,
source: source,
pathsLookup: multimap.NewMapSet[string](duplicatePredicate),
fs: fs,
source: source,
pathsLookup: multimap.NewMapSet[string](duplicatePredicate),
candidateSource: candidateSource,
}
}

func NewResultsCalculator(fs Filesystem, source ValueSource) *ResultsCalculator {
return NewCustomResultsCalculator(fs, source, NewNixCandidateSource(fs))
}

func (r *ResultsCalculator) CalculateResults() ([]ResultRow, error) {
if len(r.source.Values()) == 0 {
return nil, errors.New(r.source.Source() + " is empty")
Expand All @@ -59,18 +65,32 @@ func (r *ResultsCalculator) calculateResultRows() []ResultRow {
pathKey := r.fs.GetAbsolutePath(path)
dup := getDuplicatesOf(r.pathsLookup, pathKey, index)
exists, isdir := r.fs.PathStatus(pathKey)
candidateSources := r.getCandidateSourcesFor(path)
res = append(res, ResultRow{
Id: index + 1,
Path: path,
ExpandedPath: pathKey,
Exists: exists,
IsDir: isdir,
Duplicates: dup,
Id: index + 1,
Path: path,
ExpandedPath: pathKey,
Exists: exists,
IsDir: isdir,
Duplicates: dup,
CandidateSources: candidateSources,
})
}
return res
}

func (r *ResultsCalculator) getCandidateSourcesFor(path string) []string {
res := r.candidateSource.WhereSet(path)
if res == nil {
return nil
}
candidateSources := []string{}
for _, source := range res.WhereSet {
candidateSources = append(candidateSources, source.Original)
}
return candidateSources
}

// returns the IDs of the duplicate indices (== index+1)
func getDuplicatesOf(pathsLookup multimap.MultiMap[string, int], pathKey string, index int) []int {
instances := pathsLookup.Get(pathKey)
Expand Down
38 changes: 38 additions & 0 deletions common/results_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -63,6 +64,34 @@ func Test_no_duplicates(t *testing.T) {
mockFs.AssertExpectations(t)
}

func Test_candidate_sources_not_empty_when_found(t *testing.T) {
mockFs := new(mockFilesystem)

// setup expectations
mockFs.On("GetAbsolutePath", "a").Return("a")
mockFs.On("GetAbsolutePath", "b").Return("b")
mockFs.On("PathStatus", mock.Anything).Return(true, true)

sut := NewCustomResultsCalculator(mockFs, &mockValueSource{values_: []string{
"a",
"b",
}}, &mockCandidateSource{
map_: map[string]*PathSetIn{
// a is found, but not b
"a": {
What: Location{"a", "a"},
WhereSet: []Location{
{"a", "a"},
},
},
},
})
rows, _ := sut.CalculateResults()
require.Len(t, rows[0].CandidateSources, 1)
assert.Equal(t, "a", rows[0].CandidateSources[0])
assert.Nil(t, rows[1].CandidateSources)
}

type mockFilesystem struct {
mock.Mock
}
Expand Down Expand Up @@ -94,3 +123,12 @@ func (m *mockValueSource) Orig() string {
func (m *mockValueSource) Values() []string {
return m.values_
}

type mockCandidateSource struct {
map_ map[string]*PathSetIn
}

func (s *mockCandidateSource) WhereSet(somePath string) *PathSetIn {
fmt.Println("TRYING", somePath, s.map_[somePath])
return s.map_[somePath]
}

0 comments on commit f403f17

Please sign in to comment.