github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/text/language/coverage_test.go (about)

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