diff --git a/lib/config/parameters_parser.js b/lib/config/parameters_parser.js index 94cba3d..15b7963 100644 --- a/lib/config/parameters_parser.js +++ b/lib/config/parameters_parser.js @@ -2,9 +2,9 @@ * I transform a list of console parameters into a valid configuration object. * I can also differentiate between path parameters and configuration parameters. */ -import { isFailFastParam, isLanguageParam, isRandomizeParam } from '../utils.js'; +import { I18n } from '../i18n/i18n.js'; +import { isString } from '../utils.js'; -const LANGUAGE_OPTIONS = ['es', 'en', 'it']; export class ParametersParser { static generateRunConfigurationFromParams(params) { @@ -15,14 +15,14 @@ export class ParametersParser { static generateRunConfigurationFromSanitizedParams(sanitizedParams) { let generatedParams = {}; sanitizedParams.forEach(param => { - const runParameter = this.#generateRunConfigurationParameter(param); + const runParameter = this.generateRunConfigurationParameter(param); generatedParams = { ...generatedParams, ...runParameter }; }); return generatedParams; } - static #generateRunConfigurationParameter(param) { + static generateRunConfigurationParameter(param) { const paramParser = [FailFastConfigurationParameterParser, RandomizeConfigurationParameterParser, LanguageConfigurationParameterParser, InvalidConfigurationParameter].find(configurationParameterParser => configurationParameterParser.canHandle(param)); return paramParser.handle(param); } @@ -30,33 +30,40 @@ export class ParametersParser { static sanitizeParameters(params) { const languageParamIndex = params.findIndex(param => param === '-l' || param === '--language'); if (languageParamIndex >= 0) { - return this.#sanitizeLanguageParamOptions(params, languageParamIndex); + return this.sanitizeLanguageParamOptions(params, languageParamIndex); } return params; } - static #sanitizeLanguageParamOptions(params, languageParamIndex) { - const languageOption = this.#validateLanguageOption(params, languageParamIndex); + static sanitizeLanguageParamOptions(params, languageParamIndex) { + const languageOption = this.validateLanguageOption(params, languageParamIndex); const languageConfig = [`-l ${languageOption}`]; - this.#removeLanguageParameters(params, languageParamIndex); + this.removeLanguageParameters(params, languageParamIndex); return [...params, ...languageConfig]; } - static #validateLanguageOption(params, languageParamIndex) { + static validateLanguageOption(params, languageParamIndex) { const languageOption = params[languageParamIndex + 1]; - if (!LANGUAGE_OPTIONS.includes(languageOption)) { - throw new Error('Invalid language option. Please choose an option between es for Spanish, en for English or it for Italian'); + const supportedLanguages = I18n.supportedLanguages(); + if (!supportedLanguages.includes(languageOption)) { + I18n.unsupportedLanguageException(languageOption, supportedLanguages); } return languageOption; } - static #removeLanguageParameters(params, languageParamIndex) { + static removeLanguageParameters(params, languageParamIndex) { params.splice(languageParamIndex, 2); } static getPathsAndConfigurationParams(allParams) { const firstConfigParamIndex = allParams.findIndex(param => this.isConfigurationParam(param)); + if (firstConfigParamIndex >= 0) { + const paramsAfterFirstConfigurationParam = allParams.slice(firstConfigParamIndex); + const thereIsPathParamAfterConfigParams = paramsAfterFirstConfigurationParam.some(param => !this.isConfigurationParam(param)); + if (thereIsPathParamAfterConfigParams) { + throw new Error('Run configuration parameters should always be sent at the end of test paths routes'); + } return { pathsParams: allParams.slice(0, firstConfigParamIndex), configurationParams: allParams.slice(firstConfigParamIndex), @@ -70,14 +77,27 @@ export class ParametersParser { } static isConfigurationParam(param) { - return isFailFastParam(param) || isRandomizeParam(param) || isLanguageParam(param); + return this.isFailFastParam(param) || this.isRandomizeParam(param) || this.isLanguageParam(param); + } + + static isFailFastParam(string) { + return isString(string) && (string === '-f' || string === '--fail-fast'); } + + static isRandomizeParam(string) { + return isString(string) && (string === '-r' || string === '--randomize'); + } + + static isLanguageParam(param) { + const options = param.split(' '); + return isString(options[0]) && (options[0] === '-l' || options[0] === '--language'); + }; } class FailFastConfigurationParameterParser { static canHandle(consoleParam) { - return isFailFastParam(consoleParam); + return ParametersParser.isFailFastParam(consoleParam); } static handle(_consoleParam) { @@ -88,7 +108,7 @@ class FailFastConfigurationParameterParser { class RandomizeConfigurationParameterParser { static canHandle(consoleParam) { - return isRandomizeParam(consoleParam); + return ParametersParser.isRandomizeParam(consoleParam); } static handle(_consoleParam) { @@ -99,7 +119,7 @@ class RandomizeConfigurationParameterParser { class LanguageConfigurationParameterParser { static canHandle(consoleParam) { - return isLanguageParam(consoleParam); + return ParametersParser.isLanguageParam(consoleParam); } static handle(consoleParam) { diff --git a/lib/i18n/i18n.js b/lib/i18n/i18n.js index e3825e4..07ec0c4 100644 --- a/lib/i18n/i18n.js +++ b/lib/i18n/i18n.js @@ -37,6 +37,10 @@ class I18n { return `Translation key '${key}' not found in translations for language: ${languageCode}!`; } + static unsupportedLanguageException(languageCode, supportedLanguages) { + throw new Error(`Language '${languageCode}' is not supported. Allowed values: ${supportedLanguages.join(', ')}`); + } + constructor(languageCode, translations = TRANSLATIONS) { this.#assertLanguageIsSupported(languageCode, translations); this.#languageCode = languageCode; @@ -55,7 +59,7 @@ class I18n { #assertLanguageIsSupported(languageCode, translations) { const supportedLanguages = I18n.supportedLanguages(translations); if (!supportedLanguages.includes(languageCode)) { - throw new Error(`Language '${languageCode}' is not supported. Allowed values: ${supportedLanguages.join(', ')}`); + I18n.unsupportedLanguageException(languageCode, supportedLanguages); } } diff --git a/lib/utils.js b/lib/utils.js index b27bb91..bf5125d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -78,16 +78,6 @@ const isFunction = object => const isUndefined = object => typeof object === 'undefined'; -const isFailFastParam = string => - isString(string) && (string === '-f' || string === '--fail-fast'); - -const isRandomizeParam = string => - isString(string) && (string === '-r' || string === '--randomize'); - -const isLanguageParam = param => { - const options = param.split(' '); - return isString(options[0]) && (options[0] === '-l' || options[0] === '--language'); -}; const notNullOrUndefined = object => !isUndefined(object) && object !== null; @@ -183,8 +173,4 @@ export { errorDetailOf, // metaprogramming detectUserCallingLocation, - // parsing - isFailFastParam, - isRandomizeParam, - isLanguageParam, }; diff --git a/tests/configuration/parameters_parser_test.js b/tests/configuration/parameters_parser_test.js index acda758..447d444 100644 --- a/tests/configuration/parameters_parser_test.js +++ b/tests/configuration/parameters_parser_test.js @@ -1,5 +1,6 @@ import { assert, suite, test } from '../../lib/testy.js'; import { ParametersParser } from '../../lib/config/parameters_parser.js'; +import { I18n } from '../../lib/i18n/i18n.js'; suite('Parameters parser', () => { @@ -84,6 +85,13 @@ suite('Parameters parser', () => { assert.areEqual(configurationParams, []); }); + test('throws an error when file path params are sent after configuration parameters', () => { + const testPath1 = 'I am a test path'; + assert + .that(() => ParametersParser.getPathsAndConfigurationParams(['-f', testPath1])) + .raises(new Error('Run configuration parameters should always be sent at the end of test paths routes')); + }); + test('returns sanitized params when passing a valid list of params', () => { const sanitizedParams = ParametersParser.sanitizeParameters(['-f', '-r']); assert.areEqual(sanitizedParams, ['-f', '-r']); @@ -97,12 +105,12 @@ suite('Parameters parser', () => { test('throws an error when sending invalid language option', () => { assert .that(() => ParametersParser.sanitizeParameters(['-l', 'fakeLanguage'])) - .raises(new Error('Invalid language option. Please choose an option between es for Spanish, en for English or it for Italian')); + .raises(new Error(`Language '${'fakeLanguage'}' is not supported. Allowed values: ${I18n.supportedLanguages().join(', ')}`)); }); test('throws an error when language parameters do not have the proper order', () => { assert .that(() => ParametersParser.sanitizeParameters(['it', '-l'])) - .raises(new Error('Invalid language option. Please choose an option between es for Spanish, en for English or it for Italian')); + .raises(new Error(`Language '${undefined}' is not supported. Allowed values: ${I18n.supportedLanguages().join(', ')}`)); }); }); diff --git a/tests/core/configuration_test.js b/tests/core/configuration_test.js index a14b728..8242f9a 100644 --- a/tests/core/configuration_test.js +++ b/tests/core/configuration_test.js @@ -54,7 +54,7 @@ suite('Configuration parameters', () => { }); test('can send a configuration and values not present will default to default configuration', () => { - const userCustomizedConfiguration = Configuration.withConfiguration({language: "es", failFast: true}); + const userCustomizedConfiguration = Configuration.withConfiguration({ language: 'es', failFast: true }); assert.areEqual(userCustomizedConfiguration.language(), 'es'); assert.areEqual(userCustomizedConfiguration.failFastMode(), FailFast.default()); });