Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added random postal code generator, wikiword, tests and manual entry #28

Merged
merged 12 commits into from
Dec 28, 2017
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void registerSymbolTypes(SymbolProvider symbolProvider) throws PluginExce
add(symbolProvider, new RandomBsn());
add(symbolProvider, new RandomEmail());
add(symbolProvider, new RandomIban());
add(symbolProvider, new RandomPostalCode());
add(symbolProvider, new DefineDefault());
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/nl/hsac/fitnesse/symbols/RandomPostalCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nl.hsac.fitnesse.symbols;

import fitnesse.wikitext.parser.*;
import nl.hsac.fitnesse.util.RandomPostalCodeGenerator;

/**
* Generates a random Postal for the indicated country
* Default country is The Netherlands
* if a country code is chosen without postalcode definition, a warning is given
*/
public class RandomPostalCode extends SymbolBase implements Rule, Translation {
private static final RandomPostalCodeGenerator RANDOM_POSTAL_CODE_GENERATOR = new RandomPostalCodeGenerator();
private static final String COUNTRY_CODE = "CountryCode";

public RandomPostalCode() {
super("RandomPostalCode");
wikiMatcher(new Matcher().string("!randomPostalCode"));
wikiRule(this);
htmlTranslation(this);
}

public Maybe<Symbol> parse(Symbol current, Parser parser) {
return storeParenthesisContent(current, parser, COUNTRY_CODE);
}

public String toTarget(Translator translator, Symbol symbol) {
String param = symbol.getProperty(COUNTRY_CODE, "NL");
return RANDOM_POSTAL_CODE_GENERATOR.getRandomPostalCodeString(param);
}
}
86 changes: 86 additions & 0 deletions src/main/java/nl/hsac/fitnesse/util/RandomPostalCodeGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package nl.hsac.fitnesse.util;

public class RandomPostalCodeGenerator {
private static RandomUtil random = new RandomUtil();

private String getRandomStringNumbers(int length) {
return random.randomString("1234567890", length);
}

private String getRandomNonZeroNumber() {
return random.randomString("123456789", 1);
}

private String getRandomStringLetters(int length) {
return random.randomString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", length);
}

public String getRandomPostalCodeString(String countryCode) {
String result;
switch (countryCode) {
case "AU":
result = getRandomStringNumbers(4);
break;
case "BE":
result = getRandomNonZeroNumber() + getRandomStringNumbers(3);
break;
case "BR":
result = getRandomStringNumbers(5) +
pickOne("", "-") +
getRandomStringNumbers(3);
break;
case "CA":
result = getRandomStringLetters(1) +
getRandomStringNumbers(1) +
getRandomStringLetters(1) +
pickOne("-", " ") +
getRandomStringNumbers(1) +
getRandomStringLetters(1) +
getRandomStringNumbers(1);
break;
case "CH":
result = getRandomNonZeroNumber() + getRandomStringNumbers(3);
break;
case "DE":
result = getRandomStringNumbers(5);
break;
case "GL":
result = "39" + getRandomStringNumbers(2);
break;
case "FR":
result = getRandomStringNumbers(5);
break;
case "IT":
result = getRandomStringNumbers(5);
break;
case "NL":
result = getRandomNonZeroNumber() +
getRandomStringNumbers(3) +
getRandomStringLetters(2);
break;
case "NO":
result = getRandomStringNumbers(4);
break;
case "US":
result = getRandomStringNumbers(5);
if ("long".equals(pickOne("short", "long"))) {
result = result + "-" + getRandomStringNumbers(4);
}
break;
default:
result = "This country code is not available for random postal code generation";
}
return result;
}

private String pickOne(String result1, String result2) {
String result;
if (random.random(2) == 0) {
result = result1;
} else {
result = result2;
}
return result;
}
}

123 changes: 123 additions & 0 deletions src/test/java/nl/hsac/fitnesse/util/RandomPostalCodeGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package nl.hsac.fitnesse.util;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertTrue;

/**
* Tests RandomPostalCodeGenerator.
* The postal codes are checked on length only to validate the switch
*/
public class RandomPostalCodeGeneratorTest {
private final RandomPostalCodeGenerator postalCode = new RandomPostalCodeGenerator();

@Rule
public final ExpectedException exception = ExpectedException.none();

private boolean resultEqualsLength(String result, int length) {
return result.length() == length;
}

@Test
public void testRandomPostalCodeAustralia() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("AU", 4);
}
}

@Test
public void testRandomPostalCodeBelgium() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("BE", 4);
}
}

@Test
public void testRandomPostalCodeBrazil() {
checkCodeLengths("BR", 8, 9);
}

@Test
public void testRandomPostalCanada() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("CA", 7);
}
}

@Test
public void testRandomPostalCodeFrance() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("FR", 5);
}
}

