new Test.Unit.Runner({
testInterpret: function(){
this.assertIdentical('true', String.interpret(true));
this.assertIdentical('123', String.interpret(123));
this.assertIdentical('foo bar', String.interpret('foo bar'));
this.assertIdentical(
'object string',
String.interpret({ toString: function(){ return 'object string' } }));
this.assertIdentical('0', String.interpret(0));
this.assertIdentical('false', String.interpret(false));
this.assertIdentical('', String.interpret(undefined));
this.assertIdentical('', String.interpret(null));
this.assertIdentical('', String.interpret(''));
},
testGsubWithReplacementFunction: function() {
var source = 'foo boo boz';
this.assertEqual('Foo Boo BoZ',
source.gsub(/[^o]+/, function(match) {
return match[0].toUpperCase()
}));
this.assertEqual('f2 b2 b1z',
source.gsub(/o+/, function(match) {
return match[0].length;
}));
this.assertEqual('f0 b0 b1z',
source.gsub(/o+/, function(match) {
return match[0].length % 2;
}));
},
testGsubWithReplacementString: function() {
var source = 'foo boo boz';
this.assertEqual('foobooboz',
source.gsub(/\s+/, ''));
this.assertEqual(' z',
source.gsub(/(.)(o+)/, ''));
this.assertEqual('ウィメンズ2007
クルーズコレクション',
'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'
'));
this.assertEqual('ウィメンズ2007
クルーズコレクション',
'ウィメンズ2007\nクルーズコレクション'.gsub('\n','
'));
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
source.gsub('', 'bar'));
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
source.gsub(new RegExp(''), 'bar'));
},
testGsubWithReplacementTemplateString: function() {
var source = 'foo boo boz';
this.assertEqual('-oo-#{1}- -oo-#{1}- -o-#{1}-z',
source.gsub(/(.)(o+)/, '-#{2}-\\#{1}-'));
this.assertEqual('-foo-f- -boo-b- -bo-b-z',
source.gsub(/(.)(o+)/, '-#{0}-#{1}-'));
this.assertEqual('-oo-f- -oo-b- -o-b-z',
source.gsub(/(.)(o+)/, '-#{2}-#{1}-'));
this.assertEqual(' z',
source.gsub(/(.)(o+)/, '#{3}'));
},
testGsubWithTroublesomeCharacters: function() {
this.assertEqual('ab', 'a|b'.gsub('|', ''));
//'ab'.gsub('', ''); // freeze
this.assertEqual('ab', 'ab(?:)'.gsub('(?:)', ''));
this.assertEqual('ab', 'ab()'.gsub('()', ''));
this.assertEqual('ab', 'ab'.gsub('^', ''));
this.assertEqual('ab', 'a?b'.gsub('?', ''))
this.assertEqual('ab', 'a+b'.gsub('+', ''));
this.assertEqual('ab', 'a*b'.gsub('*', ''));
this.assertEqual('ab', 'a{1}b'.gsub('{1}', ''));
this.assertEqual('ab', 'a.b'.gsub('.', ''));
},
testSubWithReplacementFunction: function() {
var source = 'foo boo boz';
this.assertEqual('Foo boo boz',
source.sub(/[^o]+/, function(match) {
return match[0].toUpperCase()
}), 1);
this.assertEqual('Foo Boo boz',
source.sub(/[^o]+/, function(match) {
return match[0].toUpperCase()
}, 2), 2);
this.assertEqual(source,
source.sub(/[^o]+/, function(match) {
return match[0].toUpperCase()
}, 0), 0);
this.assertEqual(source,
source.sub(/[^o]+/, function(match) {
return match[0].toUpperCase()
}, -1), -1);
},
testSubWithReplacementString: function() {
var source = 'foo boo boz';
this.assertEqual('oo boo boz',
source.sub(/[^o]+/, ''));
this.assertEqual('oooo boz',
source.sub(/[^o]+/, '', 2));
this.assertEqual('-f-oo boo boz',
source.sub(/[^o]+/, '-#{0}-'));
this.assertEqual('-f-oo- b-oo boz',
source.sub(/[^o]+/, '-#{0}-', 2));
},
testScan: function() {
var source = 'foo boo boz', results = [];
var str = source.scan(/[o]+/, function(match) {
results.push(match[0].length);
});
this.assertEnumEqual([2, 2, 1], results);
this.assertEqual(source, source.scan(/x/, this.fail));
this.assert(typeof str == 'string');
},
testToArray: function() {
this.assertEnumEqual([],''.toArray());
this.assertEnumEqual(['a'],'a'.toArray());
this.assertEnumEqual(['a','b'],'ab'.toArray());
this.assertEnumEqual(['f','o','o'],'foo'.toArray());
},
/*
Note that camelize() differs from its Rails counterpart,
as it is optimized for dealing with JavaScript object
properties in conjunction with CSS property names:
- Looks for dashes, not underscores
- CamelCases first word if there is a front dash
*/
testCamelize: function() {
this.assertEqual('', ''.camelize());
this.assertEqual('', '-'.camelize());
this.assertEqual('foo', 'foo'.camelize());
this.assertEqual('foo_bar', 'foo_bar'.camelize());
this.assertEqual('FooBar', '-foo-bar'.camelize());
this.assertEqual('FooBar', 'FooBar'.camelize());
this.assertEqual('fooBar', 'foo-bar'.camelize());
this.assertEqual('borderBottomWidth', 'border-bottom-width'.camelize());
this.assertEqual('classNameTest','class-name-test'.camelize());
this.assertEqual('classNameTest','className-test'.camelize());
this.assertEqual('classNameTest','class-nameTest'.camelize());
/* this.benchmark(function(){
'class-name-test'.camelize();
},10000); */
},
testCapitalize: function() {
this.assertEqual('',''.capitalize());
this.assertEqual('Ä','ä'.capitalize());
this.assertEqual('A','A'.capitalize());
this.assertEqual('Hello','hello'.capitalize());
this.assertEqual('Hello','HELLO'.capitalize());
this.assertEqual('Hello','Hello'.capitalize());
this.assertEqual('Hello world','hello WORLD'.capitalize());
},
testUnderscore: function() {
this.assertEqual('', ''.underscore());
this.assertEqual('_', '-'.underscore());
this.assertEqual('foo', 'foo'.underscore());
this.assertEqual('foo', 'Foo'.underscore());
this.assertEqual('foo_bar', 'foo_bar'.underscore());
this.assertEqual('border_bottom', 'borderBottom'.underscore());
this.assertEqual('border_bottom_width', 'borderBottomWidth'.underscore());
this.assertEqual('border_bottom_width', 'border-Bottom-Width'.underscore());
},
testDasherize: function() {
this.assertEqual('', ''.dasherize());
this.assertEqual('foo', 'foo'.dasherize());
this.assertEqual('Foo', 'Foo'.dasherize());
this.assertEqual('foo-bar', 'foo-bar'.dasherize());
this.assertEqual('border-bottom-width', 'border_bottom_width'.dasherize());
},
testTruncate: function() {
var source = 'foo boo boz foo boo boz foo boo boz foo boo boz';
this.assertEqual(source, source.truncate(source.length));
this.assertEqual('foo boo boz foo boo boz foo...', source.truncate(0));
this.assertEqual('fo...', source.truncate(5));
this.assertEqual('foo b', source.truncate(5, ''));
this.assert(typeof 'foo'.truncate(5) == 'string');
this.assert(typeof 'foo bar baz'.truncate(5) == 'string');
},
testStrip: function() {
this.assertEqual('hello world', ' hello world '.strip());
this.assertEqual('hello world', 'hello world'.strip());
this.assertEqual('hello \n world', ' hello \n world '.strip());
this.assertEqual('', ' '.strip());
},
testStripTags: function() {
this.assertEqual('hello world', 'hello world'.stripTags());
this.assertEqual('hello world', 'hello world'.stripTags());
this.assertEqual('hello world', 'hello world'.stripTags());
this.assertEqual('hello world', 'hello world'.stripTags());
this.assertEqual('1\n2', '1\n2'.stripTags());
},
testStripScripts: function() {
this.assertEqual('foo bar', 'foo bar'.stripScripts());
this.assertEqual('foo bar', ('foo