...

Source file src/golang.org/x/text/language/coverage_test.go

Documentation: golang.org/x/text/language

     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  
     5  package language
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"golang.org/x/text/internal/language"
    13  )
    14  
    15  func TestSupported(t *testing.T) {
    16  	// To prove the results are correct for a type, we test that the number of
    17  	// results is identical to the number of results on record, that all results
    18  	// are distinct and that all results are valid.
    19  	tests := map[string]int{
    20  		"BaseLanguages": language.NumLanguages,
    21  		"Scripts":       language.NumScripts,
    22  		"Regions":       language.NumRegions,
    23  		"Tags":          0,
    24  	}
    25  	sup := reflect.ValueOf(Supported)
    26  	for name, num := range tests {
    27  		v := sup.MethodByName(name).Call(nil)[0]
    28  		if n := v.Len(); n != num {
    29  			t.Errorf("len(%s()) was %d; want %d", name, n, num)
    30  		}
    31  		dup := make(map[string]bool)
    32  		for i := 0; i < v.Len(); i++ {
    33  			x := v.Index(i).Interface()
    34  			// An invalid value will either cause a crash or result in a
    35  			// duplicate when passed to Sprint.
    36  			s := fmt.Sprint(x)
    37  			if dup[s] {
    38  				t.Errorf("%s: duplicate entry %q", name, s)
    39  			}
    40  			dup[s] = true
    41  		}
    42  		if len(dup) != v.Len() {
    43  			t.Errorf("%s: # unique entries was %d; want %d", name, len(dup), v.Len())
    44  		}
    45  	}
    46  }
    47  
    48  func TestNewCoverage(t *testing.T) {
    49  	bases := []Base{Base{0}, Base{3}, Base{7}}
    50  	scripts := []Script{Script{11}, Script{17}, Script{23}}
    51  	regions := []Region{Region{101}, Region{103}, Region{107}}
    52  	tags := []Tag{Make("pt"), Make("en"), Make("en-GB"), Make("en-US"), Make("pt-PT")}
    53  	fbases := func() []Base { return bases }
    54  	fscripts := func() []Script { return scripts }
    55  	fregions := func() []Region { return regions }
    56  	ftags := func() []Tag { return tags }
    57  
    58  	tests := []struct {
    59  		desc    string
    60  		list    []interface{}
    61  		bases   []Base
    62  		scripts []Script
    63  		regions []Region
    64  		tags    []Tag
    65  	}{
    66  		{
    67  			desc: "empty",
    68  		},
    69  		{
    70  			desc:  "bases",
    71  			list:  []interface{}{bases},
    72  			bases: bases,
    73  		},
    74  		{
    75  			desc:    "scripts",
    76  			list:    []interface{}{scripts},
    77  			scripts: scripts,
    78  		},
    79  		{
    80  			desc:    "regions",
    81  			list:    []interface{}{regions},
    82  			regions: regions,
    83  		},
    84  		{
    85  			desc:  "bases derives from tags",
    86  			list:  []interface{}{tags},
    87  			bases: []Base{Base{_en}, Base{_pt}},
    88  			tags:  tags,
    89  		},
    90  		{
    91  			desc:  "tags and bases",
    92  			list:  []interface{}{tags, bases},
    93  			bases: bases,
    94  			tags:  tags,
    95  		},
    96  		{
    97  			desc:    "fully specified",
    98  			list:    []interface{}{tags, bases, scripts, regions},
    99  			bases:   bases,
   100  			scripts: scripts,
   101  			regions: regions,
   102  			tags:    tags,
   103  		},
   104  		{
   105  			desc:  "bases func",
   106  			list:  []interface{}{fbases},
   107  			bases: bases,
   108  		},
   109  		{
   110  			desc:    "scripts func",
   111  			list:    []interface{}{fscripts},
   112  			scripts: scripts,
   113  		},
   114  		{
   115  			desc:    "regions func",
   116  			list:    []interface{}{fregions},
   117  			regions: regions,
   118  		},
   119  		{
   120  			desc:  "tags func",
   121  			list:  []interface{}{ftags},
   122  			bases: []Base{Base{_en}, Base{_pt}},
   123  			tags:  tags,
   124  		},
   125  		{
   126  			desc:  "tags and bases",
   127  			list:  []interface{}{ftags, fbases},
   128  			bases: bases,
   129  			tags:  tags,
   130  		},
   131  		{
   132  			desc:    "fully specified",
   133  			list:    []interface{}{ftags, fbases, fscripts, fregions},
   134  			bases:   bases,
   135  			scripts: scripts,
   136  			regions: regions,
   137  			tags:    tags,
   138  		},
   139  	}
   140  
   141  	for i, tt := range tests {
   142  		l := NewCoverage(tt.list...)
   143  		if a := l.BaseLanguages(); !reflect.DeepEqual(a, tt.bases) {
   144  			t.Errorf("%d:%s: BaseLanguages was %v; want %v", i, tt.desc, a, tt.bases)
   145  		}
   146  		if a := l.Scripts(); !reflect.DeepEqual(a, tt.scripts) {
   147  			t.Errorf("%d:%s: Scripts was %v; want %v", i, tt.desc, a, tt.scripts)
   148  		}
   149  		if a := l.Regions(); !reflect.DeepEqual(a, tt.regions) {
   150  			t.Errorf("%d:%s: Regions was %v; want %v", i, tt.desc, a, tt.regions)
   151  		}
   152  		if a := l.Tags(); !reflect.DeepEqual(a, tt.tags) {
   153  			t.Errorf("%d:%s: Tags was %v; want %v", i, tt.desc, a, tt.tags)
   154  		}
   155  	}
   156  }
   157  

View as plain text