| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::Build::File::Base; |
| 3 |
|
| 4 |
{ |
| 5 |
package |
| 6 |
FFI::Build::File::Foo; |
| 7 |
use parent qw( FFI::Build::File::Base ); |
| 8 |
use constant default_suffix => '.foo'; |
| 9 |
use constant default_encoding => ':utf8'; |
| 10 |
} |
| 11 |
|
| 12 |
subtest 'basic' => sub { |
| 13 |
|
| 14 |
subtest 'basic usage' => sub { |
| 15 |
|
| 16 |
eval { FFI::Build::File::Foo->new }; |
| 17 |
my $error = $@; |
| 18 |
like $error, qr/content is required/; |
| 19 |
note "error = $error"; |
| 20 |
|
| 21 |
}; |
| 22 |
|
| 23 |
subtest 'array filename' => sub { |
| 24 |
|
| 25 |
my $file = FFI::Build::File::Foo->new(['corpus', 'ffi_build_file_base', 'basic.foo']); |
| 26 |
isa_ok $file, 'FFI::Build::File::Base'; |
| 27 |
isa_ok $file, 'FFI::Build::File::Foo'; |
| 28 |
is("$file", $file->path, "stringifies to path"); |
| 29 |
is($file->slurp, "This is a basic foo.\n"); |
| 30 |
ok(!$file->is_temp, "is_temp"); |
| 31 |
is($file->basename, 'basic.foo', 'basename'); |
| 32 |
ok(-d $file->dirname, 'dirname'); |
| 33 |
note "dirname = @{[ $file->dirname ]}"; |
| 34 |
unlike $file->path, qr/\\/, "No forward slashes!"; |
| 35 |
|
| 36 |
if($^O eq 'MSWin32') |
| 37 |
{ |
| 38 |
is($file->native, "corpus\\ffi_build_file_base\\basic.foo", "native name"); |
| 39 |
} |
| 40 |
else |
| 41 |
{ |
| 42 |
is($file->native, $file->path, "native name"); |
| 43 |
} |
| 44 |
note "native = @{[ $file->native ]}"; |
| 45 |
}; |
| 46 |
|
| 47 |
subtest 'string filename' => sub { |
| 48 |
|
| 49 |
my $file = FFI::Build::File::Foo->new("corpus/ffi_build_file_base/basic.foo"); |
| 50 |
isa_ok $file, 'FFI::Build::File::Base'; |
| 51 |
isa_ok $file, 'FFI::Build::File::Foo'; |
| 52 |
is($file->slurp, "This is a basic foo.\n"); |
| 53 |
ok(!$file->is_temp, "is_temp"); |
| 54 |
unlike $file->path, qr/\\/, "No forward slashes!"; |
| 55 |
|
| 56 |
}; |
| 57 |
|
| 58 |
subtest 'string ref' => sub { |
| 59 |
|
| 60 |
my $file = FFI::Build::File::Foo->new(\"Something different!\n"); |
| 61 |
isa_ok $file, 'FFI::Build::File::Base'; |
| 62 |
isa_ok $file, 'FFI::Build::File::Foo'; |
| 63 |
like($file->basename, qr/\.foo$/, 'has the correct extension'); |
| 64 |
ok($file->is_temp, "is_temp"); |
| 65 |
is($file->slurp, "Something different!\n"); |
| 66 |
note "path: @{[ $file->path ]}"; |
| 67 |
unlike $file->path, qr/\\/, "No forward slashes!"; |
| 68 |
my $path = $file->path; |
| 69 |
ok(-f $path, "file exists"); |
| 70 |
undef $file; |
| 71 |
ok(!-f $path, "file is removed after destroy"); |
| 72 |
|
| 73 |
}; |
| 74 |
|
| 75 |
subtest 'string ref keep' => sub { |
| 76 |
|
| 77 |
my $file = FFI::Build::File::Foo->new(\"Again\n"); |
| 78 |
$file->keep; |
| 79 |
my $path = $file->path; |
| 80 |
is($file->slurp, "Again\n"); |
| 81 |
ok(-f $path, "file exists"); |
| 82 |
unlike $file->path, qr/\\/, "No forward slashes!"; |
| 83 |
undef $file; |
| 84 |
ok(-f $path, "file exists after undef"); |
| 85 |
unlink $path; |
| 86 |
|
| 87 |
}; |
| 88 |
|
| 89 |
}; |
| 90 |
|
| 91 |
done_testing; |