Debian Codegraph @

utf8_test.go

raw symbols SCIP
1 // Copyright 2024 Garrett D'Amore
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
15 package encoding
16
17 import (
18 "errors"
19 "testing"
20
21 "golang.org/x/text/transform"
22 )
23
24 func TestUTF8(t *testing.T) {
25 t.Logf("ASCII UTF8 identity transforms")
26 for i := 0; i < 128; i++ {
27 verifyMap(t, UTF8, byte(i), rune(i))
28 }
29 }
30
31 func TestDstShortUTF8Decoder(t *testing.T) {
32 decoder := ISO8859_1.NewDecoder()
33
34 out := make([]byte, 1)
35 nat := []byte{0xC0} // Latin1 A grave
36
37 _, _, err := decoder.Transform(out, nat, true)
38 if err == nil {
39 t.Errorf("Passed but should not have")
40 } else if !errors.Is(err, transform.ErrShortDst) {
41 t.Errorf("Wrong error return: %v", err)
42 }
43 }
44
45 func TestDstShortUTF8Encoder(t *testing.T) {
46
47 encoder := UTF8.NewEncoder()
48 nat := []byte("À")
49 out := make([]byte, 1)
50 _, _, err := encoder.Transform(out, nat, true)
51 if err == nil {
52 t.Errorf("Passed but should not have")
53 } else if !errors.Is(err, transform.ErrShortDst) {
54 t.Errorf("Wrong error return: %v", err)
55 }
56 }
57
58 func TestSrcShortUTF8(t *testing.T) {
59 encoder := ISO8859_1.NewEncoder()
60
61 out := make([]byte, 2)
62 nat := []byte("À")
63 nat = nat[0:1]
64
65 _, _, err := encoder.Transform(out, nat, true)
66 if err == nil {
67 t.Errorf("Passed but should not have: %v", nat)
68 } else if !errors.Is(err, transform.ErrShortSrc) {
69 t.Errorf("Wrong error return: %v %v", nat, err)
70 }
71 }