template / windows / files / name_pattern_test.go
Branch windows · default branch · all branches
name_pattern_test.go
da15cf49 2022-09-23 Pavel Safronov | 1 | package main |
| 2 | ||
| 3 | import ( | |
| 4 | "testing" | |
| 5 | "time" | |
10676da3 2022-11-06 Pavel Safronov | 6 | |
| 7 | "golang.org/x/exp/slices" | |
da15cf49 2022-09-23 Pavel Safronov | 8 | ) |
| 9 | ||
| 10 | func TestCreateNewName(t *testing.T) { | |
| 11 | // setup code | |
| 12 | ||
55c36f6a 2022-10-05 Pavel Safronov | 13 | extras := make(map[string]string) |
| 14 | extras["test1"] = "test1" | |
| 15 | ||
da15cf49 2022-09-23 Pavel Safronov | 16 | // Mon Jan 2 15:04:05 MST 2006 |
| 17 | now := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) | |
| 18 | profile := Profile{ | |
| 19 | IsDefault: true, | |
| 20 | Type: FileType, | |
1a02ef4e 2022-10-21 Pavel Safronov | 21 | NamePatterns: []ParameterizedText{ |
55c36f6a 2022-10-05 Pavel Safronov | 22 | "{{.Extras.test1}}-{{.Year}}-{{.Month}}-{{.Day}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.txt", |
| 23 | "{{.Extras.test1}}-{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}{{if .ProjectName}}-{{.ProjectName}}{{end}}.txt", | |
| 24 | "{{.Extras.test1}}-{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}-{{.Second}}-{{.ProjectName}}.txt", | |
da15cf49 2022-09-23 Pavel Safronov | 25 | }, |
| 26 | } | |
55c36f6a 2022-10-05 Pavel Safronov | 27 | newNameNoProject := "test1-2006-01-02.txt" |
| 28 | newName1 := "test1-2006-01-02-test-project.txt" | |
| 29 | newName2 := "test1-2006-01-02-15-test-project.txt" | |
| 30 | newName3 := "test1-2006-01-02-15-04-05-test-project.txt" | |
da15cf49 2022-09-23 Pavel Safronov | 31 | |
| 32 | testHappyPath := func(projectName string, expectedName string, existingEntities []string) func(t *testing.T) { | |
| 33 | return func(t *testing.T) { | |
8c15a714 2022-11-04 Pavel Safronov | 34 | prePatternData := createPrePatternData(now, projectName, extras) |
10676da3 2022-11-06 Pavel Safronov | 35 | validator := func(newName string) bool { return !slices.Contains(existingEntities, newName) } |
| 36 | newName, err := createNewNameForPatterns(now, prePatternData, projectName, profile.NamePatterns, extras, validator) | |
da15cf49 2022-09-23 Pavel Safronov | 37 | if err != nil { |
| 38 | t.Error("Error creating new name: ", err) | |
| 39 | } | |
| 40 | if newName != expectedName { | |
| 41 | t.Error("Expected name to be ", expectedName, " but got ", newName) | |
| 42 | } | |
687d5626 2022-10-28 Pavel Safronov | 43 | if prePatternData.ProjectName != projectName { |
| 44 | t.Error("Expected prePatternData.ProjectName to be ", projectName, " but got ", prePatternData.ProjectName) | |
| 45 | } | |
| 46 | if prePatternData.Year != "2006" { | |
| 47 | t.Error("Expected prePatternData.Year to be 2006 but got ", prePatternData.Year) | |
| 48 | } | |
| 49 | if prePatternData.Month != "01" { | |
| 50 | t.Error("Expected prePatternData.Month to be 01 but got ", prePatternData.Month) | |
| 51 | } | |
| 52 | if prePatternData.Day != "02" { | |
| 53 | t.Error("Expected prePatternData.Day to be 02 but got ", prePatternData.Day) | |
| 54 | } | |
| 55 | if prePatternData.Hour != "15" { | |
| 56 | t.Error("Expected prePatternData.Hour to be 15 but got ", prePatternData.Hour) | |
| 57 | } | |
| 58 | if prePatternData.Minute != "04" { | |
| 59 | t.Error("Expected prePatternData.Minute to be 04 but got ", prePatternData.Minute) | |
| 60 | } | |
| 61 | if prePatternData.Second != "05" { | |
| 62 | t.Error("Expected prePatternData.Second to be 05 but got ", prePatternData.Second) | |
| 63 | } | |
| 64 | ||
da15cf49 2022-09-23 Pavel Safronov | 65 | } |
| 66 | } | |
| 67 | testErrorPath := func(projectName string, existingEntities []string) func(t *testing.T) { | |
| 68 | return func(t *testing.T) { | |
8c15a714 2022-11-04 Pavel Safronov | 69 | prePatternData := createPrePatternData(now, projectName, extras) |
10676da3 2022-11-06 Pavel Safronov | 70 | validator := func(newName string) bool { return !slices.Contains(existingEntities, newName) } |
| 71 | _, err := createNewNameForPatterns(now, prePatternData, projectName, profile.NamePatterns, extras, validator) | |
da15cf49 2022-09-23 Pavel Safronov | 72 | if err == nil { |
| 73 | t.Error("Expected error but got none") | |
| 74 | } | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | // test code | |
| 79 | ||
| 80 | // happy path | |
| 81 | t.Run("createNewName - happy path", testHappyPath("test-project", newName1, []string{})) | |
| 82 | t.Run("createNewName - use second pattern", testHappyPath("test-project", newName2, []string{newName1})) | |
| 83 | t.Run("createNewName - use third pattern", testHappyPath("test-project", newName3, []string{newName1, newName2})) | |
0e962bb6 2022-09-23 Pavel Safronov | 84 | t.Run("createNewName - happy path", testHappyPath("", newNameNoProject, []string{})) |
da15cf49 2022-09-23 Pavel Safronov | 85 | |
| 86 | // error path | |
| 87 | t.Run("createNewName - error path - collision", testErrorPath("test-project", []string{newName1, newName2, newName3})) | |
0e962bb6 2022-09-23 Pavel Safronov | 88 | } |
| 89 | ||
| 90 | func TestCreateNewName_Index(t *testing.T) { | |
55c36f6a 2022-10-05 Pavel Safronov | 91 | extras := make(map[string]string) |
| 92 | extras["test1"] = "test1" | |
| 93 | ||
0e962bb6 2022-09-23 Pavel Safronov | 94 | // Mon Jan 2 15:04:05 MST 2006 |
| 95 | now := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) | |
| 96 | profile := Profile{ | |
| 97 | IsDefault: true, | |
| 98 | Type: FileType, | |
1a02ef4e 2022-10-21 Pavel Safronov | 99 | NamePatterns: []ParameterizedText{ |
55c36f6a 2022-10-05 Pavel Safronov | 100 | "{{.Extras.test1}}-{{$version := getVersion}}{{.Year}}-{{.Month}}-{{.Day}}v{{$version}}.txt", |
0e962bb6 2022-09-23 Pavel Safronov | 101 | }, |
| 102 | } | |
55c36f6a 2022-10-05 Pavel Safronov | 103 | newName1 := "test1-2006-01-02v1.txt" |
| 104 | newName2 := "test1-2006-01-02v2.txt" | |
| 105 | newName3 := "test1-2006-01-02v3.txt" | |
da15cf49 2022-09-23 Pavel Safronov | 106 | |
0e962bb6 2022-09-23 Pavel Safronov | 107 | testHappyPath := func(expectedName string, existingEntities []string) func(t *testing.T) { |
| 108 | return func(t *testing.T) { | |
8c15a714 2022-11-04 Pavel Safronov | 109 | prePatternData := createPrePatternData(now, "test-project", extras) |
10676da3 2022-11-06 Pavel Safronov | 110 | validator := func(newName string) bool { return !slices.Contains(existingEntities, newName) } |
| 111 | newName, err := createNewNameForPatterns(now, prePatternData, "test-project", profile.NamePatterns, extras, validator) | |
0e962bb6 2022-09-23 Pavel Safronov | 112 | if err != nil { |
| 113 | t.Error("Error creating new name: ", err) | |
| 114 | } | |
| 115 | if newName != expectedName { | |
| 116 | t.Error("Expected name to be ", expectedName, " but got ", newName) | |
| 117 | } | |
687d5626 2022-10-28 Pavel Safronov | 118 | if prePatternData.ProjectName != "test-project" { |
| 119 | t.Error("Expected prePatternData.ProjectName to be test-project but got ", prePatternData.ProjectName) | |
| 120 | } | |
| 121 | if prePatternData.Year != "2006" { | |
| 122 | t.Error("Expected prePatternData.Year to be 2006 but got ", prePatternData.Year) | |
| 123 | } | |
| 124 | if prePatternData.Month != "01" { | |
| 125 | t.Error("Expected prePatternData.Month to be 01 but got ", prePatternData.Month) | |
| 126 | } | |
| 127 | if prePatternData.Day != "02" { | |
| 128 | t.Error("Expected prePatternData.Day to be 02 but got ", prePatternData.Day) | |
| 129 | } | |
| 130 | if prePatternData.Hour != "15" { | |
| 131 | t.Error("Expected prePatternData.Hour to be 15 but got ", prePatternData.Hour) | |
| 132 | } | |
| 133 | if prePatternData.Minute != "04" { | |
| 134 | t.Error("Expected prePatternData.Minute to be 04 but got ", prePatternData.Minute) | |
| 135 | } | |
| 136 | if prePatternData.Second != "05" { | |
| 137 | t.Error("Expected prePatternData.Second to be 05 but got ", prePatternData.Second) | |
| 138 | } | |
0e962bb6 2022-09-23 Pavel Safronov | 139 | } |
| 140 | } | |
| 141 | ||
| 142 | // test code | |
| 143 | ||
| 144 | // happy path | |
| 145 | t.Run("createNewName - v1", testHappyPath(newName1, []string{})) | |
| 146 | t.Run("createNewName - v2", testHappyPath(newName2, []string{newName1})) | |
| 147 | t.Run("createNewName - v3", testHappyPath(newName3, []string{newName1, newName2})) | |
| 148 | t.Run("createNewName - v2", testHappyPath(newName2, []string{newName1, newName3})) | |
da15cf49 2022-09-23 Pavel Safronov | 149 | } |