File: charset_test.go

package info (click to toggle)
golang-github-emersion-go-message 0.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 348 kB
  • sloc: makefile: 2
file content (88 lines) | stat: -rw-r--r-- 1,926 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package charset

import (
	"bytes"
	"io/ioutil"
	"strings"
	"testing"
)

var testCharsets = []struct {
	charset string
	encoded []byte
	decoded string
}{
	{
		charset: "us-ascii",
		encoded: []byte("yuudachi"),
		decoded: "yuudachi",
	},
	{
		charset: "utf-8",
		encoded: []byte("café"),
		decoded: "café",
	},
	{
		charset: "utf8",
		encoded: []byte("café"),
		decoded: "café",
	},
	{
		charset: "windows-1250",
		encoded: []byte{0x8c, 0x8d, 0x8f, 0x9c, 0x9d, 0x9f, 0xbc, 0xbe},
		decoded: "ŚŤŹśťźĽľ",
	},
	{
		charset: "windows-1252",
		encoded: []byte{0x63, 0x61, 0x66, 0xE9, 0x20, 0x80},
		decoded: "café €",
	},
	{
		charset: "iso-8859-1",
		encoded: []byte{0x63, 0x61, 0x66, 0xE9},
		decoded: "café",
	},
	{
		charset: "idontexist",
		encoded: []byte{42},
	},
	{
		charset: "gb2312",
		encoded: []byte{178, 226, 202, 212},
		decoded: "测试",
	},
	{
		charset: "iso8859-2",
		encoded: []byte{0x63, 0x61, 0x66, 0xE9, 0x20, 0xfb},
		decoded: "café ű",
	},
}

func TestCharsetReader(t *testing.T) {
	for _, test := range testCharsets {
		r, err := Reader(test.charset, bytes.NewReader(test.encoded))
		if test.decoded == "" {
			if err == nil {
				t.Errorf("Expected an error when creating reader for charset %q", test.charset)
			}
		}
		if test.decoded != "" {
			if err != nil {
				t.Errorf("Expected no error when creating reader for charset %q, but got: %v", test.charset, err)
			} else if b, err := ioutil.ReadAll(r); err != nil {
				t.Errorf("Expected no error when reading charset %q, but got: %v", test.charset, err)
			} else if s := string(b); s != test.decoded {
				t.Errorf("Expected decoded text to be %q but got %q", test.decoded, s)
			}
		}
	}
}

func TestDisabledCharsetReader(t *testing.T) {
	charsets["DISABLED"] = nil

	_, err := Reader("DISABLED", strings.NewReader("Some dummy text"))
	if err == nil {
		t.Errorf("Reader(): expected disabled charset to return an error")
	}
}