github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/x/text/encoding/japanese/all_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 japanese
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/insionng/yougam/libraries/x/text/encoding"
    11  	"github.com/insionng/yougam/libraries/x/text/encoding/internal"
    12  	"github.com/insionng/yougam/libraries/x/text/transform"
    13  )
    14  
    15  func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
    16  	return "Decode", e.NewDecoder(), nil
    17  }
    18  func enc(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
    19  	return "Encode", e.NewEncoder(), internal.ErrASCIIReplacement
    20  }
    21  
    22  func TestNonRepertoire(t *testing.T) {
    23  	testCases := []struct {
    24  		init      func(e encoding.Encoding) (string, transform.Transformer, error)
    25  		e         encoding.Encoding
    26  		src, want string
    27  	}{
    28  		{dec, EUCJP, "\xfe\xfc", "\ufffd"},
    29  		{dec, ISO2022JP, "\x1b$B\x7e\x7e", "\ufffd"},
    30  		{dec, ShiftJIS, "\xef\xfc", "\ufffd"},
    31  
    32  		{enc, EUCJP, "갂", ""},
    33  		{enc, EUCJP, "a갂", "a"},
    34  		{enc, EUCJP, "丌갂", "\x8f\xb0\xa4"},
    35  
    36  		{enc, ISO2022JP, "갂", ""},
    37  		{enc, ISO2022JP, "a갂", "a"},
    38  		{enc, ISO2022JP, "朗갂", "\x1b$BzF\x1b(B"}, // switch back to ASCII mode at end
    39  
    40  		{enc, ShiftJIS, "갂", ""},
    41  		{enc, ShiftJIS, "a갂", "a"},
    42  		{enc, ShiftJIS, "\u2190갂", "\x81\xa9"},
    43  	}
    44  	for _, tc := range testCases {
    45  		dir, tr, wantErr := tc.init(tc.e)
    46  
    47  		dst, _, err := transform.String(tr, tc.src)
    48  		if err != wantErr {
    49  			t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, wantErr)
    50  		}
    51  		if got := string(dst); got != tc.want {
    52  			t.Errorf("%s %v(%q):\ngot  %q\nwant %q", dir, tc.e, tc.src, got, tc.want)
    53  		}
    54  	}
    55  }
    56  
    57  func TestCorrect(t *testing.T) {
    58  	testCases := []struct {
    59  		init      func(e encoding.Encoding) (string, transform.Transformer, error)
    60  		e         encoding.Encoding
    61  		src, want string
    62  	}{
    63  		{dec, ShiftJIS, "\x9f\xfc", "滌"},
    64  		{dec, ShiftJIS, "\xfb\xfc", "髙"},
    65  		{dec, ShiftJIS, "\xfa\xb1", "﨑"},
    66  		{enc, ShiftJIS, "滌", "\x9f\xfc"},
    67  		{enc, ShiftJIS, "﨑", "\xed\x95"},
    68  	}
    69  	for _, tc := range testCases {
    70  		dir, tr, _ := tc.init(tc.e)
    71  
    72  		dst, _, err := transform.String(tr, tc.src)
    73  		if err != nil {
    74  			t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, nil)
    75  		}
    76  		if got := string(dst); got != tc.want {
    77  			t.Errorf("%s %v(%q):\ngot  %q\nwant %q", dir, tc.e, tc.src, got, tc.want)
    78  		}
    79  	}
    80  }