aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorShinigami <[email protected]>2022-05-22 12:49:29 +0200
committerGitHub <[email protected]>2022-05-22 12:49:29 +0200
commita0d25bbec84c710a6dc8d2cf438af351cf486ab0 (patch)
tree047d053fcd18d7c6242f074716c2db406eeddad3 /test
parent58145dd009cf675a311523fed5850612bda2dbcd (diff)
downloadfaker-a0d25bbec84c710a6dc8d2cf438af351cf486ab0.tar.xz
faker-a0d25bbec84c710a6dc8d2cf438af351cf486ab0.zip
feat: allow banned as string (#819)
Diffstat (limited to 'test')
-rw-r--r--test/random.spec.ts74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/random.spec.ts b/test/random.spec.ts
index 3ff24ff7..fc57ca43 100644
--- a/test/random.spec.ts
+++ b/test/random.spec.ts
@@ -213,6 +213,16 @@ describe('random', () => {
expect(actual).toMatch(/^[b-oq-z]{5}$/);
});
+ it('should be able to ban some characters via string', () => {
+ const actual = faker.random.alpha({
+ count: 5,
+ bannedChars: 'ap',
+ });
+
+ expect(actual).toHaveLength(5);
+ expect(actual).toMatch(/^[b-oq-z]{5}$/);
+ });
+
it('should be able handle mistake in banned characters array', () => {
const alphaText = faker.random.alpha({
count: 5,
@@ -296,6 +306,18 @@ describe('random', () => {
}
});
+ it('should be able to ban all alphabetic characters via string', () => {
+ const bannedChars = 'abcdefghijklmnopqrstuvwxyz';
+ const alphaText = faker.random.alphaNumeric(5, {
+ bannedChars,
+ });
+
+ expect(alphaText).toHaveLength(5);
+ for (const bannedChar of bannedChars) {
+ expect(alphaText).not.includes(bannedChar);
+ }
+ });
+
it('should be able to ban all numeric characters', () => {
const bannedChars = '0123456789'.split('');
const alphaText = faker.random.alphaNumeric(5, {
@@ -308,6 +330,18 @@ describe('random', () => {
}
});
+ it('should be able to ban all numeric characters via string', () => {
+ const bannedChars = '0123456789';
+ const alphaText = faker.random.alphaNumeric(5, {
+ bannedChars,
+ });
+
+ expect(alphaText).toHaveLength(5);
+ for (const bannedChar of bannedChars) {
+ expect(alphaText).not.includes(bannedChar);
+ }
+ });
+
it('should be able to handle mistake in banned characters array', () => {
const alphaText = faker.random.alphaNumeric(5, {
bannedChars: ['a', 'p', 'a'],
@@ -330,6 +364,15 @@ describe('random', () => {
);
});
+ it('should throw if all possible characters being banned via string', () => {
+ const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789';
+ expect(() =>
+ faker.random.alphaNumeric(5, {
+ bannedChars,
+ })
+ ).toThrowError();
+ });
+
it('should not mutate the input object', () => {
const input: {
bannedChars: string[];
@@ -395,6 +438,15 @@ describe('random', () => {
expect(actual).toBe('0000');
});
+ it('should allow leading zeros via option and all other digits banned via string', () => {
+ const actual = faker.random.numeric(4, {
+ allowLeadingZeros: true,
+ bannedDigits: '123456789',
+ });
+
+ expect(actual).toBe('0000');
+ });
+
it('should fail on leading zeros via option and all other digits banned', () => {
expect(() =>
faker.random.numeric(4, {
@@ -408,6 +460,19 @@ describe('random', () => {
);
});
+ it('should fail on leading zeros via option and all other digits banned via string', () => {
+ expect(() =>
+ faker.random.numeric(4, {
+ allowLeadingZeros: false,
+ bannedDigits: '123456789',
+ })
+ ).toThrowError(
+ new FakerError(
+ 'Unable to generate numeric string, because all possible digits are banned.'
+ )
+ );
+ });
+
it('should ban all digits passed via bannedDigits', () => {
const actual = faker.random.numeric(1000, {
bannedDigits: 'c84U1'.split(''),
@@ -416,6 +481,15 @@ describe('random', () => {
expect(actual).toHaveLength(1000);
expect(actual).toMatch(/^[0235679]{1000}$/);
});
+
+ it('should ban all digits passed via bannedDigits via string', () => {
+ const actual = faker.random.numeric(1000, {
+ bannedDigits: 'c84U1',
+ });
+
+ expect(actual).toHaveLength(1000);
+ expect(actual).toMatch(/^[0235679]{1000}$/);
+ });
});
});
});