1
0
Эх сурвалжийг харах

尝试将测试用例使用到的测试数据独立到独立的项目(ChineseSubFinder-TestData)中,然后通过统一的调用方式去拿到对应的测试数据的路径

Signed-off-by: allan716 <[email protected]>
allan716 3 жил өмнө
parent
commit
21cfa0ecc0

+ 3 - 1
internal/pkg/sub_helper/dialogue_merger_test.go

@@ -3,6 +3,8 @@ package sub_helper
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
+
 	// "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
 	"path/filepath"
@@ -57,7 +59,7 @@ func Test_isFirstLetterIsEngLower(t *testing.T) {
 
 func TestNewDialogueMerger(t *testing.T) {
 
-	testRootDir := filepath.FromSlash("../../../TestData/FixTimeline")
+	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"FixTimeline", "test"}, 4)
 
 	subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
 	//bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(filepath.Join(testRootDir, "2line-The Card Counter (2021) WEBDL-1080p.chinese(inside).ass"))

+ 26 - 0
internal/pkg/unit_test_helper/unit_test_helper.go

@@ -0,0 +1,26 @@
+package unit_test_helper
+
+import (
+	"path/filepath"
+)
+
+// GetTestDataResourceRootPath 向上返回几层就能够到 ChineseSubFinder-TestData 同级目录,然后进入其中的 resourceFolderName 资源文件夹中
+func GetTestDataResourceRootPath(resourceFolderNames []string, goBackTimes int) string {
+
+	times := ""
+	for i := 0; i < goBackTimes; i++ {
+		times += oneBackTime
+	}
+	outPath := times + testResourceProjectName
+
+	for _, name := range resourceFolderNames {
+		outPath += "/" + name
+	}
+
+	outPath = filepath.FromSlash(outPath)
+
+	return outPath
+}
+
+const oneBackTime = "../"
+const testResourceProjectName = "ChineseSubFinder-TestData"

+ 40 - 0
internal/pkg/unit_test_helper/unit_test_helper_test.go

@@ -0,0 +1,40 @@
+package unit_test_helper
+
+import (
+	"path/filepath"
+	"testing"
+)
+
+func TestGetTestDataResourceRootPath(t *testing.T) {
+	type args struct {
+		resourceFolderNames []string
+		goBackTimes         int
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{
+			name: "loghelper", args: args{
+				resourceFolderNames: []string{"log_helper"},
+				goBackTimes:         1,
+			},
+			want: filepath.FromSlash("../ChineseSubFinder-TestData/log_helper"),
+		},
+		{
+			name: "language", args: args{
+				resourceFolderNames: []string{"language", "test"},
+				goBackTimes:         1,
+			},
+			want: filepath.FromSlash("../ChineseSubFinder-TestData/language/test"),
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := GetTestDataResourceRootPath(tt.args.resourceFolderNames, tt.args.goBackTimes); got != tt.want {
+				t.Errorf("GetTestDataResourceRootPath() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}