gitee.com/quant1x/gox@v1.21.2/text/runewidth/runewidth_posix_test.go (about)

     1  //go:build !windows && !js && !appengine
     2  // +build !windows,!js,!appengine
     3  
     4  package runewidth
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  type envVars struct {
    12  	lang    string
    13  	lcall   string
    14  	lcctype string
    15  }
    16  
    17  func saveEnv() envVars {
    18  	return envVars{
    19  		lang:    os.Getenv("LANG"),
    20  		lcall:   os.Getenv("LC_ALL"),
    21  		lcctype: os.Getenv("LC_CTYPE"),
    22  	}
    23  }
    24  func restoreEnv(env *envVars) {
    25  	os.Setenv("LANG", env.lang)
    26  	os.Setenv("LC_ALL", env.lcall)
    27  	os.Setenv("LC_CTYPE", env.lcctype)
    28  }
    29  
    30  func TestIsEastAsian(t *testing.T) {
    31  	testcases := []struct {
    32  		locale string
    33  		want   bool
    34  	}{
    35  		{"foo@cjk_narrow", false},
    36  		{"foo@cjk", false},
    37  		{"utf-8@cjk", false},
    38  		{"ja_JP.CP932", true},
    39  	}
    40  
    41  	for _, tt := range testcases {
    42  		got := isEastAsian(tt.locale)
    43  		if got != tt.want {
    44  			t.Fatalf("isEastAsian(%q) should be %v", tt.locale, tt.want)
    45  		}
    46  	}
    47  }
    48  
    49  func TestIsEastAsianLCCTYPE(t *testing.T) {
    50  	env := saveEnv()
    51  	defer restoreEnv(&env)
    52  	os.Setenv("LC_ALL", "")
    53  
    54  	testcases := []struct {
    55  		lcctype string
    56  		want    bool
    57  	}{
    58  		{"ja_JP.UTF-8", true},
    59  		{"C", false},
    60  		{"POSIX", false},
    61  		{"en_US.UTF-8", false},
    62  	}
    63  
    64  	for _, tt := range testcases {
    65  		os.Setenv("LC_CTYPE", tt.lcctype)
    66  		got := IsEastAsian()
    67  		if got != tt.want {
    68  			t.Fatalf("IsEastAsian() for LC_CTYPE=%v should be %v", tt.lcctype, tt.want)
    69  		}
    70  	}
    71  }
    72  
    73  func TestIsEastAsianLANG(t *testing.T) {
    74  	env := saveEnv()
    75  	defer restoreEnv(&env)
    76  	os.Setenv("LC_ALL", "")
    77  	os.Setenv("LC_CTYPE", "")
    78  
    79  	testcases := []struct {
    80  		lcctype string
    81  		want    bool
    82  	}{
    83  		{"ja_JP.UTF-8", true},
    84  		{"C", false},
    85  		{"POSIX", false},
    86  		{"en_US.UTF-8", false},
    87  		{"C.UTF-8", false},
    88  	}
    89  
    90  	for _, tt := range testcases {
    91  		os.Setenv("LANG", tt.lcctype)
    92  		got := IsEastAsian()
    93  		if got != tt.want {
    94  			t.Fatalf("IsEastAsian() for LANG=%v should be %v", tt.lcctype, tt.want)
    95  		}
    96  	}
    97  }