diff --git a/challengeTitles.js b/challengeTitles.js new file mode 100644 index 0000000000..c7edd45481 --- /dev/null +++ b/challengeTitles.js @@ -0,0 +1,26 @@ +import _ from 'lodash'; + +class ChallengeTitles { + constructor() { + this.knownTitles = []; + } + check(title) { + if (typeof title !== 'string') { + throw new Error(`Expected a valid string for ${title}, got ${typeof title}`); + } else if (title.length === 0) { + throw new Error(`Expected a title length greater than 0`); + } + const titleToCheck = title.toLowerCase().replace(/\s+/g, ''); + const titleIndex = _.findIndex(this.knownTitles, existing => titleToCheck === existing); + if (titleIndex !== -1) { + throw new Error(` + All challenges must have a unique title. + + The title ${title} is already assigned + `); + } + this.knownTitles = [ ...this.knownTitles, titleToCheck ]; + } +} + +export default ChallengeTitles; diff --git a/test-challenges.js b/test-challenges.js index a7cdf6b11b..61c5f3e2e2 100644 --- a/test-challenges.js +++ b/test-challenges.js @@ -6,9 +6,11 @@ import tape from 'tape'; import getChallenges from './getChallenges'; import { modern } from '../common/app/utils/challengeTypes'; import MongoIds from './mongoIds'; +import ChallengeTitles from './challengeTitles'; import addAssertsToTapTest from './addAssertsToTapTest'; let mongoIds = new MongoIds(); +let challengeTitles = new ChallengeTitles(); function evaluateTest(solution, assert, react, redux, reactRedux, @@ -115,6 +117,7 @@ function createTest({ reactRedux = false }) { mongoIds.check(id, title); + challengeTitles.check(title); solutions = solutions.filter(solution => !!solution); tests = tests.filter(test => !!test);