Tests/Specs: Use Chai for assertions, instead of Jasmine's assertions.
[mootools.git] / Specs / Types / String.js
blobe9af28cafe4317996b2b8d56ce4ad72701c76bd5
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()).to.equal('I Like Cookies');
15                 expect('I Like cOOKIES'.capitalize()).to.equal('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()).to.equal('iLikeCookies');
22                 expect('I-Like-Cookies'.camelCase()).to.equal('ILikeCookies');
23         });
25         // String.hyphenate
27         it('should convert a camel cased string into a hyphenated string', function(){
28                 expect('iLikeCookies'.hyphenate()).to.equal('i-like-cookies');
29                 expect('ILikeCookies'.hyphenate()).to.equal('-i-like-cookies');
30         });
32         // String.clean
34         it('should clean all extraneous whitespace from the string', function(){
35                 expect('  i     like    cookies   '.clean()).to.equal("i like cookies");
36                 expect('  i\nlike \n cookies \n\t  '.clean()).to.equal("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()).to.equal('i like cookies');
43                 expect('  i  \tlike  cookies  '.trim()).to.equal('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')).to.equal(true);
50                 expect('i,like,cookies'.contains('cookies')).to.equal(true);
51                 expect('mootools'.contains('inefficient javascript')).to.equal(false);
52         });
54         it('should return true if the string constains the string and separator otherwise false', function(){
55                 expect('i like cookies'.contains('cookies', ' ')).to.equal(true);
56                 expect('i like cookies'.contains('cookies', ',')).to.equal(false);
58                 expect('i,like,cookies'.contains('cookies', ' ')).to.equal(false);
59                 expect('i,like,cookies'.contains('cookies', ',')).to.equal(true);
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')).to.equal(true);
67                 expect('i like cookies'.test('ke coo')).to.equal(true);
68                 expect('I LIKE COOKIES'.test('cookie', 'i')).to.equal(true);
69                 expect('i like cookies'.test('cookiez')).to.equal(false);
70         });
72         it('should return true if the regular expression test matches the string otherwise false', function(){
73                 expect('i like cookies'.test(/like/)).to.equal(true);
74                 expect('i like cookies'.test(/^l/)).to.equal(false);
75         });
77         // String.toInt
79         it('should convert the string into an integer', function(){
80                 expect('10'.toInt()).to.equal(10);
81                 expect('10px'.toInt()).to.equal(10);
82                 expect('10.10em'.toInt()).to.equal(10);
83         });
85         it('should convert the string into an integer with a specific base', function(){
86                 expect('10'.toInt(5)).to.equal(5);
87         });
89         // String.toFloat
91         it('should convert the string into a float', function(){
92                 expect('10.11'.toFloat()).to.equal(10.11);
93                 expect('10.55px'.toFloat()).to.equal(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()).to.equal('#ffffff');
100                 expect('rgb(255,255,255,0)'.rgbToHex()).to.equal('transparent');
101         });
103         // String.hexToRgb
105         it('should convert the CSS hex string into a CSS rgb string', function(){
106                 expect('#fff'.hexToRgb()).to.equal('rgb(255,255,255)');
107                 expect('ff00'.hexToRgb()).to.equal('rgb(255,0,0)');
108                 expect('#000000'.hexToRgb()).to.equal('rgb(0,0,0)');
109         });
111         // String.substitute
113         it('should substitute values from objects', function(){
114                 expect('This is {color}.'.substitute({'color': 'blue'})).to.equal('This is blue.');
115                 expect('This is {color} and {size}.'.substitute({'color': 'blue', 'size': 'small'})).to.equal('This is blue and small.');
116         });
118         it('should substitute values from arrays', function(){
119                 expect('This is {0}.'.substitute(['blue'])).to.equal('This is blue.');
120                 expect('This is {0} and {1}.'.substitute(['blue', 'small'])).to.equal('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])).to.equal('Checking 1, 0, ,  and .');
125                 expect('This is {not-set}.'.substitute({})).to.equal('This is .');
126         });
128         it('should ignore escaped placeholders', function(){
129                 expect('Ignore \\{this} but not {that}.'.substitute({'that': 'the others'})).to.equal('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)).to.equal('I feel so PHP.');
135                 var ror = (/#\{([^}]+)\}/g);
136                 expect('I feel so #{language}.'.substitute({'language': 'RoR'}, ror)).to.equal('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' })).to.not.equal("fred BROKEN very} cool");
141                 expect('this {should {break} mo} betta'.substitute({ 'break':'work' })).to.equal('this {should work mo} betta');
142         });