...

Source file src/github.com/gdamore/tcell/v2/encoding/encoding_init_test.go

Documentation: github.com/gdamore/tcell/v2/encoding

     1  // Copyright 2022 The TCell Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use file except in compliance with the License.
     5  // You may obtain a copy of the license at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package encoding
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/gdamore/tcell/v2"
    20  )
    21  
    22  func TestGBK(t *testing.T) {
    23  	enc := tcell.GetEncoding("GBK")
    24  	if enc == nil {
    25  		t.Fatal("NULL encoding for GBK")
    26  	}
    27  	glyph, _ := enc.NewDecoder().Bytes([]byte{0x82, 0x74})
    28  	if string(glyph) != "倀" {
    29  		t.Errorf("failed to match: %s != 倀", string(glyph))
    30  	}
    31  }
    32  
    33  func TestAscii(t *testing.T) {
    34  	encodings := []string{
    35  		"ASCII",
    36  		"ISO-8859-1",
    37  		"KOI8-R",
    38  		"KOI8-U",
    39  		"SJIS",
    40  		"Big5",
    41  		"GB2312",
    42  		"GB18030",
    43  		"EUC-JP",
    44  		"EUCKR",
    45  	}
    46  
    47  	for _, name := range encodings {
    48  		t.Run(name, func(t *testing.T) {
    49  			enc := tcell.GetEncoding(name)
    50  			if enc == nil {
    51  				t.Errorf("Failed getting encoding for %s", name)
    52  				return
    53  			}
    54  			encoder := enc.NewEncoder()
    55  			decoder := enc.NewDecoder()
    56  			// Ensure that all US-ASCII (lower 7 bit values) encode and decode identically
    57  			for i := byte(0); i < 126; i++ { // well, KOI8-R has some problem with "~"
    58  				s := string([]byte{i})
    59  				if x, err := encoder.String(s); err != nil || x != s {
    60  					t.Errorf("failed encoding for character: %d, err %v expect %s got %s", i, err, s, x)
    61  				}
    62  				if x, err := decoder.String(s); err != nil || x != s {
    63  					t.Errorf("failed decoding for character: %d, err %v expect %s got %s", i, err, s, x)
    64  				}
    65  			}
    66  		})
    67  	}
    68  }
    69  

View as plain text