|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func errNoProjectName(sectionTitle string) error { |
| 10 | + return fmt.Errorf("%s: is missing project name", sectionTitle) |
| 11 | +} |
| 12 | + |
| 13 | +func errIncomplete(sectionTitle string) error { |
| 14 | + return fmt.Errorf("%s: was not completed for application", sectionTitle) |
| 15 | +} |
| 16 | + |
| 17 | +func errEmpty(sectionTitle string) error { |
| 18 | + return fmt.Errorf("%s: is empty", sectionTitle) |
| 19 | +} |
| 20 | + |
| 21 | +func errMustBeChecked(sectionTitle string) error { |
| 22 | + return fmt.Errorf("%s: must be checked", sectionTitle) |
| 23 | +} |
| 24 | + |
| 25 | +func errInvalidAccountUrl(sectionTitle string) error { |
| 26 | + return fmt.Errorf("%s: is invalid 1Password account URL", sectionTitle) |
| 27 | +} |
| 28 | + |
| 29 | +func errContainsEmoji(sectionTitle string) error { |
| 30 | + return fmt.Errorf("%s: cannot contain emoji characters", sectionTitle) |
| 31 | +} |
| 32 | + |
| 33 | +func errParsingNumber(sectionTitle string) error { |
| 34 | + return fmt.Errorf("%s: could not be parsed into a number", sectionTitle) |
| 35 | +} |
| 36 | + |
| 37 | +func errInvalidUrl(sectionTitle string) error { |
| 38 | + return fmt.Errorf("%s: is an invalid URL", sectionTitle) |
| 39 | +} |
| 40 | + |
| 41 | +func TestApplication(t *testing.T) { |
| 42 | + originalDir, err := os.Getwd() |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("Failed to get current directory: %s", err) |
| 45 | + } |
| 46 | + |
| 47 | + defer func() { |
| 48 | + if err := os.Chdir(originalDir); err != nil { |
| 49 | + t.Fatalf("Failed to change back to original directory: %s", err) |
| 50 | + } |
| 51 | + }() |
| 52 | + |
| 53 | + if err := os.Chdir("../"); err != nil { |
| 54 | + t.Fatalf("Failed to change working directory: %s", err) |
| 55 | + } |
| 56 | + |
| 57 | + testCases := []struct { |
| 58 | + name string |
| 59 | + expectedValid bool |
| 60 | + expectedProblems []error |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "project", |
| 64 | + expectedValid: true, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "team", |
| 68 | + expectedValid: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "event", |
| 72 | + expectedValid: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "empty-body", |
| 76 | + expectedValid: false, |
| 77 | + expectedProblems: []error{ |
| 78 | + errIncomplete("Account URL"), |
| 79 | + errIncomplete("Non-commercial confirmation"), |
| 80 | + errIncomplete("Team application"), |
| 81 | + errIncomplete("Event application"), |
| 82 | + errIncomplete("Project name"), |
| 83 | + errIncomplete("Short description"), |
| 84 | + errIncomplete("Number of team members/core contributors"), |
| 85 | + errIncomplete("Homepage URL"), |
| 86 | + errIncomplete("Repository URL"), |
| 87 | + errIncomplete("License type"), |
| 88 | + errIncomplete("License URL"), |
| 89 | + errIncomplete("Age confirmation"), |
| 90 | + errIncomplete("Name"), |
| 91 | + errIncomplete("Email"), |
| 92 | + errIncomplete("Project role"), |
| 93 | + errIncomplete("Profile or website"), |
| 94 | + errIncomplete("Additional comments"), |
| 95 | + errIncomplete("Can we contact you?"), |
| 96 | + }, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "no-responses", |
| 100 | + expectedValid: false, |
| 101 | + expectedProblems: []error{ |
| 102 | + errNoProjectName("Application title"), |
| 103 | + errEmpty("Account URL"), |
| 104 | + errMustBeChecked("Non-commercial confirmation"), |
| 105 | + errEmpty("Project name"), |
| 106 | + errEmpty("Short description"), |
| 107 | + errEmpty("Number of team members/core contributors"), |
| 108 | + errEmpty("Homepage URL"), |
| 109 | + errEmpty("License type"), |
| 110 | + errEmpty("License URL"), |
| 111 | + errMustBeChecked("Age confirmation"), |
| 112 | + errEmpty("Name"), |
| 113 | + errEmpty("Email"), |
| 114 | + errEmpty("Project role"), |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "examples-1", |
| 119 | + expectedValid: false, |
| 120 | + expectedProblems: []error{ |
| 121 | + errNoProjectName("Application title"), |
| 122 | + errInvalidAccountUrl("Account URL"), |
| 123 | + errMustBeChecked("Non-commercial confirmation"), |
| 124 | + errContainsEmoji("Project name"), |
| 125 | + errParsingNumber("Number of team members/core contributors"), |
| 126 | + errInvalidUrl("Homepage URL"), |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, tt := range testCases { |
| 132 | + t.Run(tt.name, func(t *testing.T) { |
| 133 | + application := Application{} |
| 134 | + |
| 135 | + if tt.expectedValid { |
| 136 | + testIssueName = fmt.Sprintf("valid-%s", tt.name) |
| 137 | + } else { |
| 138 | + testIssueName = fmt.Sprintf("invalid-%s", tt.name) |
| 139 | + } |
| 140 | + |
| 141 | + application.Parse(*getTestIssue()) |
| 142 | + |
| 143 | + if application.IsValid() != tt.expectedValid { |
| 144 | + if tt.expectedValid { |
| 145 | + t.Errorf("Test issue '%s' is invalid, expected valid", testIssueName) |
| 146 | + } else { |
| 147 | + t.Errorf("Test issue '%s' is valid, expected invalid", testIssueName) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if !tt.expectedValid && !errSliceEqual(application.Problems, tt.expectedProblems) { |
| 152 | + t.Errorf("Expected problems %v, got %v", tt.expectedProblems, application.Problems) |
| 153 | + } |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
0 commit comments