template / files / internal / core / name_pattern_test.go
name_pattern_test.go
37160b89 2026-05-01 Pavel Safronov | 1 | package core |
8fc7c452 2023-03-29 Pavel Safronov | 2 | |
| 3 | import ( | |
| 4 | "testing" | |
| 5 | "time" | |
| 6 | ||
| 7 | "github.com/stretchr/testify/assert" | |
37160b89 2026-05-01 Pavel Safronov | 8 | "slices" |
8fc7c452 2023-03-29 Pavel Safronov | 9 | ) |
| 10 | ||
| 11 | // Mon Jan 2 15:04:05 MST 2006 | |
| 12 | var now = time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) | |
| 13 | ||
| 14 | func TestCreateNewName(t *testing.T) { | |
| 15 | // setup code | |
| 16 | ||
| 17 | extras := make(map[string]string) | |
| 18 | extras["test1"] = "test1" | |
| 19 | ||
| 20 | profile := Profile{ | |
| 21 | IsDefault: true, | |
| 22 | Type: FileType, | |
| 23 | NamePatterns: []ParameterizedText{ | |
| 24 | "{{.Extras.test1}}-{{.Year}}-{{.Month}}-{{.Day}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.txt", | |
| 25 | "{{.Extras.test1}}-{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.txt", | |
| 26 | "{{.Extras.test1}}-{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}-{{.Second}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.txt", | |
| 27 | }, | |
| 28 | } | |
| 29 | newNameNoProject := "test1-2006-01-02.txt" | |
| 30 | newName1 := "test1-2006-01-02-test-project.txt" | |
| 31 | newName2 := "test1-2006-01-02-15-test-project.txt" | |
| 32 | newName3 := "test1-2006-01-02-15-04-05-test-project.txt" | |
| 33 | ||
| 34 | testHappyPath := func(projectName string, expectedName string, existingEntities []string) func(t *testing.T) { | |
| 35 | return func(t *testing.T) { | |
| 36 | prePatternData := createPrePatternData(now, projectName, extras) | |
| 37 | validator := func(newName string) bool { return !slices.Contains(existingEntities, newName) } | |
| 38 | newName, err := createNewNameForPatterns(now, prePatternData, projectName, profile.NamePatterns, extras, validator) | |
2105a1d7 2023-04-02 Pavel Safronov | 39 | assert.NoError(t, err) |
| 40 | assert.Equal(t, expectedName, newName, "generated name") | |
| 41 | assert.Equal(t, projectName, prePatternData.ProjectName, "project name") | |
| 42 | assert.Equal(t, "2006", prePatternData.Year, "year") | |
| 43 | assert.Equal(t, "01", prePatternData.Month, "month") | |
| 44 | assert.Equal(t, "02", prePatternData.Day, "day") | |
| 45 | assert.Equal(t, "15", prePatternData.Hour, "hour") | |
| 46 | assert.Equal(t, "04", prePatternData.Minute, "minute") | |
| 47 | assert.Equal(t, "05", prePatternData.Second, "second") | |
| 48 | assert.Equal(t, "2006-01-02", prePatternData.YYYY_MM_DD, "YYYY_MM_DD") | |
| 49 | assert.Equal(t, "2006-01-02-15-04-05", prePatternData.YYYY_MM_DD_HH_MM_SS, "YYYY_MM_DD_HH_MM_SS") | |
| 50 | assert.Equal(t, "test1", prePatternData.Extras["test1"], "test1") | |
8fc7c452 2023-03-29 Pavel Safronov | 51 | } |
| 52 | } | |
| 53 | testErrorPath := func(projectName string, existingEntities []string) func(t *testing.T) { | |
| 54 | return func(t *testing.T) { | |
| 55 | prePatternData := createPrePatternData(now, projectName, extras) | |
| 56 | validator := func(newName string) bool { return !slices.Contains(existingEntities, newName) } | |
| 57 | _, err := createNewNameForPatterns(now, prePatternData, projectName, profile.NamePatterns, extras, validator) | |
| 58 | if err == nil { | |
| 59 | t.Error("Expected error but got none") | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 | // test code | |
| 65 | ||
| 66 | // happy path | |
| 67 | t.Run("createNewName - happy path", testHappyPath("test-project", newName1, []string{})) | |
| 68 | t.Run("createNewName - use second pattern", testHappyPath("test-project", newName2, []string{newName1})) | |
| 69 | t.Run("createNewName - use third pattern", testHappyPath("test-project", newName3, []string{newName1, newName2})) | |
| 70 | t.Run("createNewName - happy path", testHappyPath("", newNameNoProject, []string{})) | |
| 71 | ||
| 72 | // error path | |
| 73 | t.Run("createNewName - error path - collision", testErrorPath("test-project", []string{newName1, newName2, newName3})) | |
| 74 | } | |
| 75 | ||
| 76 | func TestCreateNewName_Index(t *testing.T) { | |
| 77 | extras := make(map[string]string) | |
| 78 | extras["test1"] = "test1" | |
| 79 | ||
| 80 | profile := Profile{ | |
| 81 | IsDefault: true, | |
| 82 | Type: FileType, | |
| 83 | NamePatterns: []ParameterizedText{ | |
| 84 | "{{.Extras.test1}}-{{$version := getVersion}}{{.Year}}-{{.Month}}-{{.Day}}v{{$version}}.txt", | |
| 85 | }, | |
| 86 | } | |
| 87 | newName1 := "test1-2006-01-02v1.txt" | |
| 88 | newName2 := "test1-2006-01-02v2.txt" | |
| 89 | newName3 := "test1-2006-01-02v3.txt" | |
| 90 | ||
| 91 | testHappyPath := func(expectedName string, existingEntities []string) func(t *testing.T) { | |
| 92 | return func(t *testing.T) { | |
| 93 | prePatternData := createPrePatternData(now, "test-project", extras) | |
| 94 | validator := func(newName string) bool { return !slices.Contains(existingEntities, newName) } | |
| 95 | newName, err := createNewNameForPatterns(now, prePatternData, "test-project", profile.NamePatterns, extras, validator) | |
2105a1d7 2023-04-02 Pavel Safronov | 96 | assert.NoError(t, err) |
| 97 | assert.Equal(t, expectedName, newName, "generated name") | |
| 98 | assert.Equal(t, "test-project", prePatternData.ProjectName, "project name") | |
| 99 | assert.Equal(t, "2006", prePatternData.Year, "year") | |
| 100 | assert.Equal(t, "01", prePatternData.Month, "month") | |
| 101 | assert.Equal(t, "02", prePatternData.Day, "day") | |
| 102 | assert.Equal(t, "15", prePatternData.Hour, "hour") | |
| 103 | assert.Equal(t, "04", prePatternData.Minute, "minute") | |
| 104 | assert.Equal(t, "05", prePatternData.Second, "second") | |
| 105 | assert.Equal(t, "2006-01-02", prePatternData.YYYY_MM_DD, "YYYY_MM_DD") | |
| 106 | assert.Equal(t, "2006-01-02-15-04-05", prePatternData.YYYY_MM_DD_HH_MM_SS, "YYYY_MM_DD_HH_MM_SS") | |
8fc7c452 2023-03-29 Pavel Safronov | 107 | } |
| 108 | } | |
| 109 | ||
| 110 | // test code | |
| 111 | ||
| 112 | // happy path | |
| 113 | t.Run("createNewName - v1", testHappyPath(newName1, []string{})) | |
| 114 | t.Run("createNewName - v2", testHappyPath(newName2, []string{newName1})) | |
| 115 | t.Run("createNewName - v3", testHappyPath(newName3, []string{newName1, newName2})) | |
| 116 | t.Run("createNewName - v2", testHappyPath(newName2, []string{newName1, newName3})) | |
| 117 | } | |
| 118 | ||
| 119 | func TestCreateNewName_sprig(t *testing.T) { | |
| 120 | profile := Profile{ | |
| 121 | IsDefault: true, | |
| 122 | Type: FileType, | |
| 123 | NamePatterns: []ParameterizedText{`{{"HELLO" | lower}}`}, | |
| 124 | } | |
| 125 | projectName := "test-project" | |
| 126 | prePatternData := createPrePatternData(now, projectName, nil) | |
| 127 | newName, err := createNewNameForPatterns( | |
| 128 | now, | |
| 129 | prePatternData, | |
| 130 | projectName, | |
| 131 | profile.NamePatterns, | |
| 132 | nil, | |
| 133 | func(newName string) bool { | |
| 134 | return true | |
| 135 | }) | |
| 136 | assert.Nil(t, err) | |
| 137 | assert.Equal(t, "hello", newName) | |
| 138 | } |