From d8123e814d37669daca4cf69107109f9009d39fa Mon Sep 17 00:00:00 2001 From: Jeroen Wijenbergh Date: Mon, 27 May 2024 13:39:43 +0200 Subject: Levenshtein: Add initial tests --- internal/levenshtein/levenshtein_test.go | 194 +++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 internal/levenshtein/levenshtein_test.go (limited to 'internal') diff --git a/internal/levenshtein/levenshtein_test.go b/internal/levenshtein/levenshtein_test.go new file mode 100644 index 0000000..41f7f4a --- /dev/null +++ b/internal/levenshtein/levenshtein_test.go @@ -0,0 +1,194 @@ +package levenshtein + +import "testing" + +func TestLevenshtein(t *testing.T) { + cases := []struct{ + a string + b string + score int + }{ + { + a: "foo", + b: "foo", + score: 0, + }, + { + a: "foo", + b: "foo", + score: 0, + }, + { + a: "foo", + b: "foosd", + score: 2, + }, + { + a: "foo", + b: "bla", + score: 3, + }, + { + a: "foo", + b: "", + score: 3, + }, + { + a: "", + b: "foo", + score: 3, + }, + } + for i, c := range cases { + g := levenshtein(c.a, c.b) + if g != c.score { + t.Fatalf("case %d not equal, got: %d, want: %d", i, g, c.score) + } + } +} + +func TestAdjusted(t *testing.T) { + cases := []struct{ + a string + b string + score int + }{ + { + a: "foo", + b: "foo", + score: 0, + }, + { + a: "foo", + b: "foo", + score: 0, + }, + { + a: "foo", + b: "foosd", + score: 2, + }, + { + a: "foo", + b: "bla", + score: -1, + }, + { + a: "bla foo", + b: "bla", + score: -1, + }, + { + a: "foo", + b: "", + score: -1, + }, + { + a: "", + b: "foo", + score: 3, + }, + } + for i, c := range cases { + g := adjusted(c.a, c.b) + if g != c.score { + t.Fatalf("case %d not equal, got: %d, want: %d", i, g, c.score) + } + } +} + +func TestDiscoveryScore(t *testing.T) { + cases := []struct{ + q string + disp map[string]string + keys map[string]string + score int + }{ + { + q: "test", + disp: map[string]string{ + "en": "test", + "de": "test", + }, + keys: map[string]string{ + "en": "testing", + "de": "testing", + }, + score: 4, + }, + { + q: "test", + disp: map[string]string{ + "en": "testing", + "de": "testing", + }, + keys: map[string]string{ + "en": "test", + "de": "test", + }, + score: 8, + }, + { + q: "foo", + disp: map[string]string{ + "en": "test", + "de": "testing", + }, + keys: map[string]string{ + "en": "foo", + "de": "foo", + }, + score: 6, + }, + { + q: "fox", + disp: map[string]string{ + "en": "test", + "de": "testing", + }, + keys: map[string]string{ + "en": "foo", + "de": "foo", + }, + score: -2, + }, + } + + for i, c := range cases { + g := DiscoveryScore(c.q, c.disp, c.keys) + if g != c.score { + t.Fatalf("case %d not equal, got: %d, want: %d", i, g, c.score) + } + } +} + +func TestRemoveDiacritics(t *testing.T) { + cases := []struct { + input string + want string + e error + }{ + { + input: "foobar", + want: "foobar", + e: nil, + }, + { + input: "fòóbår", + want: "foobar", + e: nil, + }, + { + input: "GÉANT", + want: "GEANT", + e: nil, + }, + } + + for _, c := range cases { + result, e := removeDiacritics(c.input) + if result != c.want || e != c.e { + t.Fatalf("Result: %s, %v Want: %s, %v", result, e, c.want, c.e) + } + } +} -- cgit v1.2.3