Skip to content

Commit

Permalink
fix(result): tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksasiriski committed Jun 21, 2024
1 parent 83cb2f4 commit b035242
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 156 deletions.
4 changes: 4 additions & 0 deletions src/search/result/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (r General) EngineRanks() []Rank {
return r.generalJSON.EngineRanks
}

func (r *General) InitEngineRanks() {
r.generalJSON.EngineRanks = make([]Rank, 0)
}

func (r *General) ShrinkEngineRanks() {
if r.generalJSON.EngineRanks == nil {
log.Panic().Msg("EngineRanks is nil")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Result interface {
Score() float64
SetScore(float64)
EngineRanks() []Rank
InitEngineRanks()
ShrinkEngineRanks()
AppendEngineRanks(Rank)
ConvertToOutput(string) ResultOutput
Expand Down
2 changes: 2 additions & 0 deletions src/search/result/rank/filler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func TestFillEngineRankRank(t *testing.T) {
for _, rankPair := range ranksTests {
var resOrig result.Result = &result.General{}
var resExpected result.Result = &result.General{}
resOrig.InitEngineRanks()
resExpected.InitEngineRanks()

for _, rank := range rankPair.orig {
resOrig.AppendEngineRanks(rank.Convert())
Expand Down
184 changes: 28 additions & 156 deletions src/search/result/shorten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,11 @@ func TestShortenNegative(t *testing.T) {
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."},
}

// Create test results.
var results = make([]General, 0, len(tests))
for _, test := range tests {
v := General{
generalJSON: generalJSON{
Title: test.orig,
Description: test.orig,
},
}
results = append(results, v)
}

// Shorten the results.
for i := range results {
results[i] = *results[i].Shorten(-1, -1).(*General)
}

// Check if the results are shortened as expected.
for i, test := range tests {
v := results[i]
if v.Title() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Title(), len(v.Title()), test.expected, len(test.expected))
}
if v.Description() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Description(), len(v.Description()), test.expected, len(test.expected))
for _, test := range tests {
short := shortString(test.orig, -1)
if short != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), short, len(short), test.expected, len(test.expected))
}
}
}
Expand All @@ -130,31 +110,11 @@ func TestShortenZero(t *testing.T) {
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", ""},
}

// Create test results.
var results = make([]General, 0, len(tests))
for _, test := range tests {
v := General{
generalJSON: generalJSON{
Title: test.orig,
Description: test.orig,
},
}
results = append(results, v)
}

// Shorten the results.
for i := range results {
results[i] = *results[i].Shorten(0, 0).(*General)
}

// Check if the results are shortened as expected.
for i, test := range tests {
v := results[i]
if v.Title() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Title(), len(v.Title()), test.expected, len(test.expected))
}
if v.Description() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Description(), len(v.Description()), test.expected, len(test.expected))
for _, test := range tests {
short := shortString(test.orig, 0)
if short != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), short, len(short), test.expected, len(test.expected))
}
}
}
Expand All @@ -173,31 +133,11 @@ func TestShorten1(t *testing.T) {
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "L"},
}

// Create test results.
var results = make([]General, 0, len(tests))
for _, test := range tests {
v := General{
generalJSON: generalJSON{
Title: test.orig,
Description: test.orig,
},
}
results = append(results, v)
}

// Shorten the results.
for i := range results {
results[i] = *results[i].Shorten(1, 1).(*General)
}

// Check if the results are shortened as expected.
for i, test := range tests {
v := results[i]
if v.Title() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Title(), len(v.Title()), test.expected, len(test.expected))
}
if v.Description() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Description(), len(v.Description()), test.expected, len(test.expected))
for _, test := range tests {
short := shortString(test.orig, 1)
if short != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), short, len(short), test.expected, len(test.expected))
}
}
}
Expand All @@ -216,31 +156,11 @@ func TestShorten2(t *testing.T) {
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "Lo"},
}

// Create test results.
var results = make([]General, 0, len(tests))
for _, test := range tests {
v := General{
generalJSON: generalJSON{
Title: test.orig,
Description: test.orig,
},
}
results = append(results, v)
}

// Shorten the results.
for i := range results {
results[i] = *results[i].Shorten(2, 2).(*General)
}

// Check if the results are shortened as expected.
for i, test := range tests {
v := results[i]
if v.Title() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Title(), len(v.Title()), test.expected, len(test.expected))
}
if v.Description() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Description(), len(v.Description()), test.expected, len(test.expected))
for _, test := range tests {
short := shortString(test.orig, 2)
if short != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), short, len(short), test.expected, len(test.expected))
}
}
}
Expand Down Expand Up @@ -271,19 +191,11 @@ func TestShorten3(t *testing.T) {
results = append(results, v)

Check failure on line 191 in src/search/result/shorten_test.go

View workflow job for this annotation

GitHub Actions / lint

SA4010: this result of append is never used, except maybe in other appends (staticcheck)
}

// Shorten the results.
for i := range results {
results[i] = *results[i].Shorten(3, 3).(*General)
}

// Check if the results are shortened as expected.
for i, test := range tests {
v := results[i]
if v.Title() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Title(), len(v.Title()), test.expected, len(test.expected))
}
if v.Description() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Description(), len(v.Description()), test.expected, len(test.expected))
for _, test := range tests {
short := shortString(test.orig, 3)
if short != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), short, len(short), test.expected, len(test.expected))
}
}
}
Expand All @@ -302,31 +214,11 @@ func TestShorten4(t *testing.T) {
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "L..."},
}

// Create test results.
var results = make([]General, 0, len(tests))
for _, test := range tests {
v := General{
generalJSON: generalJSON{
Title: test.orig,
Description: test.orig,
},
}
results = append(results, v)
}

// Shorten the results.
for i := range results {
results[i] = *results[i].Shorten(4, 4).(*General)
}

// Check if the results are shortened as expected.
for i, test := range tests {
v := results[i]
if v.Title() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Title(), len(v.Title()), test.expected, len(test.expected))
}
if v.Description() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Description(), len(v.Description()), test.expected, len(test.expected))
for _, test := range tests {
short := shortString(test.orig, 4)
if short != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), short, len(short), test.expected, len(test.expected))
}
}
}
Expand All @@ -345,31 +237,11 @@ func TestShorten400(t *testing.T) {
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa ..."},
}

// Create test results.
var results = make([]General, 0, len(tests))
for _, test := range tests {
v := General{
generalJSON: generalJSON{
Title: test.orig,
Description: test.orig,
},
}
results = append(results, v)
}

// Shorten the results.
for i := range results {
results[i] = *results[i].Shorten(400, 400).(*General)
}

// Check if the results are shortened as expected.
for i, test := range tests {
v := results[i]
if v.Title() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Title(), len(v.Title()), test.expected, len(test.expected))
}
if v.Description() != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), v.Description(), len(v.Description()), test.expected, len(test.expected))
for _, test := range tests {
short := shortString(test.orig, 400)
if short != test.expected {
t.Errorf("\n\tShorten(%q)\n\tlen = %v\n\n\tGot: %q\n\tlen = %v\n\n\tWant: %q\n\tlen = %v", test.orig, len(test.orig), short, len(short), test.expected, len(test.expected))
}
}
}

0 comments on commit b035242

Please sign in to comment.