File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed
Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,8 @@ class Expect extends gns.Expect {
5555 void toHaveCssStyle (styles) {
5656 gns.guinness.matchers.toBeTrue (elementContainsStyle (actual, styles));
5757 }
58+ void toMatchPattern (pattern) =>
59+ gns.guinness.matchers.toBeTrue (pattern.hasMatch (actual));
5860 void toImplement (expected) => toBeA (expected);
5961 void toBeNaN () =>
6062 gns.guinness.matchers.toBeTrue (double .NAN .compareTo (actual) == 0 );
@@ -98,6 +100,8 @@ class NotExpect extends gns.NotExpect {
98100 void toHaveCssStyle (styles) {
99101 gns.guinness.matchers.toBeFalse (elementContainsStyle (actual, styles));
100102 }
103+ void toMatchPattern (pattern) =>
104+ gns.guinness.matchers.toBeFalse (pattern.hasMatch (actual));
101105 void toBeNull () => gns.guinness.matchers.toBeFalse (actual == null );
102106 Function get _expect => gns.guinness.matchers.expect;
103107}
Original file line number Diff line number Diff line change @@ -78,6 +78,15 @@ export interface NgMatchers extends jasmine.Matchers {
7878 */
7979 toThrowErrorWith ( expectedMessage : any ) : boolean ;
8080
81+ /**
82+ * Expect a string to match the given regular expression.
83+ *
84+ * ## Example
85+ *
86+ * {@example testing/ts/matchers.ts region='toMatchPattern'}
87+ */
88+ toMatchPattern ( expectedMessage : any ) : boolean ;
89+
8190 /**
8291 * Invert the matchers.
8392 */
@@ -240,6 +249,21 @@ _global.beforeEach(function() {
240249 } ;
241250 } ,
242251
252+ toMatchPattern ( ) {
253+ return { compare : buildError ( false ) , negativeCompare : buildError ( true ) } ;
254+
255+ function buildError ( isNot ) {
256+ return function ( actual , regex ) {
257+ return {
258+ pass : regex . test ( actual ) == ! isNot ,
259+ get message ( ) {
260+ return `Expected ${ actual } ${ isNot ? 'not ' : '' } to match ${ regex . toString ( ) } ` ;
261+ }
262+ } ;
263+ } ;
264+ }
265+ } ,
266+
243267 toImplement : function ( ) {
244268 return {
245269 compare : function ( actualObject , expectedInterface ) {
Original file line number Diff line number Diff line change @@ -54,6 +54,14 @@ export function main() {
5454 } ) ;
5555 } ) ;
5656
57+ describe ( "toMatchPAttern" , ( ) => {
58+ it ( "should assert that a string matches a given pattern" , ( ) => {
59+ expect ( "matias" ) . toMatchPattern ( / i a s $ / g) ;
60+ expect ( "tobias" ) . toMatchPattern ( / i a s $ / g) ;
61+ expect ( "joonas" ) . not . toMatchPattern ( / i a s $ / g) ;
62+ } ) ;
63+ } ) ;
64+
5765 describe ( 'toEqual for Maps' , ( ) => {
5866 it ( 'should detect equality for same reference' , ( ) => {
5967 var m1 = MapWrapper . createFromStringMap ( { 'a' : 1 } ) ;
You can’t perform that action at this time.
0 commit comments