summaryrefslogtreecommitdiff
path: root/fml/lib/IO/t
diff options
context:
space:
mode:
authorfukachan <fukachan>2001-03-11 12:02:35 +0000
committerfukachan <fukachan>2001-03-11 12:02:35 +0000
commitebfe131226689bf1ab87f7d09dae111104ac2eb3 (patch)
treebf8f1c7d989cdcb53b943f5d319b9b1e6be52760 /fml/lib/IO/t
parentc8785198f4d27a3f83ba1c5c2cbcdcfe9c4e33bc (diff)
downloadfml8-ebfe131226689bf1ab87f7d09dae111104ac2eb3.tar.gz
fml8-ebfe131226689bf1ab87f7d09dae111104ac2eb3.tar.bz2
fml8-ebfe131226689bf1ab87f7d09dae111104ac2eb3.zip
replace() method and the checker
Diffstat (limited to 'fml/lib/IO/t')
-rwxr-xr-xfml/lib/IO/t/replace.pl75
1 files changed, 75 insertions, 0 deletions
diff --git a/fml/lib/IO/t/replace.pl b/fml/lib/IO/t/replace.pl
new file mode 100755
index 00000000..111abb57
--- /dev/null
+++ b/fml/lib/IO/t/replace.pl
@@ -0,0 +1,75 @@
+#-*- perl -*-
+#
+# Copyright (C) 2001 Ken'ichi Fukamachi
+# All rights reserved. This program is free software; you can
+# redistribute it and/or modify it under the same terms as Perl itself.
+#
+# $Id$
+# $FML$
+#
+
+use strict;
+use Carp;
+
+my $org_file = "/etc/passwd";
+my $file = "/tmp/passwd";
+my $tmpf = "/tmp/passwd.tmp";
+my $map = "file:". $file;
+my $regexp = '^root';
+my $value = '@root';
+
+### MAIN ###
+print "${map}->replace() ";
+
+# prepare
+system "sed 1d $org_file > $tmpf";
+system "cp $org_file /tmp/";
+
+# append
+use IO::MapAdapter;
+my $obj = new IO::MapAdapter $map;
+$obj->replace( $regexp, $value ) || croak("cannot add to $map");
+if ($obj->error) { croak( $obj->error );}
+
+# verify the result
+# assemble the original from the replaced line and modified file itself.
+my $newbuf = GetContent($file);
+my $expbuf = $value . "\n". GetContent($tmpf);
+
+if ($expbuf eq $newbuf) {
+ print " ... ok\n";
+}
+else {
+ print " ... fail\n";
+ system "diff -ub $org_file $file";
+}
+
+
+$map = 'unix.group:fml';
+print "${map}->replace() ... ";
+$obj = new IO::MapAdapter $map;
+eval q{ $obj->replace( $regexp, $value ); };
+if ($@) {
+ print "ok\n"; # XXX fail (non null $@) is ok here.
+}
+else {
+ print "fail" unless $@;
+ print "<", $obj->error, ">" if $obj->error;
+ print "\n";
+}
+
+exit 0;
+
+
+sub GetContent
+{
+ my ($file) = @_;
+ my $buf;
+
+ use FileHandle;
+ my $fh = new FileHandle $file;
+ while (<$fh>) { $buf .= $_;}
+ close($fh);
+
+ $buf;
+}