github.com/dennys-bd/goals@v0.0.0-20210328114421-251a004d41e3/test/project_test.go (about) 1 package test 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/dennys-bd/goals/cmd" 8 ) 9 10 type ProjectTest struct { 11 folder string 12 projectName string 13 result cmd.Project 14 } 15 16 var projectTest = []ProjectTest{ 17 {".", "letest/goals", cmd.Project{ 18 AbsPath: os.Getenv("GOPATH") + "/src/github.com/dennys-bd/goals/test/letest/goals", 19 ImportPath: "github.com/dennys-bd/goals/test/letest/goals", 20 Name: "goals"}, 21 }, 22 {os.Getenv("GOPATH") + "/src", "legoals", cmd.Project{ 23 AbsPath: os.Getenv("GOPATH") + "/src/legoals", 24 ImportPath: "legoals", 25 Name: "legoals"}, 26 }, 27 {os.Getenv("HOME") + "/Desktop", "github.com/goals/outhergoals", cmd.Project{ 28 AbsPath: os.Getenv("GOPATH") + "/src/github.com/goals/outhergoals", 29 ImportPath: "github.com/goals/outhergoals", 30 Name: "outhergoals"}, 31 }, 32 {os.Getenv("HOME"), "onemoregoals", cmd.Project{ 33 AbsPath: os.Getenv("GOPATH") + "/src/onemoregoals", 34 ImportPath: "onemoregoals", 35 Name: "onemoregoals"}, 36 }, 37 } 38 39 func TestNewProject(t *testing.T) { 40 for _, test := range projectTest { 41 err := os.Chdir(test.folder) 42 if err != nil { 43 t.Error(err) 44 } 45 project := cmd.NewProject(test.projectName) 46 if project.Name != test.result.Name { 47 t.Errorf(`NewProject(%s) in %s, Name wanted "%s", got "%s"`, 48 test.projectName, test.folder, test.result.Name, project.Name) 49 } 50 if project.AbsPath != test.result.AbsPath { 51 t.Errorf(`NewProject(%s) in %s, AbsPath wanted "%s", got "%s"`, 52 test.projectName, test.folder, test.result.AbsPath, project.AbsPath) 53 } 54 if project.ImportPath != test.result.ImportPath { 55 t.Errorf(`NewProject(%s) in %s, ImportPath wanted "%s", got "%s"`, 56 test.projectName, test.folder, test.result.ImportPath, project.ImportPath) 57 } 58 } 59 } 60 61 type RecreateProjectTest struct { 62 projectString string 63 result cmd.Project 64 } 65 66 var recreateProjectTest = []RecreateProjectTest{ 67 {`[project] 68 name = "goals" 69 import_path = "github.com/dennys-bd/goals" 70 go_version = "go1.8" 71 app_mode = "gateway"`, 72 cmd.Project{Name: "goals", ImportPath: "github.com/dennys-bd/goals", 73 GoVersion: "go1.8", AppMode: "gateway"}, 74 }, 75 {`[project] 76 name = "othergoals" 77 import_path = "othergoals" 78 go_version = "go1.10" 79 app_mode = "webapp"`, 80 cmd.Project{Name: "othergoals", ImportPath: "othergoals", 81 GoVersion: "go1.10", AppMode: "webapp"}, 82 }, 83 } 84 85 func TestRecriateProject(t *testing.T) { 86 for _, test := range recreateProjectTest { 87 project, err := cmd.RecreateProject(test.projectString) 88 if err != nil { 89 t.Error(err) 90 } 91 if project.Name != test.result.Name { 92 t.Errorf(`RecreateProject(%s), Name wanted "%s", got "%s"`, 93 test.projectString, test.result.Name, project.Name) 94 } 95 if project.GoVersion != test.result.GoVersion { 96 t.Errorf(`RecreateProject(%s), GoVersion wanted "%s", got "%s"`, 97 test.projectString, test.result.GoVersion, project.GoVersion) 98 } 99 if project.ImportPath != test.result.ImportPath { 100 t.Errorf(`RecreateProject(%s), ImportPath wanted "%s", got "%s"`, 101 test.projectString, test.result.ImportPath, project.ImportPath) 102 } 103 if project.AppMode != test.result.AppMode { 104 t.Errorf(`RecreateProject(%s), AppMode wanted "%s", got "%s"`, 105 test.projectString, test.result.AppMode, project.AppMode) 106 } 107 } 108 } 109 110 func TestCreateGoalsToml(t *testing.T) { 111 for _, test := range recreateProjectTest { 112 str := test.result.CreateGoalsToml() 113 if str != test.projectString { 114 t.Errorf(`CreateGoalsToml() of %s, 115 String wanted: 116 %s 117 got: 118 %s`, 119 test.result.Name, test.projectString, str) 120 } 121 } 122 }