@Test
public void testRandomPostalCodeGermany() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("DE", 5);
}
}

@Test
public void testRandomPostalCodeGreenland() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("GL", 4);
}
}

@Test
public void testRandomPostalCodeItaly() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("IT", 5);
}
}

@Test
public void testRandomPostalCodeNetherlands() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("NL", 6);
}
}

@Test
public void testRandomPostalCodeNorway() {
for (int i = 0; i < 100; i++) {
testCountryCodeToLength("NO", 4);
}
}

@Test
public void testRandomPostalCodeUnitedStates() {
checkCodeLengths("US", 5, 10);
}

private void testCountryCodeToLength(String countryCode, int length) {
String result = postalCode.getRandomPostalCodeString(countryCode);
assertTrue("Got: " + result, resultEqualsLength(result, length));
}

private void checkCodeLengths(String countryCode, int length1, int length2) {
int length1Count = 0;
int length2Count = 0;
for (int i = 0; i < 100; i++) {
int length = testCountryCodeToTwoLengths(countryCode, length1, length2);
if (length == length1) {
length1Count++;
} else {
length2Count++;
}
}
assertTrue(countryCode + ": too few length1 codes: " + length1Count, length1Count > 30);
assertTrue(countryCode + ": too few length2 codes: " + length2Count, length2Count > 30);
}

private int testCountryCodeToTwoLengths(String countryCode, int length1, int length2) {
String result = postalCode.getRandomPostalCodeString(countryCode);
boolean hasLength1 = resultEqualsLength(result, length1);
boolean hasLength2 = resultEqualsLength(result, length2);
assertTrue("Got: " + result, hasLength1 || hasLength2);
return hasLength1 ? length1 : length2;
}
}
56 changes: 29 additions & 27 deletions src/test/wiki/FitNesseRoot/HsacPlugin/Symbols.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@ Help: Wiki symbols added by hsac-fitnesse-plugin

Some extra Wiki symbols that can be used:

