Skip to content

Commit

Permalink
Added first tests for microsoft#6579
Browse files Browse the repository at this point in the history
  • Loading branch information
Igmat committed Nov 18, 2016
1 parent a7d97c0 commit 70ccfdf
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/cases/compiler/regexValidatedType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type CssColor = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i;
type Email = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
type Gmail = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@gmail\.com$/i;
34 changes: 34 additions & 0 deletions tests/cases/compiler/regexValidatedTypeAssignment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
type Email = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
type Gmail = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@gmail\.com$/i;

let email: Email;
let gmail: Gmail;
email = 'test@example.com';
email = 'test@gmail.com';
gmail = 'test@example.com';
gmail = 'test@gmail.com';
gmail = email;
email = gmail;

gmail = <Gmail>email;
if (Gmail.test(email)) {
gmail = email;
}

let someEmail = 'test@example.com';
let someGmail = 'test@gmail.com';
email = someEmail;
gmail = someGmail;
email = <Email>someEmail;
gmail = <Gmail>someGmail;
if (Email.test(someEmail)) {
email = someEmail;
}
if (Gmail.test(someGmail)) {
gmail = someGmail;
}

let someEmailLiteral: 'test@example.com' = 'test@example.com';
let someGmailLiteral: 'test@gmail.com' = 'test@gmail.com';
email = someEmailLiteral;
gmail = someGmailLiteral;
35 changes: 35 additions & 0 deletions tests/cases/compiler/regexValidatedTypeIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type Email = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
type Gmail = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@gmail\.com$/i;

interface User {
usersProperty: number;
}
interface GmailUser {
gmailProperty: number;
}

interface UsersCollection {
[email: Email]: User;
[gmail: Gmail]: GmailUser;
}

let collection: UsersCollection;
let someEmail = 'test@example.com';
let someGmail = 'test@gmail.com';

collection['test@example.com'].usersProperty;
collection['test@example.com'].gmailProperty;
collection['test@gmail.com'].usersProperty;
collection['test@gmail.com'].gmailProperty;

collection[someEmail].usersProperty;
collection[someEmail].gmailProperty;
collection[someGmail].usersProperty;
collection[someGmail].gmailProperty;

collection[<Email>someEmail].usersProperty;
collection[<Email>someEmail].gmailProperty;
collection[<Gmail>someGmail].usersProperty;
collection[<Gmail>someGmail].gmailProperty;
collection[<Email & Gmail> someGmail].usersProperty;
collection[<Email & Gmail> someGmail].gmailProperty;

0 comments on commit 70ccfdf

Please sign in to comment.