From c2108fa5db889bb1455a5735934776bcf91fabac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leyla=20J=C3=A4hnig?= Date: Mon, 29 Aug 2022 13:02:02 +0200 Subject: [PATCH] fix(random): retry on invalid word generation (#1307) --- src/modules/random/index.ts | 66 ++++++++++++++++++++------ test/__snapshots__/random.spec.ts.snap | 18 +++---- test/__snapshots__/system.spec.ts.snap | 48 +++++++++---------- 3 files changed, 84 insertions(+), 48 deletions(-) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index 94bdcda13aa..4550e298435 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -116,36 +116,67 @@ export class Random { */ word(): string { const wordMethods = [ + this.faker.address.cardinalDirection, + this.faker.address.cityName, + this.faker.address.country, + this.faker.address.county, + this.faker.address.direction, + this.faker.address.ordinalDirection, + this.faker.address.state, + this.faker.address.street, + + this.faker.color.human, + this.faker.commerce.department, - this.faker.commerce.productName, + this.faker.commerce.product, this.faker.commerce.productAdjective, this.faker.commerce.productMaterial, - this.faker.commerce.product, - this.faker.color.human, + this.faker.commerce.productName, - this.faker.company.catchPhraseAdjective, - this.faker.company.catchPhraseDescriptor, - this.faker.company.catchPhraseNoun, this.faker.company.bsAdjective, this.faker.company.bsBuzz, this.faker.company.bsNoun, - this.faker.address.county, - this.faker.address.country, - this.faker.address.state, + this.faker.company.catchPhraseAdjective, + this.faker.company.catchPhraseDescriptor, + this.faker.company.catchPhraseNoun, this.faker.finance.accountName, - this.faker.finance.transactionType, this.faker.finance.currencyName, + this.faker.finance.transactionType, - this.faker.hacker.noun, - this.faker.hacker.verb, + this.faker.hacker.abbreviation, this.faker.hacker.adjective, this.faker.hacker.ingverb, - this.faker.hacker.abbreviation, + this.faker.hacker.noun, + this.faker.hacker.verb, - this.faker.name.jobDescriptor, + this.faker.lorem.word, + + this.faker.music.genre, + + this.faker.name.gender, this.faker.name.jobArea, + this.faker.name.jobDescriptor, + this.faker.name.jobTitle, this.faker.name.jobType, + this.faker.name.sex, + + () => this.faker.science.chemicalElement().name, + () => this.faker.science.unit().name, + + this.faker.vehicle.bicycle, + this.faker.vehicle.color, + this.faker.vehicle.fuel, + this.faker.vehicle.manufacturer, + this.faker.vehicle.type, + + this.faker.word.adjective, + this.faker.word.adverb, + this.faker.word.conjunction, + this.faker.word.interjection, + this.faker.word.noun, + this.faker.word.preposition, + this.faker.word.verb, ]; const bannedChars = [ @@ -178,7 +209,12 @@ export class Random { // randomly pick from the many faker methods that can generate words const randomWordMethod = this.faker.helpers.arrayElement(wordMethods); - result = randomWordMethod(); + try { + result = randomWordMethod(); + } catch { + // catch missing locale data potentially required by randomWordMethod + continue; + } } while (!result || bannedChars.some((char) => result.includes(char))); return this.faker.helpers.arrayElement(result.split(' ')); diff --git a/test/__snapshots__/random.spec.ts.snap b/test/__snapshots__/random.spec.ts.snap index fe0e8969f48..ce472e9719f 100644 --- a/test/__snapshots__/random.spec.ts.snap +++ b/test/__snapshots__/random.spec.ts.snap @@ -14,11 +14,11 @@ exports[`random > 42 > numeric > noArgs 1`] = `"4"`; exports[`random > 42 > numeric > withLength 1`] = `"47917"`; -exports[`random > 42 > word 1`] = `"impactful"`; +exports[`random > 42 > word 1`] = `"responsive"`; -exports[`random > 42 > words > noArgs 1`] = `"mobile Fish"`; +exports[`random > 42 > words > noArgs 1`] = `"lavender Shoes"`; -exports[`random > 42 > words > withLength 1`] = `"impactful Directives transmit Home initiatives"`; +exports[`random > 42 > words > withLength 1`] = `"responsive comeback Neptunium Hip deposit"`; exports[`random > 1211 > alpha > noArgs 1`] = `"y"`; @@ -34,11 +34,11 @@ exports[`random > 1211 > numeric > noArgs 1`] = `"9"`; exports[`random > 1211 > numeric > withLength 1`] = `"94872"`; -exports[`random > 1211 > word 1`] = `"Division"`; +exports[`random > 1211 > word 1`] = `"gah"`; -exports[`random > 1211 > words > noArgs 1`] = `"functionalities Frozen Baby"`; +exports[`random > 1211 > words > noArgs 1`] = `"invoice Cyclocross assault"`; -exports[`random > 1211 > words > withLength 1`] = `"Division Forward purple Supervisor Peso"`; +exports[`random > 1211 > words > withLength 1`] = `"gah strictly Rustic assault Manager"`; exports[`random > 1337 > alpha > noArgs 1`] = `"g"`; @@ -54,8 +54,8 @@ exports[`random > 1337 > numeric > noArgs 1`] = `"3"`; exports[`random > 1337 > numeric > withLength 1`] = `"35122"`; -exports[`random > 1337 > word 1`] = `"Organic"`; +exports[`random > 1337 > word 1`] = `"Bespoke"`; -exports[`random > 1337 > words > noArgs 1`] = `"Delaware"`; +exports[`random > 1337 > words > noArgs 1`] = `"nesciunt"`; -exports[`random > 1337 > words > withLength 1`] = `"Organic Bike interactive core Montenegro"`; +exports[`random > 1337 > words > withLength 1`] = `"Bespoke connect Folsom Sleek Folk"`; diff --git a/test/__snapshots__/system.spec.ts.snap b/test/__snapshots__/system.spec.ts.snap index f3eda41ad97..3cbabb8f641 100644 --- a/test/__snapshots__/system.spec.ts.snap +++ b/test/__snapshots__/system.spec.ts.snap @@ -2,9 +2,9 @@ exports[`system > 42 > commonFileExt 1`] = `"png"`; -exports[`system > 42 > commonFileName > noArgs 1`] = `"mobile_fish.mpe"`; +exports[`system > 42 > commonFileName > noArgs 1`] = `"lavender_shoes.mpe"`; -exports[`system > 42 > commonFileName > with extension 1`] = `"mobile_fish.ext"`; +exports[`system > 42 > commonFileName > with extension 1`] = `"lavender_shoes.ext"`; exports[`system > 42 > commonFileType 1`] = `"audio"`; @@ -14,11 +14,11 @@ exports[`system > 42 > fileExt > noArgs 1`] = `"lrm"`; exports[`system > 42 > fileExt > with mimeType 1`] = `"json"`; -exports[`system > 42 > fileName > noArgs 1`] = `"mobile_fish.jxsc"`; +exports[`system > 42 > fileName > noArgs 1`] = `"lavender_shoes.jxsc"`; -exports[`system > 42 > fileName > with extensionCount 1`] = `"mobile_fish.jxsc.jardiff"`; +exports[`system > 42 > fileName > with extensionCount 1`] = `"lavender_shoes.jxsc.jardiff"`; -exports[`system > 42 > filePath 1`] = `"/opt/bin/directives_transmit_home.ptid"`; +exports[`system > 42 > filePath 1`] = `"/opt/bin/comeback_neptunium_hip.ptid"`; exports[`system > 42 > fileType 1`] = `"image"`; @@ -70,9 +70,9 @@ exports[`system > 42 > semver 1`] = `"3.7.9"`; exports[`system > 1211 > commonFileExt 1`] = `"htm"`; -exports[`system > 1211 > commonFileName > noArgs 1`] = `"functionalities_frozen_baby.m2a"`; +exports[`system > 1211 > commonFileName > noArgs 1`] = `"invoice_cyclocross_assault.mp4"`; -exports[`system > 1211 > commonFileName > with extension 1`] = `"functionalities_frozen_baby.ext"`; +exports[`system > 1211 > commonFileName > with extension 1`] = `"invoice_cyclocross_assault.ext"`; exports[`system > 1211 > commonFileType 1`] = `"application"`; @@ -82,11 +82,11 @@ exports[`system > 1211 > fileExt > noArgs 1`] = `"dic"`; exports[`system > 1211 > fileExt > with mimeType 1`] = `"map"`; -exports[`system > 1211 > fileName > noArgs 1`] = `"functionalities_frozen_baby.tsd"`; +exports[`system > 1211 > fileName > noArgs 1`] = `"invoice_cyclocross_assault.z2"`; -exports[`system > 1211 > fileName > with extensionCount 1`] = `"functionalities_frozen_baby.tsd.xml"`; +exports[`system > 1211 > fileName > with extensionCount 1`] = `"invoice_cyclocross_assault.z2.tsd"`; -exports[`system > 1211 > filePath 1`] = `"/var/log/forward_purple.avi"`; +exports[`system > 1211 > filePath 1`] = `"/var/log/strictly_rustic.avi"`; exports[`system > 1211 > fileType 1`] = `"x-shader"`; @@ -138,9 +138,9 @@ exports[`system > 1211 > semver 1`] = `"9.4.8"`; exports[`system > 1337 > commonFileExt 1`] = `"wav"`; -exports[`system > 1337 > commonFileName > noArgs 1`] = `"delaware.mp2"`; +exports[`system > 1337 > commonFileName > noArgs 1`] = `"nesciunt.mp2"`; -exports[`system > 1337 > commonFileName > with extension 1`] = `"delaware.ext"`; +exports[`system > 1337 > commonFileName > with extension 1`] = `"nesciunt.ext"`; exports[`system > 1337 > commonFileType 1`] = `"audio"`; @@ -150,11 +150,11 @@ exports[`system > 1337 > fileExt > noArgs 1`] = `"oa3"`; exports[`system > 1337 > fileExt > with mimeType 1`] = `"json"`; -exports[`system > 1337 > fileName > noArgs 1`] = `"delaware.cmc"`; +exports[`system > 1337 > fileName > noArgs 1`] = `"nesciunt.cmc"`; -exports[`system > 1337 > fileName > with extensionCount 1`] = `"delaware.cmc.kml"`; +exports[`system > 1337 > fileName > with extensionCount 1`] = `"nesciunt.cmc.kml"`; -exports[`system > 1337 > filePath 1`] = `"/Library/bike_interactive.link66"`; +exports[`system > 1337 > filePath 1`] = `"/Library/duane_folsom.dpg"`; exports[`system > 1337 > fileType 1`] = `"font"`; @@ -206,7 +206,7 @@ exports[`system > 1337 > semver 1`] = `"2.5.1"`; exports[`system > seed: 42 > commonFileExt() 1`] = `"png"`; -exports[`system > seed: 42 > commonFileName() 1`] = `"mobile_fish.mpe"`; +exports[`system > seed: 42 > commonFileName() 1`] = `"lavender_shoes.mpe"`; exports[`system > seed: 42 > commonFileType() 1`] = `"audio"`; @@ -214,9 +214,9 @@ exports[`system > seed: 42 > directoryPath() 1`] = `"/opt/bin"`; exports[`system > seed: 42 > fileExt() 1`] = `"lrm"`; -exports[`system > seed: 42 > fileName() 1`] = `"mobile_fish.jxsc"`; +exports[`system > seed: 42 > fileName() 1`] = `"lavender_shoes.jxsc"`; -exports[`system > seed: 42 > filePath() 1`] = `"/opt/bin/directives_transmit_home.ptid"`; +exports[`system > seed: 42 > filePath() 1`] = `"/opt/bin/comeback_neptunium_hip.ptid"`; exports[`system > seed: 42 > fileType() 1`] = `"image"`; @@ -228,7 +228,7 @@ exports[`system > seed: 42 > semver() 1`] = `"3.7.9"`; exports[`system > seed: 1211 > commonFileExt() 1`] = `"htm"`; -exports[`system > seed: 1211 > commonFileName() 1`] = `"functionalities_frozen_baby.m2a"`; +exports[`system > seed: 1211 > commonFileName() 1`] = `"invoice_cyclocross_assault.mp4"`; exports[`system > seed: 1211 > commonFileType() 1`] = `"application"`; @@ -236,9 +236,9 @@ exports[`system > seed: 1211 > directoryPath() 1`] = `"/var/log"`; exports[`system > seed: 1211 > fileExt() 1`] = `"dic"`; -exports[`system > seed: 1211 > fileName() 1`] = `"functionalities_frozen_baby.tsd"`; +exports[`system > seed: 1211 > fileName() 1`] = `"invoice_cyclocross_assault.z2"`; -exports[`system > seed: 1211 > filePath() 1`] = `"/var/log/forward_purple.avi"`; +exports[`system > seed: 1211 > filePath() 1`] = `"/var/log/strictly_rustic.avi"`; exports[`system > seed: 1211 > fileType() 1`] = `"x-shader"`; @@ -250,7 +250,7 @@ exports[`system > seed: 1211 > semver() 1`] = `"9.4.8"`; exports[`system > seed: 1337 > commonFileExt() 1`] = `"wav"`; -exports[`system > seed: 1337 > commonFileName() 1`] = `"delaware.mp2"`; +exports[`system > seed: 1337 > commonFileName() 1`] = `"nesciunt.mp2"`; exports[`system > seed: 1337 > commonFileType() 1`] = `"audio"`; @@ -258,9 +258,9 @@ exports[`system > seed: 1337 > directoryPath() 1`] = `"/Library"`; exports[`system > seed: 1337 > fileExt() 1`] = `"oa3"`; -exports[`system > seed: 1337 > fileName() 1`] = `"delaware.cmc"`; +exports[`system > seed: 1337 > fileName() 1`] = `"nesciunt.cmc"`; -exports[`system > seed: 1337 > filePath() 1`] = `"/Library/bike_interactive.link66"`; +exports[`system > seed: 1337 > filePath() 1`] = `"/Library/duane_folsom.dpg"`; exports[`system > seed: 1337 > fileType() 1`] = `"font"`;