|comment |
|random BSN |{{{!-!randomBSN -! }}} |!randomBSN |
|random positive integer |{{{!-!randomInt -! }}} |!randomInt |
|random integer (between where upper is non-inclusive) |{{{!-!randomInt (10,20) -! }}} |!randomInt (10,20) |
|random string (random (but at most 100) length) |{{{!-!randomString -! }}} |!randomString |
|random string (fixed length) |{{{!-!randomString (20) -! }}} |!randomString (20) |
|random string (in a length range) |{{{!-!randomString (10,20) -! }}} |!randomString (10,20) |
|random string (fixed length, choice of characters) |{{{!-!randomString (10) (abcdefghijklmnopqrstuvwxyz -)-!}}}|!randomString (10) (abcdefghijklmnopqrstuvwxyz -)|
|random string (ranged, choice of characters, fixed prefix) |{{{!-!randomString (5,10) (xyz) (aap) -! }}} |!randomString (5,10) (xyz) (aap) |
|random email address (random (but at most 100) length) |{{{!-!randomEmail-! }}} |!randomEmail |
|random email address (maximal length) |{{{!-!randomEmail (40)-! }}} |!randomEmail (40) |
|random email address (maximal length, choice of domain) |{{{!-!randomEmail (50) (hsac.nl) -! }}} |!randomEmail (40) (hsac.nl) |
|months from today |{{{!-!monthsFromToday (yyyy-MM-dd) +10 -! }}} |!monthsFromToday (yyyy-MM-dd) +10 |
|last day of month |{{{!-!lastDayOfMonth (yyyy-MM-dd) +10 -! }}} |!lastDayOfMonth (yyyy-MM-dd) +10 |
|week days from today |{{{!-!weekDaysFromToday (yyyy-MM-dd) -6 -! }}} |!weekDaysFromToday (yyyy-MM-dd) -6 |
|date relative to Monday (today or next) |{{{!-!Monday (yyyy-MM-dd)-! }}} |!Monday (yyyy-MM-dd) |
|date relative to Tuesday (today or next) |{{{!-!Tuesday (yyyy-MM-dd) +1 -! }}} |!Tuesday (yyyy-MM-dd) +1 |
|date relative to Wednesday (today or next) |{{{!-!Wednesday (yyyy-MM-dd)-! }}} |!Wednesday (yyyy-MM-dd) |
|date relative to Thursday (today or next) |{{{!-!Thursday (yyyy-MM-dd) +1 -! }}} |!Thursday (yyyy-MM-dd) +1 |
|date relative to Friday (today or next) |{{{!-!Friday (yyyy-MM-dd) -7 -! }}} |!Friday (yyyy-MM-dd) -7 |
|date relative to Saturday (today or next) |{{{!-!Saturday (yyyy-MM-dd)-! }}} |!Saturday (yyyy-MM-dd) |
|date relative to Sunday (today or next) |{{{!-!Sunday (yyyy-MM-dd) +7 -! }}} |!Sunday (yyyy-MM-dd) +7 |
|wiki variable whose value may be overridden by system property|{{{!defineDefault HALLO {Hoi} }}} |!defineDefault HALLO {Hoi} |
|random Iban (random based on available countries) |{{{!-!randomIBAN-! }}} |!randomIBAN |
|random Iban (country code) |{{{!-!randomIBAN (NL)-! }}} |!randomIBAN (NL) |
|random Iban (country code, bank code) |{{{!-!randomIBAN (NL) (ABNA) -! }}} |!randomIBAN (NL) (ABNA) |
|random Iban (country code) |{{{!-!randomIBAN (FR)-! }}} |!randomIBAN (FR) |
|comment |
|random BSN |{{{!-!randomBSN -! }}} |!randomBSN |
|random positive integer |{{{!-!randomInt -! }}} |!randomInt |
|random integer (between where upper is non-inclusive) |{{{!-!randomInt (10,20) -! }}} |!randomInt (10,20) |
|random string (random (but at most 100) length) |{{{!-!randomString -! }}} |!randomString |
|random string (fixed length) |{{{!-!randomString (20) -! }}} |!randomString (20) |
|random string (in a length range) |{{{!-!randomString (10,20) -! }}} |!randomString (10,20) |
|random string (fixed length, choice of characters) |{{{!-!randomString (10) (abcdefghijklmnopqrstuvwxyz -)-!}}}|!randomString (10) (abcdefghijklmnopqrstuvwxyz -)|
|random string (ranged, choice of characters, fixed prefix) |{{{!-!randomString (5,10) (xyz) (aap) -! }}} |!randomString (5,10) (xyz) (aap) |
|random email address (random (but at most 100) length) |{{{!-!randomEmail-! }}} |!randomEmail |
|random email address (maximal length) |{{{!-!randomEmail (40)-! }}} |!randomEmail (40) |
|random email address (maximal length, choice of domain) |{{{!-!randomEmail (50) (hsac.nl) -! }}} |!randomEmail (40) (hsac.nl) |
|months from today |{{{!-!monthsFromToday (yyyy-MM-dd) +10 -! }}} |!monthsFromToday (yyyy-MM-dd) +10 |
|last day of month |{{{!-!lastDayOfMonth (yyyy-MM-dd) +10 -! }}} |!lastDayOfMonth (yyyy-MM-dd) +10 |
|week days from today |{{{!-!weekDaysFromToday (yyyy-MM-dd) -6 -! }}} |!weekDaysFromToday (yyyy-MM-dd) -6 |
|date relative to Monday (today or next) |{{{!-!Monday (yyyy-MM-dd)-! }}} |!Monday (yyyy-MM-dd) |
|date relative to Tuesday (today or next) |{{{!-!Tuesday (yyyy-MM-dd) +1 -! }}} |!Tuesday (yyyy-MM-dd) +1 |
|date relative to Wednesday (today or next) |{{{!-!Wednesday (yyyy-MM-dd)-! }}} |!Wednesday (yyyy-MM-dd) |
|date relative to Thursday (today or next) |{{{!-!Thursday (yyyy-MM-dd) +1 -! }}} |!Thursday (yyyy-MM-dd) +1 |
|date relative to Friday (today or next) |{{{!-!Friday (yyyy-MM-dd) -7 -! }}} |!Friday (yyyy-MM-dd) -7 |
|date relative to Saturday (today or next) |{{{!-!Saturday (yyyy-MM-dd)-! }}} |!Saturday (yyyy-MM-dd) |
|date relative to Sunday (today or next) |{{{!-!Sunday (yyyy-MM-dd) +7 -! }}} |!Sunday (yyyy-MM-dd) +7 |
|wiki variable whose value may be overridden by system property |{{{!defineDefault HALLO {Hoi} }}} |!defineDefault HALLO {Hoi} |
|random Iban (random based on available countries) |{{{!-!randomIBAN-! }}} |!randomIBAN |
|random Iban (country code) |{{{!-!randomIBAN (NL)-! }}} |!randomIBAN (NL) |
|random Iban (country code, bank code) |{{{!-!randomIBAN (NL) (ABNA) -! }}} |!randomIBAN (NL) (ABNA) |
|random Iban (country code) |{{{!-!randomIBAN (FR)-! }}} |!randomIBAN (FR) |
|random Postal Code (using default NL) |{{{!-!randomPostalCode-! }}} |!randomPostalCode |
|random Postal Code (possible: AU, BE, BR, CA, CH, DE, GL, FR, IT, NL, NO, US)|{{{!-!randomPostalCode (FR)-! }}} |!randomPostalCode (FR) |