File: unicode_test.go

package info (click to toggle)
golang-github-dromara-dongle 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,440 kB
  • sloc: makefile: 4
file content (148 lines) | stat: -rw-r--r-- 4,004 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package coding

import (
	"testing"

	"github.com/dromara/dongle/internal/mock"
	"github.com/stretchr/testify/assert"
)

func TestEncoder_ByUnicode(t *testing.T) {
	t.Run("encode string", func(t *testing.T) {
		encoder := NewEncoder().FromString("hello")
		result := encoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Equal(t, []byte("hello"), result.dst)
	})

	t.Run("encode unicode string", func(t *testing.T) {
		encoder := NewEncoder().FromString("你好世界")
		result := encoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Equal(t, []byte(`\u4f60\u597d\u4e16\u754c`), result.dst)
	})

	t.Run("encode bytes", func(t *testing.T) {
		encoder := NewEncoder().FromBytes([]byte("hello"))
		result := encoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Equal(t, []byte("hello"), result.dst)
	})

	t.Run("encode empty string", func(t *testing.T) {
		encoder := NewEncoder().FromString("")
		result := encoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Empty(t, result.dst)
	})

	t.Run("encode empty bytes", func(t *testing.T) {
		encoder := NewEncoder().FromBytes([]byte{})
		result := encoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Empty(t, result.dst)
	})

	t.Run("encode from file", func(t *testing.T) {
		file := mock.NewFile([]byte("hello"), "test.txt")
		defer file.Close()

		encoder := NewEncoder().FromFile(file)
		result := encoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Equal(t, []byte("hello"), result.dst)
	})

	t.Run("encode with existing error", func(t *testing.T) {
		encoder := Encoder{Error: assert.AnError}
		result := encoder.ByUnicode()
		assert.Equal(t, assert.AnError, result.Error)
	})
}

func TestDecoder_ByUnicode(t *testing.T) {
	t.Run("decode string", func(t *testing.T) {
		decoder := NewDecoder().FromString("hello")
		result := decoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Equal(t, []byte("hello"), result.dst)
	})

	t.Run("decode unicode string", func(t *testing.T) {
		decoder := NewDecoder().FromString(`\u4f60\u597d\u4e16\u754c`)
		result := decoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Equal(t, []byte("你好世界"), result.dst)
	})

	t.Run("decode bytes", func(t *testing.T) {
		decoder := NewDecoder().FromBytes([]byte("hello"))
		result := decoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Equal(t, []byte("hello"), result.dst)
	})

	t.Run("decode empty string", func(t *testing.T) {
		decoder := NewDecoder().FromString("")
		result := decoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Empty(t, result.dst)
	})

	t.Run("decode empty bytes", func(t *testing.T) {
		decoder := NewDecoder().FromBytes([]byte{})
		result := decoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Empty(t, result.dst)
	})

	t.Run("decode from file", func(t *testing.T) {
		file := mock.NewFile([]byte("hello"), "test.txt")
		defer file.Close()

		decoder := NewDecoder().FromFile(file)
		result := decoder.ByUnicode()
		assert.Nil(t, result.Error)
		assert.Equal(t, []byte("hello"), result.dst)
	})

	t.Run("decode invalid unicode", func(t *testing.T) {
		decoder := NewDecoder().FromString(`\uZZZZ`)
		result := decoder.ByUnicode()
		assert.NotNil(t, result.Error)
	})

	t.Run("decode with existing error", func(t *testing.T) {
		decoder := Decoder{Error: assert.AnError}
		result := decoder.ByUnicode()
		assert.Equal(t, assert.AnError, result.Error)
	})
}

func TestUnicodeRoundTrip(t *testing.T) {
	testCases := []struct {
		name string
		data string
	}{
		{"empty", ""},
		{"simple", "hello"},
		{"unicode", "你好世界"},
		{"mixed", "hello 世界"},
		{"special", "hello\nworld\t"},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Encode
			encoder := NewEncoder().FromString(tc.data)
			encoded := encoder.ByUnicode()
			assert.Nil(t, encoded.Error)

			// Decode
			decoder := NewDecoder().FromBytes(encoded.dst)
			decoded := decoder.ByUnicode()
			assert.Nil(t, decoded.Error)
			assert.Equal(t, tc.data, decoded.ToString())
		})
	}
}