From 048cbfc8aa903527705887119e24fd956cce6615 Mon Sep 17 00:00:00 2001 From: peng <11710918@mail.sustech.edu.cn> Date: Fri, 15 May 2020 09:56:18 +0800 Subject: [PATCH 1/2] fixed issue 468 --- README.md | 1 + src/main/java/com/github/javafaker/Faker.java | 43 +++++++++++------- .../java/com/github/javafaker/Gender.java | 45 +++++++++++++++++++ src/main/resources/en/gender.yml | 11 ++--- .../java/com/github/javafaker/GenderTest.java | 25 +++++++++++ 5 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/github/javafaker/Gender.java create mode 100644 src/test/java/com/github/javafaker/GenderTest.java diff --git a/README.md b/README.md index 607ae4a0..aa9bca45 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ Fakers * Friends * FunnyName * GameOfThrones +* Gender * Hacker * HarryPotter * Hipster diff --git a/src/main/java/com/github/javafaker/Faker.java b/src/main/java/com/github/javafaker/Faker.java index 8942ee82..11776b0d 100644 --- a/src/main/java/com/github/javafaker/Faker.java +++ b/src/main/java/com/github/javafaker/Faker.java @@ -13,9 +13,12 @@ import java.util.Random; * @author ren */ public class Faker { + private final RandomService randomService; private final FakeValuesService fakeValuesService; + + private final Ancient ancient; private final App app; private final Artist artist; @@ -42,10 +45,10 @@ public class Faker { private final Hacker hacker; private final Options options; private final Code code; - private final Coin coin; private final Finance finance; private final Food food; private final GameOfThrones gameOfThrones; + private final Gender gender; private final DateAndTime dateAndTime; private final Demographic demographic; private final Dog dog; @@ -96,14 +99,13 @@ public class Faker { private final Dune dune; private final AquaTeenHungerForce aquaTeenHungerForce; private final ProgrammingLanguage programmingLanguage; - private final Kaamelott kaamelott; public Faker() { this(Locale.ENGLISH); } public Faker(Locale locale) { - this(locale, (Random)null); + this(locale, (Random) null); } public Faker(Random random) { @@ -122,6 +124,8 @@ public class Faker { this.randomService = random; this.fakeValuesService = fakeValuesService; + + this.ancient = new Ancient(this); this.app = new App(this); this.artist = new Artist(this); @@ -152,6 +156,7 @@ public class Faker { this.finance = new Finance(this); this.food = new Food(this); this.gameOfThrones = new GameOfThrones(this); + this.gender = new Gender(this); this.dateAndTime = new DateAndTime(this); this.demographic = new Demographic(this); this.dog = new Dog(this); @@ -159,7 +164,6 @@ public class Faker { this.shakespeare = new Shakespeare(this); this.slackEmoji = new SlackEmoji(this); this.space = new Space(this); - this.coin = new Coin(this); this.superhero = new Superhero(this); this.team = new Team(this); this.bool = new Bool(this); @@ -202,7 +206,6 @@ public class Faker { this.dune = new Dune(this); this.aquaTeenHungerForce = new AquaTeenHungerForce(this); this.programmingLanguage = new ProgrammingLanguage(this); - this.kaamelott = new Kaamelott(this); } /** @@ -327,6 +330,9 @@ public class Faker { return this.fakeValuesService; } + + + public Ancient ancient() { return ancient; } @@ -427,10 +433,6 @@ public class Faker { return code; } - public Coin coin() { - return coin; - } - public File file() { return file; } @@ -450,6 +452,10 @@ public class Faker { public GameOfThrones gameOfThrones() { return gameOfThrones; } + + public Gender gender() { + return gender; + } public DateAndTime date() { return dateAndTime; @@ -459,6 +465,7 @@ public class Faker { return demographic; } + public Dog dog() { return dog; } @@ -603,14 +610,20 @@ public class Faker { return lebowski; } - public Medical medical(){return medical;} + public Medical medical() { + return medical; + } - public Country country(){ return country;} + public Country country() { + return country; + } - public Animal animal(){ return animal; } + public Animal animal() { + return animal; + } public BackToTheFuture backToTheFuture() { - return backToTheFuture; + return backToTheFuture; } public PrincessBride princessBride() { @@ -637,10 +650,6 @@ public class Faker { return programmingLanguage; } - public Kaamelott kaamelott() { - return kaamelott; - } - public String resolve(String key) { return this.fakeValuesService.resolve(key, this, this); } diff --git a/src/main/java/com/github/javafaker/Gender.java b/src/main/java/com/github/javafaker/Gender.java new file mode 100644 index 00000000..a0a340c9 --- /dev/null +++ b/src/main/java/com/github/javafaker/Gender.java @@ -0,0 +1,45 @@ +package com.github.javafaker; + +/** + * This class is used to generate gender randomly. + * + * @author Peng + */ + +public class Gender { + private final Faker faker; + + protected Gender(Faker faker) { + this.faker = faker; + } + + /** + * This method returns a gender type + * + * @return a string of gender type + */ + public String types() { + + return faker.fakeValuesService().fetchString("gender.types"); + } + + /** + * This method returns a binary gender type + * + * @return a string of binary gender type + */ + public String binaryTypes() { + + return faker.fakeValuesService().fetchString("gender.binary_types"); + } + + /** + * This method returns a short binary gender type + * + * @return a string of short binary gender type + */ + public String shortBinaryTypes() { + + return faker.fakeValuesService().fetchString("gender.short_binary_types"); + } +} diff --git a/src/main/resources/en/gender.yml b/src/main/resources/en/gender.yml index 5c1b26fd..c727b2bf 100644 --- a/src/main/resources/en/gender.yml +++ b/src/main/resources/en/gender.yml @@ -1,5 +1,6 @@ -en: - faker: - gender: - types: ["Female", "Male", "Non-binary", "Agender", "Genderfluid", "Genderqueer", "Bigender", "Polygender"] - binary_types: ["Female", "Male"] +en: + faker: + gender: + types: ["Female", "Male", "NonBinary", "Agender", "Genderfluid", "Genderqueer", "Bigender", "Polygender"] + binary_types: ["Female", "Male"] + short_binary_types: ["f", "m"] \ No newline at end of file diff --git a/src/test/java/com/github/javafaker/GenderTest.java b/src/test/java/com/github/javafaker/GenderTest.java new file mode 100644 index 00000000..c937c730 --- /dev/null +++ b/src/test/java/com/github/javafaker/GenderTest.java @@ -0,0 +1,25 @@ +package com.github.javafaker; + +import org.junit.Test; + +import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression; +import static org.junit.Assert.assertThat; + +public class GenderTest extends AbstractFakerTest { + + @Test + public void types() { + assertThat(faker.gender().types(), matchesRegularExpression("(\\w+ ?){1,2}")); + } + + @Test + public void binaryTypes() { + assertThat(faker.gender().binaryTypes(), matchesRegularExpression("[A-Za-z ]+")); + } + + @Test + public void shortBinaryTypes() { + assertThat(faker.gender().shortBinaryTypes(), matchesRegularExpression("f|m")); + } + +} From 2183c3fd8d108f9d84ffc725af003f60a8ab1b7f Mon Sep 17 00:00:00 2001 From: peng <11710918@mail.sustech.edu.cn> Date: Fri, 15 May 2020 10:13:31 +0800 Subject: [PATCH 2/2] fixed issue 468 --- src/main/java/com/github/javafaker/Faker.java | 31 ++++++++++--------- .../github/javafaker/integration/FakerIT.java | 1 + 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/github/javafaker/Faker.java b/src/main/java/com/github/javafaker/Faker.java index 11776b0d..fd556612 100644 --- a/src/main/java/com/github/javafaker/Faker.java +++ b/src/main/java/com/github/javafaker/Faker.java @@ -13,12 +13,9 @@ import java.util.Random; * @author ren */ public class Faker { - private final RandomService randomService; private final FakeValuesService fakeValuesService; - - private final Ancient ancient; private final App app; private final Artist artist; @@ -45,10 +42,11 @@ public class Faker { private final Hacker hacker; private final Options options; private final Code code; + private final Coin coin; private final Finance finance; private final Food food; private final GameOfThrones gameOfThrones; - private final Gender gender; + private final Gender gender; private final DateAndTime dateAndTime; private final Demographic demographic; private final Dog dog; @@ -99,6 +97,7 @@ public class Faker { private final Dune dune; private final AquaTeenHungerForce aquaTeenHungerForce; private final ProgrammingLanguage programmingLanguage; + private final Kaamelott kaamelott; public Faker() { this(Locale.ENGLISH); @@ -124,8 +123,6 @@ public class Faker { this.randomService = random; this.fakeValuesService = fakeValuesService; - - this.ancient = new Ancient(this); this.app = new App(this); this.artist = new Artist(this); @@ -156,7 +153,7 @@ public class Faker { this.finance = new Finance(this); this.food = new Food(this); this.gameOfThrones = new GameOfThrones(this); - this.gender = new Gender(this); + this.gender = new Gender(this); this.dateAndTime = new DateAndTime(this); this.demographic = new Demographic(this); this.dog = new Dog(this); @@ -164,6 +161,7 @@ public class Faker { this.shakespeare = new Shakespeare(this); this.slackEmoji = new SlackEmoji(this); this.space = new Space(this); + this.coin = new Coin(this); this.superhero = new Superhero(this); this.team = new Team(this); this.bool = new Bool(this); @@ -206,6 +204,7 @@ public class Faker { this.dune = new Dune(this); this.aquaTeenHungerForce = new AquaTeenHungerForce(this); this.programmingLanguage = new ProgrammingLanguage(this); + this.kaamelott = new Kaamelott(this); } /** @@ -330,9 +329,6 @@ public class Faker { return this.fakeValuesService; } - - - public Ancient ancient() { return ancient; } @@ -433,6 +429,10 @@ public class Faker { return code; } + public Coin coin() { + return coin; + } + public File file() { return file; } @@ -452,8 +452,8 @@ public class Faker { public GameOfThrones gameOfThrones() { return gameOfThrones; } - - public Gender gender() { + + public Gender gender() { return gender; } @@ -465,7 +465,6 @@ public class Faker { return demographic; } - public Dog dog() { return dog; } @@ -650,6 +649,10 @@ public class Faker { return programmingLanguage; } + public Kaamelott kaamelott() { + return kaamelott; + } + public String resolve(String key) { return this.fakeValuesService.resolve(key, this, this); } @@ -673,4 +676,4 @@ public class Faker { public String expression(String expression) { return this.fakeValuesService.expression(expression, this); } -} +} \ No newline at end of file diff --git a/src/test/java/com/github/javafaker/integration/FakerIT.java b/src/test/java/com/github/javafaker/integration/FakerIT.java index 4ca24be0..c62ae041 100644 --- a/src/test/java/com/github/javafaker/integration/FakerIT.java +++ b/src/test/java/com/github/javafaker/integration/FakerIT.java @@ -126,6 +126,7 @@ public class FakerIT { testAllMethodsThatReturnStringsActuallyReturnStrings(faker.finance()); testAllMethodsThatReturnStringsActuallyReturnStrings(faker.food()); testAllMethodsThatReturnStringsActuallyReturnStrings(faker.gameOfThrones()); + testAllMethodsThatReturnStringsActuallyReturnStrings(faker.gender()); testAllMethodsThatReturnStringsActuallyReturnStrings(faker.hacker()); testAllMethodsThatReturnStringsActuallyReturnStrings(faker.idNumber()); testAllMethodsThatReturnStringsActuallyReturnStrings(faker.shakespeare());