Skip to content

Commit

Permalink
Add tests for Linux (codepage=65001 only)
Browse files Browse the repository at this point in the history
  • Loading branch information
hymkor committed Feb 7, 2023
1 parent f6e1d70 commit 24c2fb8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 57 deletions.
33 changes: 0 additions & 33 deletions encoder_windows_test.go

This file was deleted.

18 changes: 12 additions & 6 deletions autodetect_windows_test.go → encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/nyaosorg/go-windows-mbcs"
)

func testFiles(t *testing.T, aFilePath, uFilePath string) {
func testFiles(t *testing.T, aFilePath, uFilePath string, tr transform.Transformer) {
srcFd, err := os.Open(aFilePath)
if err != nil {
t.Fatal(err.Error())
Expand All @@ -22,7 +22,7 @@ func testFiles(t *testing.T, aFilePath, uFilePath string) {
if err != nil {
t.Fatal(err.Error())
}
r := transform.NewReader(srcFd, mbcs.AutoDecoder{CP: 932})
r := transform.NewReader(srcFd, tr)
resultUtf8, err := io.ReadAll(r)
if err != nil {
t.Fatal(err.Error())
Expand All @@ -32,10 +32,16 @@ func testFiles(t *testing.T, aFilePath, uFilePath string) {
}
}

func TestAutoDecoderFromCP932ToUTF8(t *testing.T) {
testFiles(t, "testdata/jugemu-cp932.txt", "testdata/jugemu-utf8.txt")
func TestAutoDecoderFromUTF8ToUTF8On65001(t *testing.T) {
testFiles(t,
"testdata/jugemu-utf8.txt",
"testdata/jugemu-utf8.txt",
mbcs.AutoDecoder{CP: 65001})
}

func TestAutoDecoderFromUTF8ToUTF8(t *testing.T) {
testFiles(t, "testdata/jugemu-utf8.txt", "testdata/jugemu-utf8.txt")
func TestDecoderFromUTF8ToUTF8On65001(t *testing.T) {
testFiles(t,
"testdata/jugemu-utf8.txt",
"testdata/jugemu-utf8.txt",
mbcs.Decoder{CP: 65001})
}
43 changes: 25 additions & 18 deletions decoder_windows_test.go → encoding_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import (
"github.com/nyaosorg/go-windows-mbcs"
)

func TestDecoderByReader(t *testing.T) {
srcFd, err := os.Open("testdata/jugemu-cp932.txt")
if err != nil {
t.Fatal(err.Error())
}
defer srcFd.Close()
func TestAutoDecoderFromCP932ToUTF8(t *testing.T) {
testFiles(t,
"testdata/jugemu-cp932.txt",
"testdata/jugemu-utf8.txt",
mbcs.AutoDecoder{CP: 932})
}

expectUtf8, err := os.ReadFile("testdata/jugemu-utf8.txt")
if err != nil {
t.Fatal(err.Error())
}
r := transform.NewReader(srcFd, mbcs.Decoder{CP: 932})
resultUtf8, err := io.ReadAll(r)
if err != nil {
t.Fatal(err.Error())
}
if !bytes.Equal(expectUtf8, resultUtf8) {
t.Fatalf("not equal:\n%#v\nand\n%#v", expectUtf8, resultUtf8)
}
func TestAutoDecoderFromUTF8ToUTF8(t *testing.T) {
testFiles(t,
"testdata/jugemu-utf8.txt",
"testdata/jugemu-utf8.txt",
mbcs.AutoDecoder{CP: 932})
}

func TestDecoderByReader(t *testing.T) {
testFiles(t,
"testdata/jugemu-cp932.txt",
"testdata/jugemu-utf8.txt",
mbcs.Decoder{CP: 932})
}

func TestDecoderByWriter(t *testing.T) {
Expand All @@ -54,3 +54,10 @@ func TestDecoderByWriter(t *testing.T) {
t.Fatalf("not equal:\n%#v\nand\n%#v", expectUtf8, resultUtf8)
}
}

func TestEncoder(t *testing.T) {
testFiles(t,
"testdata/jugemu-utf8.txt",
"testdata/jugemu-cp932.txt",
mbcs.Encoder{CP: 932})
}

0 comments on commit 24c2fb8

Please sign in to comment.