all repos — searchix @ 87f2c60692a7ab6a46b969ddd29e1c97e0cd6722

Search engine for NixOS, nix-darwin, home-manager and NUR users

fix: keep names_with_underscores as single tokens Fixes: https://codeberg.org/alanpearce/searchix/issues/2

Alan Pearce
commit

87f2c60692a7ab6a46b969ddd29e1c97e0cd6722

parent

660a81fb1c8f02150f18a143e031844a696e8311

1 file changed, 92 insertions(+), 0 deletions(-)

changed files
A internal/index/nixattr/nixattr_test.go
@@ -0,0 +1,92 @@
+// Copyright (c) 2016 Couchbase, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package nixattr + +import ( + "reflect" + "testing" + + "github.com/blevesearch/bleve/v2/analysis" +) + +func TestNixAttrFilter(t *testing.T) { + + tests := []struct { + input analysis.TokenStream + output analysis.TokenStream + }{ + { + input: tokenStream(""), + output: tokenStream(""), + }, + { + input: tokenStream("a"), + output: tokenStream("a"), + }, + + { + input: tokenStream("linuxKernel.kernels.linux_6_15"), + output: tokenStream("linux", "Kernel", ".", "kernels", ".", "linux_6_15"), + }, + { + input: tokenStream("Lang"), + output: tokenStream("Lang"), + }, + { + input: tokenStream("GLang"), + output: tokenStream("G", "Lang"), + }, + { + input: tokenStream("GOLang"), + output: tokenStream("GO", "Lang"), + }, + { + input: tokenStream("GOOLang"), + output: tokenStream("GOO", "Lang"), + }, + { + input: tokenStream("1234"), + output: tokenStream("1234"), + }, + { + input: tokenStream("slartibartfast"), + output: tokenStream("slartibartfast"), + }, + } + + for _, test := range tests { + ccFilter := NewFilter() + actual := ccFilter.Filter(test.input) + if !reflect.DeepEqual(actual, test.output) { + t.Errorf("expected %s \n\n got %s", test.output, actual) + } + } +} + +func tokenStream(termStrs ...string) analysis.TokenStream { + tokenStream := make([]*analysis.Token, len(termStrs)) + index := 0 + for i, termStr := range termStrs { + tokenStream[i] = &analysis.Token{ + Term: []byte(termStr), + Position: i + 1, + Start: index, + End: index + len(termStr), + } + index += len(termStr) + } + + return analysis.TokenStream(tokenStream) +}