github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/encoding/charmap/charmap_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 charmap
     6  
     7  import (
     8  	"testing"
     9  
    10  	"golang.org/x/text/encoding"
    11  	"golang.org/x/text/encoding/internal"
    12  	"golang.org/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, Windows1252, "\x81", "\ufffd"},
    29  
    30  		{enc, Windows1252, "갂", ""},
    31  		{enc, Windows1252, "a갂", "a"},
    32  		{enc, Windows1252, "\u00E9갂", "\xE9"},
    33  	}
    34  	for _, tc := range testCases {
    35  		dir, tr, wantErr := tc.init(tc.e)
    36  
    37  		dst, _, err := transform.String(tr, tc.src)
    38  		if err != wantErr {
    39  			t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, wantErr)
    40  		}
    41  		if got := string(dst); got != tc.want {
    42  			t.Errorf("%s %v(%q):\ngot  %q\nwant %q", dir, tc.e, tc.src, got, tc.want)
    43  		}
    44  	}
    45  }