mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 09:21:14 -06:00
ccc20fdad1
Uses Levenshtein distance to decide if the input is similar enough to one of the given suggestions, and returns that suggestion if so. The distance threshold of three was arrived at experimentally, and has no objective basis.
54 lines
925 B
Go
54 lines
925 B
Go
package didyoumean
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestNameSuggestion(t *testing.T) {
|
|
var keywords = []string{"false", "true", "null"}
|
|
|
|
tests := []struct {
|
|
Input, Want string
|
|
}{
|
|
{"true", "true"},
|
|
{"false", "false"},
|
|
{"null", "null"},
|
|
{"bananas", ""},
|
|
{"NaN", ""},
|
|
{"Inf", ""},
|
|
{"Infinity", ""},
|
|
{"void", ""},
|
|
{"undefined", ""},
|
|
|
|
{"ture", "true"},
|
|
{"tru", "true"},
|
|
{"tre", "true"},
|
|
{"treu", "true"},
|
|
{"rtue", "true"},
|
|
|
|
{"flase", "false"},
|
|
{"fales", "false"},
|
|
{"flse", "false"},
|
|
{"fasle", "false"},
|
|
{"fasel", "false"},
|
|
{"flue", "false"},
|
|
|
|
{"nil", "null"},
|
|
{"nul", "null"},
|
|
{"unll", "null"},
|
|
{"nll", "null"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.Input, func(t *testing.T) {
|
|
got := NameSuggestion(test.Input, keywords)
|
|
if got != test.Want {
|
|
t.Errorf(
|
|
"wrong result\ninput: %q\ngot: %q\nwant: %q",
|
|
test.Input, got, test.Want,
|
|
)
|
|
}
|
|
})
|
|
}
|
|
}
|