File Coverage

t/file.t
Criterion Covered Total %
statement 81 81 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 90 90 100.0


line stmt bran cond sub pod time code
1 1     1   27853 use utf8;
  1         8  
  1         4  
2 1     1   27 use strict;
  1         1  
  1         23  
3 1     1   3 use warnings;
  1         2  
  1         20  
4 1     1   9 use v5.12;
  1         2  
  1         28  
5              
6 1     1   309 use Test::More tests => 25;
  1         11097  
  1         6  
7              
8 1     1   441 use File::Spec::Functions;
  1         558  
  1         73  
9 1     1   391 use File::Temp qw(tempdir);
  1         11922  
  1         48  
10 1     1   199 use Sweet::Dir;
  1         3  
  1         30  
11              
12 1     1   5 use Sweet::File;
  1         1  
  1         2390  
13              
14 1         118254 my $test_dir = Sweet::Dir->new( path => 't' );
15              
16 1         23 my $file = Sweet::File->new( name => 'file.t', dir => $test_dir );
17 1         3 ok $file->is_a_plain_file, 'is_a_plain_file';
18 1         271 ok $file->is_writable, 'is_writable';
19              
20 1         184 is "$file", catfile( 't', 'file.t' ), 'stringify to path';
21              
22 1         185 is $file->path, catfile( 't', 'file.t' ), 'path';
23 1         179 is $file->extension, 't', 'extension';
24              
25 1         182 my $file_touched = Sweet::File->new( name => 'file_touched', dir => $test_dir );
26 1         3 ok $file_touched->does_not_exists, 'touched file does not exists yet';
27 1         240 is $file_touched->encoding, 'utf8', 'default encoding';
28              
29 1         161 my $file_that_do_not_exists = $test_dir->file('file_that_do_not_exists');
30 1         3 ok $file_that_do_not_exists->does_not_exists, 'file does not exists';
31              
32 1         206 my $empty_file = $test_dir->file('empty_file');
33 1         3 ok $empty_file->has_zero_size, 'empty file has zero size';
34              
35 1         209 my $file1 = Sweet::File->new(
36                 name => 'file1.txt',
37                 dir => $test_dir,
38             );
39 1         2 my @file1_lines = ( 'Hi,', 'I am a text file.' );
40              
41 1         26 is $file1->num_lines, 2, 'num_lines';
42 1         182 my @got_lines = $file1->lines;
43 1         4 is_deeply \@got_lines, \@file1_lines, 'lines';
44 1         344 is $file1->line(0), $file1_lines[0], 'line(0)';
45 1         198 is $file1->line(1), $file1_lines[1], 'line(1)';
46              
47 1         151 my @got_split_line1 = $file1->split_line->('a')->(1);
48 1         6 my @expected_split_line1 = ('I ', 'm ', ' text file.');
49 1         3 is_deeply \@got_split_line1, \@expected_split_line1,'split_line';
50              
51 1         321 my $file_from_path = Sweet::File->new(path=>'t/file.t');
52 1         19 is $file_from_path->name, 'file.t', 'name from path';
53 1         175 is $file_from_path->dir->path, 't', 'dir from path';
54              
55 1         175 my $utf8 = Sweet::File->new(path=>'t/utf8.txt');
56 1         25 is $utf8->line(0), '£¥€$', 'read utf8';
57              
58 1         154 my $temp_path = tempdir();
59 1         318 my $temp_dir = Sweet::Dir->new( path => $temp_path )->create;
60              
61 1         21 my $copied_file = $file->copy_to_dir($temp_dir);
62 1         3 ok $copied_file->is_a_plain_file, 'copy_to_dir';
63              
64 1         189 my $copied_file1 = $file1->copy_to_dir($temp_path);
65 1         3 ok $copied_file1->is_a_plain_file, 'copy_to_dir coerces Str to Sweet::Dir';
66              
67 1         191 my $copied_file2 = $file1->copy_to_dir([$temp_path, 'foo']);
68 1         4 ok $copied_file2->is_a_plain_file, 'copy_to_dir coerces ArrayRef to Sweet::Dir';
69              
70 1         199 my $copied_file3 = $file->copy_to_dir($temp_dir->sub_dir('bar'));
71 1         3 ok $copied_file3->is_a_plain_file, 'copy_to_dir creates target dir';
72              
73 1         217 my $brand_new_file1 = Sweet::File->new(
74                 name => 'brand_new_file1.txt',
75                 dir => $temp_dir,
76                 lines => \@file1_lines,
77             );
78 1         26 my @got_lines_in_brand_new_file1 = $brand_new_file1->lines;
79 1         3 is_deeply \@got_lines_in_brand_new_file1, \@file1_lines, 'lines as constructor argument';
80              
81 1         309 $brand_new_file1->write;
82 1         2 ok $brand_new_file1->is_a_plain_file, 'write';
83              
84 1         217 my @appended_lines = ('first appended line', 'second appended line');
85              
86 1         3 $brand_new_file1->append(\@appended_lines);
87              
88             # Create other instance of brand_new_file1.
89 1         24 my $new_file1 = Sweet::File->new(
90               dir => $brand_new_file1->dir,
91               name => $brand_new_file1->name,
92             );
93              
94 1         26 my @got_lines_in_new_file1 = $new_file1->lines;
95 1         1 my @expected_lines_in_new_file1;
96 1         2 push @expected_lines_in_new_file1, @file1_lines;
97 1         1 push @expected_lines_in_new_file1, @appended_lines;
98 1         3 is_deeply \@got_lines_in_new_file1, \@expected_lines_in_new_file1, 'append';
99              
100 1         319 my $expected_num_lines_in_new_file1 = scalar(@file1_lines) + scalar(@appended_lines);
101 1         30 is $brand_new_file1->num_lines, $expected_num_lines_in_new_file1, 'append updates lines';
102              
103