string_encoding_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package language
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
  4. "os"
  5. "path/filepath"
  6. "reflect"
  7. "testing"
  8. )
  9. func TestChangeFileCoding2UTF8(t *testing.T) {
  10. type args struct {
  11. subFileFPath string
  12. }
  13. tests := []struct {
  14. name string
  15. args args
  16. wantDesSubFileFPath string
  17. wantErr bool
  18. }{
  19. {
  20. name: "00",
  21. args: args{
  22. subFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
  23. "Seven.Worlds.One.Planet.S01E01.Antarctica.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,subhd).ass"),
  24. },
  25. wantDesSubFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
  26. "Seven.Worlds.One.Planet.S01E01.Antarctica.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,subhd-utf-8).ass"),
  27. wantErr: false,
  28. },
  29. {
  30. name: "01",
  31. args: args{
  32. subFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
  33. "Seven.Worlds.One.Planet.S01E07.Africa.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,zimuku).default.srt"),
  34. },
  35. wantDesSubFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
  36. "Seven.Worlds.One.Planet.S01E07.Africa.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,zimuku-utf-8).default.srt"),
  37. wantErr: false,
  38. },
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.name, func(t *testing.T) {
  42. fBytes, err := os.ReadFile(tt.args.subFileFPath)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. got, err := ChangeFileCoding2UTF8(fBytes)
  47. if (err != nil) != tt.wantErr {
  48. t.Errorf("ChangeFileCoding2UTF8() error = %v, wantErr %v", err, tt.wantErr)
  49. return
  50. }
  51. wantDesFBytes, err := os.ReadFile(tt.wantDesSubFileFPath)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if !reflect.DeepEqual(got, wantDesFBytes) {
  56. t.Errorf("ChangeFileCoding2UTF8() got = %v, want %v", got, wantDesFBytes)
  57. }
  58. })
  59. }
  60. }
  61. func TestChangeFileCoding2GBK(t *testing.T) {
  62. type args struct {
  63. subFileFPath string
  64. }
  65. tests := []struct {
  66. name string
  67. args args
  68. wantDesSubFileFPath string
  69. wantErr bool
  70. }{
  71. {
  72. name: "00",
  73. args: args{
  74. subFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
  75. "Seven.Worlds.One.Planet.S01E01.Antarctica.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,subhd).ass"),
  76. },
  77. wantDesSubFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
  78. "Seven.Worlds.One.Planet.S01E01.Antarctica.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,subhd-gbk).ass"),
  79. wantErr: false,
  80. },
  81. {
  82. name: "01",
  83. args: args{
  84. subFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
  85. "Seven.Worlds.One.Planet.S01E07.Africa.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,zimuku).default.srt"),
  86. },
  87. wantDesSubFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
  88. "Seven.Worlds.One.Planet.S01E07.Africa.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,zimuku-gbk).default.srt"),
  89. wantErr: false,
  90. },
  91. }
  92. for _, tt := range tests {
  93. t.Run(tt.name, func(t *testing.T) {
  94. fBytes, err := os.ReadFile(tt.args.subFileFPath)
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. got, err := ChangeFileCoding2GBK(fBytes)
  99. if (err != nil) != tt.wantErr {
  100. t.Errorf("ChangeFileCoding2GBK() error = %v, wantErr %v", err, tt.wantErr)
  101. return
  102. }
  103. wantDesFBytes, err := os.ReadFile(tt.wantDesSubFileFPath)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if !reflect.DeepEqual(got, wantDesFBytes) {
  108. t.Errorf("ChangeFileCoding2GBK() got = %v, want %v", got, wantDesFBytes)
  109. }
  110. })
  111. }
  112. }