File: asset.t

package info (click to toggle)
libmojolicious-perl 2.98%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,968 kB
  • sloc: perl: 10,178; sh: 48; makefile: 8
file content (165 lines) | stat: -rw-r--r-- 5,738 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use Mojo::Base -strict;

use Test::More tests => 63;

# "And now, in the spirit of the season: start shopping.
#  And for every dollar of Krusty merchandise you buy,
#  I will be nice to a sick kid.
#  For legal purposes, sick kids may include hookers with a cold."
use Mojo::Asset::File;
use Mojo::Asset::Memory;

# File asset
my $file = Mojo::Asset::File->new;
$file->add_chunk('abc');
is $file->contains(''),    0,  'empty string at position 0';
is $file->contains('abc'), 0,  '"abc" at position 0';
is $file->contains('bc'),  1,  '"bc" at position 1';
is $file->contains('db'),  -1, 'does not contain "db"';

# Cleanup
my $path = $file->path;
ok -e $path, 'temporary file exists';
undef $file;
ok !-e $path, 'temporary file has been cleaned up';

# Memory asset
my $mem = Mojo::Asset::Memory->new;
$mem->add_chunk('abc');
is $mem->contains(''),    0,  'empty string at position 0';
is $mem->contains('abc'), 0,  '"abc" at position 0';
is $mem->contains('bc'),  1,  '"bc" at position 1';
is $mem->contains('db'),  -1, 'does not contain "db"';

# Empty file asset
$file = Mojo::Asset::File->new;
is $file->contains(''), 0, 'empty string at position 0';

# Empty memory asset
$mem = Mojo::Asset::File->new;
is $mem->contains(''), 0, 'empty string at position 0';

# File asset range support (a[bcdef])
$file = Mojo::Asset::File->new(start_range => 1);
$file->add_chunk('abcdef');
is $file->contains(''),      0,  'empty string at position 0';
is $file->contains('bcdef'), 0,  '"bcdef" at position 0';
is $file->contains('cdef'),  1,  '"cdef" at position 1';
is $file->contains('db'),    -1, 'does not contain "db"';

# Memory asset range support (a[bcdef])
$mem = Mojo::Asset::Memory->new(start_range => 1);
$mem->add_chunk('abcdef');
is $mem->contains(''),      0,  'empty string at position 0';
is $mem->contains('bcdef'), 0,  '"bcdef" at position 0';
is $mem->contains('cdef'),  1,  '"cdef" at position 1';
is $mem->contains('db'),    -1, 'does not contain "db"';

# File asset range support (ab[cdefghi]jk)
{
  local $ENV{MOJO_CHUNK_SIZE} = 1024;
  $file = Mojo::Asset::File->new(start_range => 2, end_range => 8);
  $file->add_chunk('abcdefghijk');
  is $file->contains(''),        0,  'empty string at position 0';
  is $file->contains('cdefghi'), 0,  '"cdefghi" at position 0';
  is $file->contains('fghi'),    3,  '"fghi" at position 3';
  is $file->contains('f'),       3,  '"f" at position 3';
  is $file->contains('hi'),      5,  '"hi" at position 5';
  is $file->contains('db'),      -1, 'does not contain "db"';
  my $chunk = $file->get_chunk(0);
  is $chunk, 'cdefghi', 'chunk from position 0';
  $chunk = $file->get_chunk(1);
  is $chunk, 'defghi', 'chunk from position 1';
  $chunk = $file->get_chunk(5);
  is $chunk, 'hi', 'chunk from position 5';
  $ENV{MOJO_CHUNK_SIZE} = 1;
  $chunk = $file->get_chunk(0);
  is $chunk, 'c', 'chunk from position 0 with size 1';
  $chunk = $file->get_chunk(5);
  is $chunk, 'h', 'chunk from position 5 with size 1';
}

# Memory asset range support (ab[cdefghi]jk)
{
  local $ENV{MOJO_CHUNK_SIZE} = 1024;
  $mem = Mojo::Asset::Memory->new(start_range => 2, end_range => 8);
  $mem->add_chunk('abcdefghijk');
  is $mem->contains(''),        0,  'empty string at position 0';
  is $mem->contains('cdefghi'), 0,  '"cdefghi" at position 0';
  is $mem->contains('fghi'),    3,  '"fghi" at position 3';
  is $mem->contains('f'),       3,  '"f" at position 3';
  is $mem->contains('hi'),      5,  '"hi" at position 5';
  is $mem->contains('db'),      -1, 'does not contain "db"';
  my $chunk = $mem->get_chunk(0);
  is $chunk, 'cdefghi', 'chunk from position 0';
  $chunk = $mem->get_chunk(1);
  is $chunk, 'defghi', 'chunk from position 1';
  $chunk = $mem->get_chunk(5);
  is $chunk, 'hi', 'chunk from position 5';
  $ENV{MOJO_CHUNK_SIZE} = 1;
  $chunk = $mem->get_chunk(0);
  is $chunk, 'c', 'chunk from position 0 with size 1';
  $chunk = $mem->get_chunk(5);
  is $chunk, 'h', 'chunk from position 5 with size 1';
}

# Move memory asset to file
$mem = Mojo::Asset::Memory->new;
$mem->add_chunk('abc');
my $tmp = Mojo::Asset::File->new;
$tmp->add_chunk('x');
$path = $tmp->path;
ok -e $path, 'file exists';
undef $tmp;
ok !-e $path, 'file has been cleaned up';
$mem->move_to($path);
is $mem->slurp, 'abc', 'right content';
ok -e $path, 'file exists';
unlink $path;
ok !-e $path, 'file has been cleaned up';

# Move file asset to file
$file = Mojo::Asset::File->new;
$file->add_chunk('bcd');
$tmp = Mojo::Asset::File->new;
$tmp->add_chunk('x');
$path = $tmp->path;
ok -e $path, 'file exists';
undef $tmp;
ok !-e $path, 'file has been cleaned up';
$file->move_to($path);
is $file->slurp, 'bcd', 'right content';
undef $file;
ok -e $path, 'file exists';
unlink $path;
ok !-e $path, 'file has been cleaned up';

# Upgrade
my $asset = Mojo::Asset::Memory->new(max_memory_size => 5, auto_upgrade => 1);
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';
$asset = $asset->add_chunk('lala');
ok $asset->is_file, 'stored in file';
$asset = Mojo::Asset::Memory->new(max_memory_size => 5);
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';

# Temporary directory
{
  local $ENV{MOJO_TMPDIR} = '/does/not/exist';
  is(Mojo::Asset::File->new->tmpdir, '/does/not/exist', 'right value');
}

# Temporary file without cleanup
$file = Mojo::Asset::File->new(cleanup => 0)->add_chunk('test');
ok $file->is_file, 'stored in file';
is $file->slurp,   'test', 'right content';
is $file->size,    4, 'right size';
is $file->contains('es'), 1, '"es" at position 1';
$path = $file->path;
undef $file;
ok -e $path, 'file exists';
unlink $path;
ok !-e $path, 'file has been cleaned up';