1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
/*global Autolinker */
/**
* @class Autolinker.match.MatchChecker
* @singleton
*
* A testing utility used to easily make assertions about
* {@link Autolinker.match.Match} objects.
*/
Autolinker.match.MatchChecker = {
/**
* Expects an {@link Autolinker.match.Email Email} match.
*
* @param {Autolinker.match.Email} match The Match object to check.
* @param {String} email The email address to expect.
* @param {Number} offset The offset for the match in the original string to
* expect.
*/
expectEmailMatch : function( match, email, offset ) {
this.expectMatchType( match, 'Email' );
expect( match.getEmail() ).toBe( email );
expect( match.getOffset() ).toBe( offset );
},
/**
* Expects a {@link Autolinker.match.Hashtag Hashtag} match.
*
* @param {Autolinker.match.Hashtag} match The Match object to check.
* @param {String} serviceName The service name to expect of where to direct
* clicks to the hashtag to. Ex: 'facebook', 'twitter'.
* @param {String} hashtag The hashtag to expect, without the prefixed '#'
* character.
* @param {Number} offset The offset for the match in the original string to
* expect.
*/
expectHashtagMatch : function( match, serviceName, hashtag, offset ) {
this.expectMatchType( match, 'Hashtag' );
expect( match.getServiceName() ).toBe( serviceName );
expect( match.getHashtag() ).toBe( hashtag );
expect( match.getOffset() ).toBe( offset );
},
/**
* Expects a {@link Autolinker.match.Phone Phone} match.
*
* @param {Autolinker.match.Phone} match The Match object to check.
* @param {String} number The phone number to expect, without any delimiter
* characters, and without a prefixed '+' character.
* @param {Number} offset The offset for the match in the original string to
* expect.
*/
expectPhoneMatch : function( match, number, offset ) {
this.expectMatchType( match, 'Phone' );
expect( match.getNumber() ).toBe( number );
expect( match.getOffset() ).toBe( offset );
},
/**
* Expects a {@link Autolinker.match.Mention Mention} match.
*
* @param {Autolinker.match.Mention} match The Match object to check.
* @param {String} serviceName The service name to expect of where to direct
* clicks to the mention to. Ex: 'twitter', 'instagram'.
* @param {String} mention The mention to expect, without the
* prefixed '@' character.
* @param {Number} offset The offset for the match in the original string to
* expect.
*/
expectMentionMatch : function( match, serviceName, mention, offset ) {
this.expectMatchType( match, 'Mention' );
expect( match.getServiceName() ).toBe( serviceName );
expect( match.getMention() ).toBe( mention );
expect( match.getOffset() ).toBe( offset );
},
/**
* Expects a {@link Autolinker.match.Url Url} match.
*
* @param {Autolinker.match.Url} match The Match object to check.
* @param {String} url The URL to expect, with the URI scheme prepended.
* @param {Number} offset The offset for the match in the original string to
* expect.
*/
expectUrlMatch : function( match, url, offset ) {
this.expectMatchType( match, 'Url' );
expect( match.getUrl() ).toBe( url );
expect( match.getOffset() ).toBe( offset );
},
// ---------------------------------------
/**
* Private utility method used to check the type of the `match` object
* provided, and throws if it does not match the provided `typeName`.
*
* @param {Autolinker.match.Match} match The Match object to check against
* the provided `typeName`.
* @param {String} typeName The name of the Match subclass. Ex: 'Email',
* 'Twitter', 'Url', etc.
* @throws {Error} If the `match` is not an instance of the `typeName`.
*/
expectMatchType : function( match, typeName ) {
if( !( match instanceof Autolinker.match[ typeName ] ) ) {
throw new Error( 'Expected an Autolinker.match.' + typeName + ' match object' );
}
}
};
|