Concat sinon files, add them under Tests/Utilities
[mootools.git] / Specs / Types / String.js
bloba2c3096e62650131baea6e1526dd367175817c3b
1 /*
2 ---
3 name: String
4 requires: ~
5 provides: ~
6 ...
7 */
9 describe("String Methods", function(){
11         // String.capitalize
13         it('should capitalize each word', function(){
14                 expect('i like cookies'.capitalize()).toEqual('I Like Cookies');
15                 expect('I Like cOOKIES'.capitalize()).toEqual('I Like COOKIES');
16         });
18         // String.camelCase
20         it('should convert a hyphenated string into a camel cased string', function(){
21                 expect('i-like-cookies'.camelCase()).toEqual('iLikeCookies');
22                 expect('I-Like-Cookies'.camelCase()).toEqual('ILikeCookies');
23         });
25         // String.hyphenate
27         it('should convert a camel cased string into a hyphenated string', function(){
28                 expect('iLikeCookies'.hyphenate()).toEqual('i-like-cookies');
29                 expect('ILikeCookies'.hyphenate()).toEqual('-i-like-cookies');
30         });
32         // String.clean
34         it('should clean all extraneous whitespace from the string', function(){
35                 expect('  i     like    cookies   '.clean()).toEqual("i like cookies");
36                 expect('  i\nlike \n cookies \n\t  '.clean()).toEqual("i like cookies");
37         });
39         // String.trim
41         it('should trim left and right whitespace from the string', function(){
42                 expect('  i like cookies  '.trim()).toEqual('i like cookies');
43                 expect('  i  \tlike  cookies  '.trim()).toEqual('i  \tlike  cookies');
44         });
46         //<1.4compat>
47         // String.contains
48         it('should return true if the string contains a string otherwise false', function(){
49                 expect('i like cookies'.contains('cookies')).toBeTruthy();
50                 expect('i,like,cookies'.contains('cookies')).toBeTruthy();
51                 expect('mootools'.contains('inefficient javascript')).toBeFalsy();
52         });
54         it('should return true if the string constains the string and separator otherwise false', function(){
55                 expect('i like cookies'.contains('cookies', ' ')).toBeTruthy();
56                 expect('i like cookies'.contains('cookies', ',')).toBeFalsy();
58                 expect('i,like,cookies'.contains('cookies', ' ')).toBeFalsy();
59                 expect('i,like,cookies'.contains('cookies', ',')).toBeTruthy();
60         });
61         //</1.4compat>
63         // String.test
65         it('should return true if the test matches the string otherwise false', function(){
66                 expect('i like teh cookies'.test('cookies')).toBeTruthy();
67                 expect('i like cookies'.test('ke coo')).toBeTruthy();
68                 expect('I LIKE COOKIES'.test('cookie', 'i')).toBeTruthy();
69                 expect('i like cookies'.test('cookiez')).toBeFalsy();
70         });
72         it('should return true if the regular expression test matches the string otherwise false', function(){
73                 expect('i like cookies'.test(/like/)).toBeTruthy();
74                 expect('i like cookies'.test(/^l/)).toBeFalsy();
75         });
77         // String.toInt
79         it('should convert the string into an integer', function(){
80                 expect('10'.toInt()).toEqual(10);
81                 expect('10px'.toInt()).toEqual(10);
82                 expect('10.10em'.toInt()).toEqual(10);
83         });
85         it('should convert the string into an integer with a specific base', function(){
86                 expect('10'.toInt(5)).toEqual(5);
87         });
89         // String.toFloat
91         it('should convert the string into a float', function(){
92                 expect('10.11'.toFloat()).toEqual(10.11);
93                 expect('10.55px'.toFloat()).toEqual(10.55);
94         });
96         // String.rgbToHex
98         it('should convert the string into a CSS hex string', function(){
99                 expect('rgb(255,255,255)'.rgbToHex()).toEqual('#ffffff');
100                 expect('rgb(255,255,255,0)'.rgbToHex()).toEqual('transparent');
101         });
103         // String.hexToRgb
105         it('should convert the CSS hex string into a CSS rgb string', function(){
106                 expect('#fff'.hexToRgb()).toEqual('rgb(255,255,255)');
107                 expect('ff00'.hexToRgb()).toEqual('rgb(255,0,0)');
108                 expect('#000000'.hexToRgb()).toEqual('rgb(0,0,0)');
109         });
111         // String.substitute
113         it('should substitute values from objects', function(){
114                 expect('This is {color}.'.substitute({'color': 'blue'})).toEqual('This is blue.');
115                 expect('This is {color} and {size}.'.substitute({'color': 'blue', 'size': 'small'})).toEqual('This is blue and small.');
116         });
118         it('should substitute values from arrays', function(){
119                 expect('This is {0}.'.substitute(['blue'])).toEqual('This is blue.');
120                 expect('This is {0} and {1}.'.substitute(['blue', 'small'])).toEqual('This is blue and small.');
121         });
123         it('should remove undefined values', function(){
124                 expect('Checking {0}, {1}, {2}, {3} and {4}.'.substitute([1, 0, undefined, null])).toEqual('Checking 1, 0, ,  and .');
125                 expect('This is {not-set}.'.substitute({})).toEqual('This is .');
126         });
128         it('should ignore escaped placeholders', function(){
129                 expect('Ignore \\{this} but not {that}.'.substitute({'that': 'the others'})).toEqual('Ignore {this} but not the others.');
130         });
132         it('should substitute with a custom regex', function(){
133                 var php = (/\$([\w-]+)/g);
134                 expect('I feel so $language.'.substitute({'language': 'PHP'}, php)).toEqual('I feel so PHP.');
135                 var ror = (/#\{([^}]+)\}/g);
136                 expect('I feel so #{language}.'.substitute({'language': 'RoR'}, ror)).toEqual('I feel so RoR.');
137         });
139         it('should substitute without goofing up nested curly braces', function(){
140                 expect("fred {is {not} very} cool".substitute({ 'is {not':'BROKEN' })).not.toEqual("fred BROKEN very} cool");
141                 expect('this {should {break} mo} betta'.substitute({ 'break':'work' })).toEqual('this {should work mo} betta');
142         });