github.com/liquid-dev/text@v0.3.3-liquid/internal/match_test.go (about)

     1  // Copyright 2015 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  
     5  package internal
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/liquid-dev/text/language"
    12  )
    13  
    14  func TestInheritanceMatcher(t *testing.T) {
    15  	for i, tt := range []struct {
    16  		haveTags string
    17  		wantTags string
    18  		match    string
    19  		conf     language.Confidence
    20  	}{
    21  		{"und,en,en-US", "en-US", "en-US", language.Exact}, // most specific match
    22  		{"zh-Hant,zh", "zh-TW", "zh-Hant", language.High},  // zh-TW implies Hant.
    23  		{"und,zh", "zh-TW", "und", language.High},          // zh-TW does not match zh.
    24  		{"zh", "zh-TW", "und", language.No},                // zh-TW does not match zh.
    25  		{"iw,en,nl", "he", "he", language.Exact},           // matches after canonicalization
    26  		{"he,en,nl", "iw", "he", language.Exact},           // matches after canonicalization
    27  		// Prefer first match over more specific match for various reasons:
    28  		// a) consistency of user interface is more important than an exact match,
    29  		// b) _if_ und is specified, it should be considered a correct and useful match,
    30  		// Note that a call to this Match will almost always be with a single tag.
    31  		{"und,en,en-US", "he,en-US", "und", language.High},
    32  	} {
    33  		have := parseTags(tt.haveTags)
    34  		m := NewInheritanceMatcher(have)
    35  		tag, index, conf := m.Match(parseTags(tt.wantTags)...)
    36  		want := language.Raw.Make(tt.match)
    37  		if tag != want {
    38  			t.Errorf("%d:tag: got %q; want %q", i, tag, want)
    39  		}
    40  		if conf != language.No {
    41  			if got, _ := language.All.Canonicalize(have[index]); got != want {
    42  				t.Errorf("%d:index: got %q; want %q ", i, got, want)
    43  			}
    44  		}
    45  		if conf != tt.conf {
    46  			t.Errorf("%d:conf: got %v; want %v", i, conf, tt.conf)
    47  		}
    48  	}
    49  }
    50  
    51  func parseTags(list string) (out []language.Tag) {
    52  	for _, s := range strings.Split(list, ",") {
    53  		out = append(out, language.Raw.Make(strings.TrimSpace(s)))
    54  	}
    55  	return out
    56  }