File: diacritics_test.go

package info (click to toggle)
lf 34%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,296 kB
  • sloc: sh: 126; makefile: 23; csh: 4
file content (71 lines) | stat: -rw-r--r-- 3,821 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main

import (
	"testing"
)

// typical czech test sentence ;-)
const baseTestString = "Příliš žluťoučký kůň příšerně úpěl ďábelské ódy"

func TestRemoveDiacritics(t *testing.T) {
	testStr := baseTestString
	expStr := "Prilis zlutoucky kun priserne upel dabelske ody"
	checkRemoveDiacritics(testStr, expStr, t)

	// other accents (non complete, but all I found)
	testStr = "áéíóúýčďěňřšťžůåøĉĝĥĵŝŭšžõäöüàâçéèêëîïôùûüÿžščćđáéíóúąęėįųūčšžāēīūčšžļķņģáéíóúöüőűäöüëïąćęłńóśźżáàãâçéêíóõôăâîșțáäčďéíĺľňóôŕšťúýžáéíñóúüåäöâçîşûğăâđêôơưáàãảạ"
	expStr = "aeiouycdenrstzuaocghjsuszoaouaaceeeeiiouuuyzsccdaeiouaeeiuucszaeiucszlkngaeiouououaoueiacelnoszzaaaaceeioooaaistaacdeillnoorstuyzaeinouuaaoacisugaadeoouaaaaa"
	checkRemoveDiacritics(testStr, expStr, t)

	testStr = "ÁÉÍÓÚÝČĎĚŇŘŠŤŽŮÅØĈĜĤĴŜŬŠŽÕÄÖÜÀÂÇÉÈÊËÎÏÔÙÛÜŸŽŠČĆĐÁÉÍÓÚĄĘĖĮŲŪČŠŽĀĒĪŪČŠŽĻĶŅĢÁÉÍÓÚÖÜŐŰÄÖÜËÏĄĆĘŁŃÓŚŹŻÁÀÃÂÇÉÊÍÓÕÔĂÂÎȘȚÁÄČĎÉÍĹĽŇÓÔŔŠŤÚÝŽÁÉÍÑÓÚÜÅÄÖÂÇÎŞÛĞĂÂĐÊÔƠƯÁÀÃẢẠ"
	expStr = "AEIOUYCDENRSTZUAOCGHJSUSZOAOUAACEEEEIIOUUUYZSCCDAEIOUAEEIUUCSZAEIUCSZLKNGAEIOUOUOUAOUEIACELNOSZZAAAACEEIOOOAAISTAACDEILLNOORSTUYZAEINOUUAAOACISUGAADEOOUAAAAA"
	checkRemoveDiacritics(testStr, expStr, t)

	testStr = "áạàảãăắặằẳẵâấậầẩẫéẹèẻẽêếệềểễiíịìỉĩoóọòỏõôốộồổỗơớợờởỡúụùủũưứựừửữyýỵỳỷỹđ"
	expStr = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeiiiiiioooooooooooooooooouuuuuuuuuuuyyyyyyd"
	checkRemoveDiacritics(testStr, expStr, t)

	testStr = "ÁẠÀẢÃĂẮẶẰẲẴÂẤẬẦẨẪÉẸÈẺẼÊẾỆỀỂỄÍỊÌỈĨÓỌÒỎÕÔỐỘỒỔỖƠỚỢỜỞỠÚỤÙỦŨƯỨỰỪỬỮÝỴỲỶỸĐ"
	expStr = "AAAAAAAAAAAAAAAAAEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYD"
	checkRemoveDiacritics(testStr, expStr, t)
}

func checkRemoveDiacritics(testStr string, expStr string, t *testing.T) {
	resultStr := removeDiacritics(testStr)
	if resultStr != expStr {
		t.Errorf("at input '%v' expected '%v' but got '%v'", testStr, expStr, resultStr)
	}
}

func TestSearchSettings(t *testing.T) {
	runSearch(t, true, false, true, true, "Veřejný", "vere", true)

	runSearch(t, true, false, true, false, baseTestString, "Zlutoucky", true)
	runSearch(t, true, false, true, false, baseTestString, "zlutoucky", true)
	runSearch(t, true, true, true, false, baseTestString, "Zlutoucky", false)
	runSearch(t, true, true, true, true, baseTestString, "zlutoucky", true)

	runSearch(t, false, false, true, false, baseTestString, "žlutoucky", true)
	runSearch(t, false, false, true, false, baseTestString, "Žlutoucky", false)

	runSearch(t, false, false, true, true, baseTestString, "žluťoučký", true)
	runSearch(t, false, false, true, false, baseTestString, "žluťoučký", true)
	runSearch(t, false, false, false, false, baseTestString, "žluťoučký", true)
	runSearch(t, false, false, false, false, baseTestString, "zlutoucky", false)
	runSearch(t, false, false, true, true, baseTestString, "zlutoucky", true)
}

func runSearch(t *testing.T, ignorecase, smartcase, ignorediacritics, smartdiacritics bool, base, pattern string, expected bool) {
	gOpts.ignorecase = ignorecase
	gOpts.smartcase = smartcase
	gOpts.ignoredia = ignorediacritics
	gOpts.smartdia = smartdiacritics
	matched, _ := searchMatch(base, pattern, false)
	if matched != expected {
		t.Errorf("False search for ignorecase = %t, smartcase = %t, ignoredia = %t, smartdia = %t",
			gOpts.ignorecase,
			gOpts.smartcase,
			gOpts.ignoredia,
			gOpts.smartdia)
	}
}