tests and changelogging
[gitmo/Moose.git] / t / 070_native_traits / 070_trait_string.t
index f5d4ab7..cef148d 100644 (file)
@@ -9,7 +9,7 @@ use Moose ();
 use Moose::Util::TypeConstraints;
 use NoInlineAttribute;
 use Test::More;
-use Test::Exception;
+use Test::Fatal;
 use Test::Moose;
 
 {
@@ -92,60 +92,53 @@ sub run_tests {
         $obj->_string('a');
         is( $obj->length, 1, 'length returns 1 for new string' );
 
-        throws_ok { $obj->length(42) }
-        qr/Cannot call length with any arguments/,
-            'length throws an error when an argument is passed';
+        like( exception { $obj->length(42) }, qr/Cannot call length with any arguments/, 'length throws an error when an argument is passed' );
 
-        $obj->inc;
+        is( $obj->inc, 'b', 'inc returns new value' );
         is( $obj->_string, 'b', 'a becomes b after inc' );
 
-        throws_ok { $obj->inc(42) }
-        qr/Cannot call inc with any arguments/,
-            'inc throws an error when an argument is passed';
+        like( exception { $obj->inc(42) }, qr/Cannot call inc with any arguments/, 'inc throws an error when an argument is passed' );
 
-        $obj->append('foo');
+        is( $obj->append('foo'), 'bfoo', 'append returns new value' );
         is( $obj->_string, 'bfoo', 'appended to the string' );
 
-        throws_ok { $obj->append( 'foo', 2 ) }
-        qr/Cannot call append with more than 1 argument/,
-            'append throws an error when two arguments are passed';
+        like( exception { $obj->append( 'foo', 2 ) }, qr/Cannot call append with more than 1 argument/, 'append throws an error when two arguments are passed' );
 
         $obj->append_curried;
         is( $obj->_string, 'bfoo!', 'append_curried appended to the string' );
 
-        throws_ok { $obj->append_curried('foo') }
-        qr/Cannot call append with more than 1 argument/,
-            'append_curried throws an error when two arguments are passed';
+        like( exception { $obj->append_curried('foo') }, qr/Cannot call append with more than 1 argument/, 'append_curried throws an error when two arguments are passed' );
 
         $obj->_string("has nl$/");
-        $obj->chomp;
+        is( $obj->chomp, 1, 'chomp returns number of characters removed' );
         is( $obj->_string, 'has nl', 'chomped string' );
 
-        $obj->chomp;
+        is( $obj->chomp, 0, 'chomp returns number of characters removed' );
         is(
             $obj->_string, 'has nl',
             'chomp is a no-op when string has no line ending'
         );
 
-        throws_ok { $obj->chomp(42) }
-        qr/Cannot call chomp with any arguments/,
-            'chomp throws an error when an argument is passed';
+        like( exception { $obj->chomp(42) }, qr/Cannot call chomp with any arguments/, 'chomp throws an error when an argument is passed' );
 
-        $obj->chop;
+        is( $obj->chop, 'l', 'chop returns character removed' );
         is( $obj->_string, 'has n', 'chopped string' );
 
-        throws_ok { $obj->chop(42) }
-        qr/Cannot call chop with any arguments/,
-            'chop throws an error when an argument is passed';
+        like( exception { $obj->chop(42) }, qr/Cannot call chop with any arguments/, 'chop throws an error when an argument is passed' );
 
         $obj->_string('x');
-        $obj->prepend('bar');
+        is( $obj->prepend('bar'), 'barx', 'prepend returns new value' );
         is( $obj->_string, 'barx', 'prepended to string' );
 
         $obj->prepend_curried;
         is( $obj->_string, '-barx', 'prepend_curried prepended to string' );
 
-        $obj->replace( qr/([ao])/, sub { uc($1) } );
+        is(
+            $obj->replace( qr/([ao])/, sub { uc($1) } ),
+            '-bArx',
+            'replace returns new value'
+        );
+
         is(
             $obj->_string, '-bArx',
             'substitution using coderef for replacement'
@@ -157,13 +150,20 @@ sub run_tests {
             'substitution using string as replacement'
         );
 
-        throws_ok { $obj->replace( {}, 'x' ) }
-        qr/The first argument passed to replace must be a string or regexp reference/,
-            'replace throws an error when the first argument is not a string or regexp';
+        $obj->_string('foo');
+        $obj->replace( qr/oo/, q{} );
+
+        is( $obj->_string, 'f',
+            'replace accepts an empty string as second argument' );
+
+        $obj->replace( q{}, 'a' );
+
+        is( $obj->_string, 'af',
+            'replace accepts an empty string as first argument' );
 
-        throws_ok { $obj->replace( qr/x/, {} ) }
-        qr/The second argument passed to replace must be a string or code reference/,
-            'replace throws an error when the first argument is not a string or regexp';
+        like( exception { $obj->replace( {}, 'x' ) }, qr/The first argument passed to replace must be a string or regexp reference/, 'replace throws an error when the first argument is not a string or regexp' );
+
+        like( exception { $obj->replace( qr/x/, {} ) }, qr/The second argument passed to replace must be a string or code reference/, 'replace throws an error when the first argument is not a string or regexp' );
 
         $obj->_string('Moosex');
         $obj->replace_curried;
@@ -176,18 +176,24 @@ sub run_tests {
             'match -barx against /[aq]/ returns matches'
         );
 
+        is_deeply(
+            [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ],
+            'match -barx against /[aq]/ returns matches'
+        );
+
         ok(
             scalar $obj->match('b'),
             'match with string as argument returns true'
         );
 
-        throws_ok { $obj->match }
-        qr/Cannot call match without at least 1 argument/,
-            'match throws an error when no arguments are passed';
+        ok(
+            scalar $obj->match(q{}),
+            'match with empty string as argument returns true'
+        );
 
-        throws_ok { $obj->match( {} ) }
-        qr/The argument passed to match must be a string or regexp reference/,
-            'match throws an error when an invalid argument is passed';
+        like( exception { $obj->match }, qr/Cannot call match without at least 1 argument/, 'match throws an error when no arguments are passed' );
+
+        like( exception { $obj->match( {} ) }, qr/The argument passed to match must be a string or regexp reference/, 'match throws an error when an invalid argument is passed' );
 
         $obj->_string('1234');
         ok( !$obj->match_curried, 'match_curried returns false' );
@@ -198,9 +204,7 @@ sub run_tests {
         $obj->clear;
         is( $obj->_string, q{}, 'clear' );
 
-        throws_ok { $obj->clear(42) }
-        qr/Cannot call clear with any arguments/,
-            'clear throws an error when an argument is passed';
+        like( exception { $obj->clear(42) }, qr/Cannot call clear with any arguments/, 'clear throws an error when an argument is passed' );
 
         $obj->_string('some long string');
         is(
@@ -214,32 +218,33 @@ sub run_tests {
             'substr as getter with two arguments'
         );
 
-        $obj->substr( 1, 3, 'ong' );
+        is(
+            $obj->substr( 1, 3, 'ong' ),
+            'ome',
+            'substr as setter returns replaced string'
+        );
 
         is(
             $obj->_string, 'song long string',
             'substr as setter with three arguments'
         );
 
-        throws_ok { $obj->substr }
-        qr/Cannot call substr without at least 1 argument/,
-            'substr throws an error when no argumemts are passed';
+        $obj->substr( 1, 3, '' );
+
+        is(
+            $obj->_string, 's long string',
+            'substr as setter with three arguments, replacment is empty string'
+        );
+
+        like( exception { $obj->substr }, qr/Cannot call substr without at least 1 argument/, 'substr throws an error when no argumemts are passed' );
 
-        throws_ok { $obj->substr( 1, 2, 3, 4 ) }
-        qr/Cannot call substr with more than 3 arguments/,
-            'substr throws an error when four argumemts are passed';
+        like( exception { $obj->substr( 1, 2, 3, 4 ) }, qr/Cannot call substr with more than 3 arguments/, 'substr throws an error when four argumemts are passed' );
 
-        throws_ok { $obj->substr( {} ) }
-        qr/The first argument passed to substr must be an integer/,
-            'substr throws an error when first argument is not an integer';
+        like( exception { $obj->substr( {} ) }, qr/The first argument passed to substr must be an integer/, 'substr throws an error when first argument is not an integer' );
 
-        throws_ok { $obj->substr( 1, {} ) }
-        qr/The second argument passed to substr must be a positive integer/,
-            'substr throws an error when second argument is not a positive integer';
+        like( exception { $obj->substr( 1, {} ) }, qr/The second argument passed to substr must be an integer/, 'substr throws an error when second argument is not an integer' );
 
-        throws_ok { $obj->substr( 1, 2, {} ) }
-        qr/The third argument passed to substr must be a string/,
-            'substr throws an error when third argument is not a string';
+        like( exception { $obj->substr( 1, 2, {} ) }, qr/The third argument passed to substr must be a string/, 'substr throws an error when third argument is not a string' );
 
         $obj->_string('some long string');