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

Implement Email arbitrary #345

Merged
merged 3 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/Arbitraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ More specific strings:
- `fc.webQueryParameters()` Query parameters to build an URI. Fragment is the optional part right after the ? in an URI
- `fc.webSegment()` Web URL path segment
- `fc.webUrl()` Web URL following the specs specified by RFC 3986 and WHATWG URL Standard
- `fc.emailAddress()` Email address following RFC 1123 and RFC 5322

## Combinors of arbitraries (:T)

Expand Down
17 changes: 17 additions & 0 deletions src/check/arbitrary/EmailArbitrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { array } from './ArrayArbitrary';
import { buildLowerAlphaNumericArb } from './helpers/SpecificCharacterRange';
import { domain } from './HostArbitrary';
import { stringOf } from './StringArbitrary';
import { tuple } from './TupleArbitrary';

/**
* For email address
*
* According to RFC 5322 - https://www.ietf.org/rfc/rfc5322.txt
*/
export function emailAddress() {
const others = ['!', '#', '$', '%', '&', "'", '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}', '~'];
const atextArb = buildLowerAlphaNumericArb(others);
const dotAtomArb = array(stringOf(atextArb, 1, 10), 1, 5).map(a => a.join('.'));
return tuple(dotAtomArb, domain()).map(([lp, d]) => `${lp}@${d}`);
}
2 changes: 2 additions & 0 deletions src/fast-check-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { dedup } from './check/arbitrary/DedupArbitrary';
import { Arbitrary } from './check/arbitrary/definition/Arbitrary';
import { Shrinkable } from './check/arbitrary/definition/Shrinkable';
import { dictionary } from './check/arbitrary/DictionaryArbitrary';
import { emailAddress } from './check/arbitrary/EmailArbitrary';
import { double, float } from './check/arbitrary/FloatingPointArbitrary';
import { frequency } from './check/arbitrary/FrequencyArbitrary';
import { compareBooleanFunc, compareFunc, func } from './check/arbitrary/FunctionArbitrary';
Expand Down Expand Up @@ -157,6 +158,7 @@ export {
webFragments,
webQueryParameters,
webUrl,
emailAddress,
// model-based
AsyncCommand,
Command,
Expand Down
30 changes: 30 additions & 0 deletions test/unit/check/arbitrary/EmailArbitrary.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { emailAddress } from '../../../../src/check/arbitrary/EmailArbitrary';

import * as genericHelper from './generic/GenericArbitraryHelper';

const isValidEmailRfc1123 = (t: string) => {
// Taken from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
const rfc1123 = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
return rfc1123.test(t);
};

const isValidEmailRfc5322 = (t: string) => {
// Taken from https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression
const rfc5322 = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return rfc5322.test(t);
};

describe('EmailArbitrary', () => {
describe('emailAddress', () => {
describe('RFC 1123', () => {
genericHelper.isValidArbitrary(() => emailAddress(), {
isValidValue: (g: string) => isValidEmailRfc1123(g)
});
});
describe('RFC 5322', () => {
genericHelper.isValidArbitrary(() => emailAddress(), {
isValidValue: (g: string) => isValidEmailRfc5322(g)
});
});
});
});