summaryrefslogtreecommitdiff
path: root/t/edit_file.t
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2011-06-07 08:06:16 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2011-06-07 08:06:16 +0000
commit9d4173f2716c2f9a2d26f8f9ab0f47b351b87de7 (patch)
tree98ae3fb82096d8bb96686512ada27fa72126b09a /t/edit_file.t
downloadFile-Slurp-tarball-9d4173f2716c2f9a2d26f8f9ab0f47b351b87de7.tar.gz
Diffstat (limited to 't/edit_file.t')
-rw-r--r--t/edit_file.t107
1 files changed, 107 insertions, 0 deletions
diff --git a/t/edit_file.t b/t/edit_file.t
new file mode 100644
index 0000000..240103a
--- /dev/null
+++ b/t/edit_file.t
@@ -0,0 +1,107 @@
+
+use strict ;
+use warnings ;
+
+use lib qw(t) ;
+
+use File::Slurp qw( :edit read_file write_file ) ;
+use Test::More ;
+
+use TestDriver ;
+
+my $file = 'edit_file_data' ;
+
+my $existing_data = <<PRE ;
+line 1
+line 2
+more
+foo
+bar
+junk here and foo
+last line
+PRE
+
+my $tests = [
+ {
+ name => 'edit_file - no-op',
+ sub => \&edit_file,
+ pretest => sub {
+ my( $test ) = @_ ;
+ write_file( $file, $existing_data ) ;
+ $test->{args} = [
+ sub {},
+ $file
+ ] ;
+ $test->{expected} = $existing_data ;
+ },
+ posttest => sub { $_[0]->{result} = read_file( $file ) },
+ },
+ {
+
+ name => 'edit_file - s/foo/bar/',
+ sub => \&edit_file,
+ pretest => sub {
+ my( $test ) = @_ ;
+ write_file( $file, $existing_data ) ;
+ $test->{args} = [
+ sub { s/foo/bar/g },
+ $file
+ ] ;
+ ( $test->{expected} = $existing_data )
+ =~ s/foo/bar/g ;
+ },
+ posttest => sub { $_[0]->{result} = read_file( $file ) },
+ },
+ {
+
+ name => 'edit_file - upper first words',
+ sub => \&edit_file,
+ pretest => sub {
+ my( $test ) = @_ ;
+ write_file( $file, $existing_data ) ;
+ $test->{args} = [
+ sub { s/^(\w+)/\U$1/gm },
+ $file
+ ] ;
+ ( $test->{expected} = $existing_data )
+ =~ s/^(\w+)/\U$1/gm ;
+ },
+ posttest => sub { $_[0]->{result} = read_file( $file ) },
+ },
+ {
+ name => 'edit_file_lines - no-op',
+ sub => \&edit_file_lines,
+ pretest => sub {
+ my( $test ) = @_ ;
+ write_file( $file, $existing_data ) ;
+ $test->{args} = [
+ sub {},
+ $file
+ ] ;
+ $test->{expected} = $existing_data ;
+ },
+ posttest => sub { $_[0]->{result} = read_file( $file ) },
+ },
+ {
+
+ name => 'edit_file - delete foo lines',
+ sub => \&edit_file_lines,
+ pretest => sub {
+ my( $test ) = @_ ;
+ write_file( $file, $existing_data ) ;
+ $test->{args} = [
+ sub { $_ = '' if /foo/ },
+ $file
+ ] ;
+ ( $test->{expected} = $existing_data )
+ =~ s/^.*foo.*\n//gm ;
+ },
+ posttest => sub { $_[0]->{result} = read_file( $file ) },
+ },
+] ;
+
+test_driver( $tests ) ;
+
+unlink $file ;
+
+exit ;