...

Source file src/golang.org/x/text/collate/option_test.go

Documentation: golang.org/x/text/collate

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  package collate
     5  
     6  import (
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  
    11  	"golang.org/x/text/internal/colltab"
    12  	"golang.org/x/text/language"
    13  )
    14  
    15  var (
    16  	defaultIgnore = ignore(colltab.Tertiary)
    17  	defaultTable  = getTable(locales[0])
    18  )
    19  
    20  func TestOptions(t *testing.T) {
    21  	for i, tt := range []struct {
    22  		in  []Option
    23  		out options
    24  	}{
    25  		0: {
    26  			out: options{
    27  				ignore: defaultIgnore,
    28  			},
    29  		},
    30  		1: {
    31  			in: []Option{IgnoreDiacritics},
    32  			out: options{
    33  				ignore: [colltab.NumLevels]bool{false, true, false, true, true},
    34  			},
    35  		},
    36  		2: {
    37  			in: []Option{IgnoreCase, IgnoreDiacritics},
    38  			out: options{
    39  				ignore: ignore(colltab.Primary),
    40  			},
    41  		},
    42  		3: {
    43  			in: []Option{ignoreDiacritics, IgnoreWidth},
    44  			out: options{
    45  				ignore:    ignore(colltab.Primary),
    46  				caseLevel: true,
    47  			},
    48  		},
    49  		4: {
    50  			in: []Option{IgnoreWidth, ignoreDiacritics},
    51  			out: options{
    52  				ignore:    ignore(colltab.Primary),
    53  				caseLevel: true,
    54  			},
    55  		},
    56  		5: {
    57  			in: []Option{IgnoreCase, IgnoreWidth},
    58  			out: options{
    59  				ignore: ignore(colltab.Secondary),
    60  			},
    61  		},
    62  		6: {
    63  			in: []Option{IgnoreCase, IgnoreWidth, Loose},
    64  			out: options{
    65  				ignore: ignore(colltab.Primary),
    66  			},
    67  		},
    68  		7: {
    69  			in: []Option{Force, IgnoreCase, IgnoreWidth, Loose},
    70  			out: options{
    71  				ignore: [colltab.NumLevels]bool{false, true, true, true, false},
    72  			},
    73  		},
    74  		8: {
    75  			in: []Option{IgnoreDiacritics, IgnoreCase},
    76  			out: options{
    77  				ignore: ignore(colltab.Primary),
    78  			},
    79  		},
    80  		9: {
    81  			in: []Option{Numeric},
    82  			out: options{
    83  				ignore:  defaultIgnore,
    84  				numeric: true,
    85  			},
    86  		},
    87  		10: {
    88  			in: []Option{OptionsFromTag(language.MustParse("und-u-ks-level1"))},
    89  			out: options{
    90  				ignore: ignore(colltab.Primary),
    91  			},
    92  		},
    93  		11: {
    94  			in: []Option{OptionsFromTag(language.MustParse("und-u-ks-level4"))},
    95  			out: options{
    96  				ignore: ignore(colltab.Quaternary),
    97  			},
    98  		},
    99  		12: {
   100  			in:  []Option{OptionsFromTag(language.MustParse("und-u-ks-identic"))},
   101  			out: options{},
   102  		},
   103  		13: {
   104  			in: []Option{
   105  				OptionsFromTag(language.MustParse("und-u-kn-true-kb-true-kc-true")),
   106  			},
   107  			out: options{
   108  				ignore:    defaultIgnore,
   109  				caseLevel: true,
   110  				backwards: true,
   111  				numeric:   true,
   112  			},
   113  		},
   114  		14: {
   115  			in: []Option{
   116  				OptionsFromTag(language.MustParse("und-u-kn-true-kb-true-kc-true")),
   117  				OptionsFromTag(language.MustParse("und-u-kn-false-kb-false-kc-false")),
   118  			},
   119  			out: options{
   120  				ignore: defaultIgnore,
   121  			},
   122  		},
   123  		15: {
   124  			in: []Option{
   125  				OptionsFromTag(language.MustParse("und-u-kn-true-kb-true-kc-true")),
   126  				OptionsFromTag(language.MustParse("und-u-kn-foo-kb-foo-kc-foo")),
   127  			},
   128  			out: options{
   129  				ignore:    defaultIgnore,
   130  				caseLevel: true,
   131  				backwards: true,
   132  				numeric:   true,
   133  			},
   134  		},
   135  		16: { // Normal options take precedence over tag options.
   136  			in: []Option{
   137  				Numeric, IgnoreCase,
   138  				OptionsFromTag(language.MustParse("und-u-kn-false-kc-true")),
   139  			},
   140  			out: options{
   141  				ignore:    ignore(colltab.Secondary),
   142  				caseLevel: false,
   143  				numeric:   true,
   144  			},
   145  		},
   146  		17: {
   147  			in: []Option{
   148  				OptionsFromTag(language.MustParse("und-u-ka-shifted")),
   149  			},
   150  			out: options{
   151  				ignore:    defaultIgnore,
   152  				alternate: altShifted,
   153  			},
   154  		},
   155  		18: {
   156  			in: []Option{
   157  				OptionsFromTag(language.MustParse("und-u-ka-blanked")),
   158  			},
   159  			out: options{
   160  				ignore:    defaultIgnore,
   161  				alternate: altBlanked,
   162  			},
   163  		},
   164  		19: {
   165  			in: []Option{
   166  				OptionsFromTag(language.MustParse("und-u-ka-posix")),
   167  			},
   168  			out: options{
   169  				ignore:    defaultIgnore,
   170  				alternate: altShiftTrimmed,
   171  			},
   172  		},
   173  	} {
   174  		c := newCollator(defaultTable)
   175  		c.t = nil
   176  		c.variableTop = 0
   177  		c.f = 0
   178  
   179  		c.setOptions(tt.in)
   180  		if !reflect.DeepEqual(c.options, tt.out) {
   181  			t.Errorf("%d: got %v; want %v", i, c.options, tt.out)
   182  		}
   183  	}
   184  }
   185  
   186  func TestAlternateSortTypes(t *testing.T) {
   187  	testCases := []struct {
   188  		lang string
   189  		in   []string
   190  		want []string
   191  	}{{
   192  		lang: "zh,cmn,zh-Hant-u-co-pinyin,zh-HK-u-co-pinyin,zh-pinyin",
   193  		in:   []string{"爸爸", "妈妈", "儿子", "女儿"},
   194  		want: []string{"爸爸", "儿子", "妈妈", "女儿"},
   195  	}, {
   196  		lang: "zh-Hant,zh-u-co-stroke,zh-Hant-u-co-stroke",
   197  		in:   []string{"爸爸", "妈妈", "儿子", "女儿"},
   198  		want: []string{"儿子", "女儿", "妈妈", "爸爸"},
   199  	}}
   200  	for _, tc := range testCases {
   201  		for _, tag := range strings.Split(tc.lang, ",") {
   202  			got := append([]string{}, tc.in...)
   203  			New(language.MustParse(tag)).SortStrings(got)
   204  			if !reflect.DeepEqual(got, tc.want) {
   205  				t.Errorf("New(%s).SortStrings(%v) = %v; want %v", tag, tc.in, got, tc.want)
   206  			}
   207  		}
   208  	}
   209  }
   210  

View as plain text