File: stringify.t

package info (click to toggle)
libfile-slurp-perl 9999.32-2.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 412 kB
  • sloc: perl: 650; makefile: 39
file content (42 lines) | stat: -rw-r--r-- 953 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use strict;
use warnings;

use File::Spec ();
use File::Slurp;
use File::Temp qw(tempfile);
use IO::Handle ();
use Test::More;

# older EUMMs turn this on. We don't want to emit warnings.
# also, some of our CORE function overrides emit warnings. Silence those.
local $^W;

# this code creates the object which has a stringified path
{
    package FileObject;
    use Exporter qw(import);
    use overload
        q[""] => \&stringify,
        fallback => 1;

    sub new { bless { path => $_[1] }, $_[0] }

    sub stringify { $_[0]->{path} }
}

plan tests => 3 ;

my (undef, $path) = tempfile('tempXXXXX', DIR => File::Spec->tmpdir, OPEN => 0);
my $data = "random junk\n";

# create an object with an overloaded path
my $obj = FileObject->new($path);

isa_ok($obj, 'FileObject');
is("$obj", $path, "object stringifies to path");

write_file($obj, $data);
my $read = read_file($obj);
is($data, $read, 'read_file of stringified object');

unlink $path;