github.com/exercism/configlet@v3.9.3-0.20200318193232-c70be6269e71+incompatible/track/problem_specification_test.go (about) 1 package track 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestNewProblemSpecification(t *testing.T) { 11 tests := []struct { 12 desc string 13 slug string 14 trackPath string 15 specPath string 16 expected ProblemSpecification 17 }{ 18 { 19 desc: "shared spec if custom is missing", 20 slug: "one", 21 trackPath: filepath.FromSlash("../fixtures/numbers"), 22 expected: ProblemSpecification{ 23 Description: "This is one.\n", 24 Source: "The internet.", 25 SourceURL: "http://example.com", 26 }, 27 }, 28 { 29 desc: "custom spec overrides shared", 30 slug: "two", 31 trackPath: filepath.FromSlash("../fixtures/numbers"), 32 expected: ProblemSpecification{ 33 Description: "This is two, customized.\n", 34 Source: "The web.", 35 SourceURL: "", 36 }, 37 }, 38 { 39 desc: "shared spec from alternate problem-specifications location", 40 slug: "one", 41 trackPath: filepath.FromSlash("../fixtures/numbers"), 42 specPath: filepath.FromSlash("../fixtures/alternate/problem-specifications"), 43 expected: ProblemSpecification{ 44 Description: "This is the alternate one.\n", 45 Source: "The internet.", 46 SourceURL: "http://example.com", 47 }, 48 }, 49 { 50 desc: "custom spec metadata with shared description", 51 slug: "metadata-example", 52 trackPath: filepath.FromSlash("../fixtures/granular-metadata-overrides"), 53 specPath: filepath.FromSlash("../fixtures/granular-metadata-overrides/problem-specifications"), 54 expected: ProblemSpecification{ 55 Description: "This is a shared description.\n", 56 Source: "The web.", 57 SourceURL: "", 58 }, 59 }, 60 { 61 desc: "shared spec metadata with custom description", 62 slug: "description-example", 63 trackPath: filepath.FromSlash("../fixtures/granular-metadata-overrides"), 64 specPath: filepath.FromSlash("../fixtures/granular-metadata-overrides/problem-specifications"), 65 expected: ProblemSpecification{ 66 Description: "This is a custom description.\n", 67 Source: "The internet.", 68 SourceURL: "http://example.com", 69 }, 70 }, 71 } 72 originalSpecPath := ProblemSpecificationsPath 73 defer func() { ProblemSpecificationsPath = originalSpecPath }() 74 75 for _, test := range tests { 76 ProblemSpecificationsPath = test.specPath 77 root, trackID := filepath.Dir(test.trackPath), filepath.Base(test.trackPath) 78 spec, err := NewProblemSpecification(root, trackID, test.slug) 79 assert.NoError(t, err) 80 81 assert.Equal(t, test.expected.Source, spec.Source) 82 assert.Equal(t, test.expected.SourceURL, spec.SourceURL) 83 assert.Equal(t, test.expected.Description, spec.Description) 84 } 85 } 86 87 func TestMissingProblemSpecification(t *testing.T) { 88 root := filepath.FromSlash("../fixtures") 89 _, err := NewProblemSpecification(root, "numbers", "three") 90 assert.Error(t, err) 91 } 92 93 func TestProblemSpecificationName(t *testing.T) { 94 tests := []struct { 95 desc string 96 slug string 97 title string 98 expected string 99 }{ 100 { 101 desc: "simple slug as name", 102 slug: "apple", 103 expected: "Apple", 104 }, 105 { 106 desc: "multi-word slug as name", 107 slug: "1-apple-per-day", 108 expected: "1 Apple Per Day", 109 }, 110 { 111 desc: "title overrides slug as name", 112 slug: "rna-transcription", 113 title: "RNA Transcription", 114 expected: "RNA Transcription", 115 }, 116 } 117 118 for _, test := range tests { 119 spec := ProblemSpecification{Slug: test.slug, Title: test.title} 120 assert.Equal(t, test.expected, spec.Name(), test.desc) 121 } 122 } 123 124 func TestProblemSpecificationMixedCaseName(t *testing.T) { 125 tests := []struct { 126 slug string 127 expected string 128 }{ 129 { 130 slug: "apple", 131 expected: "Apple", 132 }, 133 { 134 slug: "1-apple-per-day", 135 expected: "1ApplePerDay", 136 }, 137 { 138 slug: "rna-transcription", 139 expected: "RnaTranscription", 140 }, 141 } 142 143 for _, test := range tests { 144 spec := ProblemSpecification{Slug: test.slug} 145 assert.Equal(t, test.expected, spec.MixedCaseName()) 146 } 147 } 148 149 func TestProblemSpecificationSnakeCaseName(t *testing.T) { 150 tests := []struct { 151 slug string 152 expected string 153 }{ 154 { 155 slug: "apple", 156 expected: "apple", 157 }, 158 { 159 slug: "1-apple-per-day", 160 expected: "1_apple_per_day", 161 }, 162 { 163 slug: "rna-transcription", 164 expected: "rna_transcription", 165 }, 166 } 167 168 for _, test := range tests { 169 spec := ProblemSpecification{Slug: test.slug} 170 assert.Equal(t, test.expected, spec.SnakeCaseName()) 171 } 172 } 173 174 func TestProblemSpecificationTitle(t *testing.T) { 175 root := filepath.FromSlash("../fixtures") 176 originalSpecPath := ProblemSpecificationsPath 177 ProblemSpecificationsPath = filepath.Join(root, "titled-problem-specifications") 178 defer func() { ProblemSpecificationsPath = originalSpecPath }() 179 180 tests := []struct { 181 desc string 182 slug string 183 expected string 184 }{ 185 { 186 desc: "title is inferred from slug if not explicitly provided", 187 slug: "slug-as-title", 188 expected: "Slug As Title", 189 }, 190 { 191 desc: "explicit title takes precedence over slug", 192 slug: "explicit-title", 193 expected: "(Explicit) Title", 194 }, 195 } 196 197 for _, test := range tests { 198 spec, err := NewProblemSpecification(root, "titled-problem-specifications", test.slug) 199 assert.NoError(t, err) 200 assert.Equal(t, test.expected, spec.Title) 201 } 202 } 203 204 func TestProblemSpecificationCredits(t *testing.T) { 205 tests := []struct { 206 spec ProblemSpecification 207 credits string 208 }{ 209 { 210 spec: ProblemSpecification{ 211 Source: "Apple.", 212 SourceURL: "http://apple.com", 213 }, 214 credits: "Apple. [http://apple.com](http://apple.com)", 215 }, 216 { 217 spec: ProblemSpecification{ 218 Source: "banana", 219 SourceURL: "", 220 }, 221 credits: "banana", 222 }, 223 { 224 spec: ProblemSpecification{ 225 Source: "", 226 SourceURL: "http://cherry.com", 227 }, 228 credits: "[http://cherry.com](http://cherry.com)", 229 }, 230 { 231 spec: ProblemSpecification{ 232 Source: "", 233 SourceURL: "", 234 }, 235 credits: "", 236 }, 237 } 238 239 for _, test := range tests { 240 assert.Equal(t, test.credits, test.spec.Credits()) 241 } 242 }