aboutsummaryrefslogtreecommitdiff
path: root/test/string.spec.ts
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2022-11-21 17:55:32 +0100
committerGitHub <[email protected]>2022-11-21 17:55:32 +0100
commit9cd716e891d3bb8d9a8f9d43899d0dcd161e1832 (patch)
treeb8e24d4d9d9c6372e3b687e64600de0f825a33bc /test/string.spec.ts
parent7cbeda6eeab94eef6e8f56f9e31cc57c4072f3b2 (diff)
downloadfaker-9cd716e891d3bb8d9a8f9d43899d0dcd161e1832.tar.xz
faker-9cd716e891d3bb8d9a8f9d43899d0dcd161e1832.zip
feat(helpers): add rangeToNumber method and add range parameters (#1486)
Diffstat (limited to 'test/string.spec.ts')
-rw-r--r--test/string.spec.ts78
1 files changed, 75 insertions, 3 deletions
diff --git a/test/string.spec.ts b/test/string.spec.ts
index 6d34f098..32dc2ddf 100644
--- a/test/string.spec.ts
+++ b/test/string.spec.ts
@@ -11,6 +11,7 @@ describe('string', () => {
t.it('noArgs')
.itRepeated('with length parameter', 5, 5)
.it('with length', { length: 6 })
+ .it('with length range', { length: { min: 10, max: 20 } })
.it('with casing = lower', { casing: 'lower' })
.it('with casing = upper', { casing: 'upper' })
.it('with casing = mixed', { casing: 'mixed' })
@@ -26,6 +27,7 @@ describe('string', () => {
t.it('noArgs')
.itRepeated('with length parameter', 5, 5)
.it('with length', { length: 6 })
+ .it('with length range', { length: { min: 10, max: 20 } })
.it('with casing = lower', { casing: 'lower' })
.it('with casing = upper', { casing: 'upper' })
.it('with casing = mixed', { casing: 'mixed' })
@@ -40,6 +42,7 @@ describe('string', () => {
t.describe('hexadecimal', (t) => {
t.it('noArgs')
.it('with length', { length: 6 })
+ .it('with length range', { length: { min: 10, max: 20 } })
.it('with casing = lower', { casing: 'lower' })
.it('with casing = upper', { casing: 'upper' })
.it('with casing = mixed', { casing: 'mixed' })
@@ -55,6 +58,7 @@ describe('string', () => {
t.it('noArgs')
.itRepeated('with length parameter', 5, 5)
.it('with length', { length: 6 })
+ .it('with length range', { length: { min: 10, max: 20 } })
.it('with allowLeadingZeros', { allowLeadingZeros: true })
.it('with exclude', { exclude: '12345' })
.it('with length, allowLeadingZeros and exclude', {
@@ -65,7 +69,9 @@ describe('string', () => {
});
t.describe('sample', (t) => {
- t.it('noArgs').itRepeated('with length parameter', 5, 5);
+ t.it('noArgs')
+ .itRepeated('with length parameter', 5, 5)
+ .it('with length range', { min: 10, max: 20 });
});
t.itRepeated('uuid', 5);
@@ -95,7 +101,7 @@ describe('string', () => {
expect(actual).toMatch(pattern);
});
- it('should generate many random letters', () => {
+ it('should generate 5 random letters', () => {
const actual = faker.string.alpha(5);
expect(actual).toHaveLength(5);
@@ -110,6 +116,16 @@ describe('string', () => {
}
);
+ it('should return a random amount of characters', () => {
+ const actual = faker.string.alpha({ length: { min: 10, max: 20 } });
+
+ expect(actual).toBeTruthy();
+ expect(actual).toBeTypeOf('string');
+
+ expect(actual.length).toBeGreaterThanOrEqual(10);
+ expect(actual.length).toBeLessThanOrEqual(20);
+ });
+
it('should be able to ban some characters', () => {
const actual = faker.string.alpha({
length: 5,
@@ -190,7 +206,7 @@ describe('string', () => {
expect(actual).toMatch(pattern);
});
- it('should generate many random characters', () => {
+ it('should generate 5 random characters', () => {
const actual = faker.string.alphanumeric(5);
expect(actual).toHaveLength(5);
@@ -205,6 +221,18 @@ describe('string', () => {
}
);
+ it('should return a random amount of characters', () => {
+ const actual = faker.string.alphanumeric({
+ length: { min: 10, max: 20 },
+ });
+
+ expect(actual).toBeTruthy();
+ expect(actual).toBeTypeOf('string');
+
+ expect(actual.length).toBeGreaterThanOrEqual(10);
+ expect(actual.length).toBeLessThanOrEqual(20);
+ });
+
it('should be able to ban all alphabetic characters', () => {
const exclude = 'abcdefghijklmnopqrstuvwxyz'.split('');
const alphaText = faker.string.alphanumeric({
@@ -325,6 +353,28 @@ describe('string', () => {
expect(hex).toMatch(/^[0-9a-f]*$/i);
expect(hex).toHaveLength(5);
});
+
+ it.each([0, -1, -100])(
+ 'should return the prefix when length is <= 0',
+ (length) => {
+ const actual = faker.string.hexadecimal({ length });
+
+ expect(actual).toBe('0x');
+ }
+ );
+
+ it('should return a random amount of characters', () => {
+ const actual = faker.string.hexadecimal({
+ length: { min: 10, max: 20 },
+ prefix: '',
+ });
+
+ expect(actual).toBeTruthy();
+ expect(actual).toBeTypeOf('string');
+
+ expect(actual.length).toBeGreaterThanOrEqual(10);
+ expect(actual.length).toBeLessThanOrEqual(20);
+ });
});
describe('numeric', () => {
@@ -345,6 +395,18 @@ describe('string', () => {
}
);
+ it('should return a random amount of characters', () => {
+ const actual = faker.string.numeric({
+ length: { min: 10, max: 20 },
+ });
+
+ expect(actual).toBeTruthy();
+ expect(actual).toBeTypeOf('string');
+
+ expect(actual.length).toBeGreaterThanOrEqual(10);
+ expect(actual.length).toBeLessThanOrEqual(20);
+ });
+
it('should return empty string with a length of 0', () => {
const actual = faker.string.numeric(0);
@@ -468,6 +530,16 @@ describe('string', () => {
const generatedString = faker.string.sample(length);
expect(generatedString).toHaveLength(length);
});
+
+ it('should return a random amount of characters', () => {
+ const actual = faker.string.sample({ min: 10, max: 20 });
+
+ expect(actual).toBeTruthy();
+ expect(actual).toBeTypeOf('string');
+
+ expect(actual.length).toBeGreaterThanOrEqual(10);
+ expect(actual.length).toBeLessThanOrEqual(20);
+ });
});
describe(`uuid`, () => {