2019-10-20 16:02:28 +02:00
|
|
|
package sheet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestSearchNoMatch ensures that the expected output is returned when no
|
|
|
|
// matches are found
|
|
|
|
func TestSearchNoMatch(t *testing.T) {
|
|
|
|
|
|
|
|
// mock a cheatsheet
|
|
|
|
sheet := Sheet{
|
|
|
|
Text: "The quick brown fox\njumped over\nthe lazy dog.",
|
|
|
|
}
|
|
|
|
|
|
|
|
// compile the search regex
|
|
|
|
reg, err := regexp.Compile("(?i)foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to compile regex: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// search the sheet
|
2020-02-15 20:40:33 +01:00
|
|
|
matches := sheet.Search(reg)
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// assert that no matches were found
|
2020-02-15 20:40:33 +01:00
|
|
|
if matches != "" {
|
|
|
|
t.Errorf("failure: expected no matches: got: %s", matches)
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:40:33 +01:00
|
|
|
// TestSearchSingleMatch asserts that the expected output is returned
|
|
|
|
// when a single match is returned
|
|
|
|
func TestSearchSingleMatch(t *testing.T) {
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// mock a cheatsheet
|
|
|
|
sheet := Sheet{
|
2020-02-15 21:56:25 +01:00
|
|
|
Text: "The quick brown fox\njumped over\n\nthe lazy dog.",
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// compile the search regex
|
|
|
|
reg, err := regexp.Compile("(?i)fox")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to compile regex: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// search the sheet
|
2020-02-15 20:40:33 +01:00
|
|
|
matches := sheet.Search(reg)
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// specify the expected results
|
2020-02-15 21:56:25 +01:00
|
|
|
want := "The quick brown fox\njumped over"
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// assert that the correct matches were returned
|
2020-02-15 20:40:33 +01:00
|
|
|
if matches != want {
|
2019-10-20 16:02:28 +02:00
|
|
|
t.Errorf(
|
|
|
|
"failed to return expected matches: want:\n%s, got:\n%s",
|
2020-02-15 20:40:33 +01:00
|
|
|
want,
|
|
|
|
matches,
|
2019-10-20 16:02:28 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:40:33 +01:00
|
|
|
// TestSearchMultiMatch asserts that the expected output is returned
|
|
|
|
// when a multiple matches are returned
|
|
|
|
func TestSearchMultiMatch(t *testing.T) {
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// mock a cheatsheet
|
|
|
|
sheet := Sheet{
|
2020-02-15 21:56:25 +01:00
|
|
|
Text: "The quick brown fox\n\njumped over\n\nthe lazy dog.",
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// compile the search regex
|
|
|
|
reg, err := regexp.Compile("(?i)the")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to compile regex: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// search the sheet
|
2020-02-15 20:40:33 +01:00
|
|
|
matches := sheet.Search(reg)
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// specify the expected results
|
2020-02-15 21:56:25 +01:00
|
|
|
want := "The quick brown fox\n\nthe lazy dog."
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// assert that the correct matches were returned
|
|
|
|
if !reflect.DeepEqual(matches, want) {
|
|
|
|
t.Errorf(
|
|
|
|
"failed to return expected matches: want:\n%s, got:\n%s",
|
2020-02-15 20:40:33 +01:00
|
|
|
want,
|
|
|
|
matches,
|
2019-10-20 16:02:28 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|