Skip to content

Commit

Permalink
Using I18n to know supported languages. Adding test for path paramete…
Browse files Browse the repository at this point in the history
…rs sent after configuration parameters
  • Loading branch information
beluamat29 committed Dec 29, 2024
1 parent babd225 commit 5ffdac2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 34 deletions.
52 changes: 36 additions & 16 deletions lib/config/parameters_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -15,48 +15,55 @@ 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);
}

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),
Expand All @@ -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) {
Expand All @@ -88,7 +108,7 @@ class FailFastConfigurationParameterParser {
class RandomizeConfigurationParameterParser {

static canHandle(consoleParam) {
return isRandomizeParam(consoleParam);
return ParametersParser.isRandomizeParam(consoleParam);
}

static handle(_consoleParam) {
Expand All @@ -99,7 +119,7 @@ class RandomizeConfigurationParameterParser {
class LanguageConfigurationParameterParser {

static canHandle(consoleParam) {
return isLanguageParam(consoleParam);
return ParametersParser.isLanguageParam(consoleParam);
}

static handle(consoleParam) {
Expand Down
6 changes: 5 additions & 1 deletion lib/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
14 changes: 0 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -183,8 +173,4 @@ export {
errorDetailOf,
// metaprogramming
detectUserCallingLocation,
// parsing
isFailFastParam,
isRandomizeParam,
isLanguageParam,
};
12 changes: 10 additions & 2 deletions tests/configuration/parameters_parser_test.js
Original file line number Diff line number Diff line change
@@ -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', () => {

Expand Down Expand Up @@ -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']);
Expand All @@ -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(', ')}`));
});
});
2 changes: 1 addition & 1 deletion tests/core/configuration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
Expand Down

0 comments on commit 5ffdac2

Please sign in to comment.