popcap 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +4 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +52 -0
- data/LICENSE +9 -0
- data/README.md +137 -0
- data/lib/pop_cap/audio_file.rb +100 -0
- data/lib/pop_cap/commander.rb +64 -0
- data/lib/pop_cap/converter.rb +40 -0
- data/lib/pop_cap/ffmpeg.rb +130 -0
- data/lib/pop_cap/fileable.rb +134 -0
- data/lib/pop_cap/formatters/bit_rate.rb +29 -0
- data/lib/pop_cap/formatters/date.rb +42 -0
- data/lib/pop_cap/formatters/duration.rb +44 -0
- data/lib/pop_cap/formatters/filesize.rb +69 -0
- data/lib/pop_cap/formatters.rb +33 -0
- data/lib/pop_cap/helper.rb +53 -0
- data/lib/pop_cap/tag_key.rb +32 -0
- data/lib/pop_cap/tag_line.rb +36 -0
- data/lib/pop_cap/tag_struct.rb +45 -0
- data/lib/pop_cap/taggable.rb +100 -0
- data/lib/pop_cap/version.rb +3 -0
- data/lib/popcap.rb +5 -0
- data/popcap.gemspec +27 -0
- data/spec/integration/convert_audio_file_spec.rb +15 -0
- data/spec/integration/read_metatags_spec.rb +12 -0
- data/spec/integration/update_metatags_spec.rb +20 -0
- data/spec/lib/pop_cap/audio_file_spec.rb +72 -0
- data/spec/lib/pop_cap/commander_spec.rb +64 -0
- data/spec/lib/pop_cap/converter_spec.rb +67 -0
- data/spec/lib/pop_cap/ffmpeg_spec.rb +96 -0
- data/spec/lib/pop_cap/fileable_spec.rb +118 -0
- data/spec/lib/pop_cap/formatters/bit_rate_spec.rb +53 -0
- data/spec/lib/pop_cap/formatters/date_spec.rb +74 -0
- data/spec/lib/pop_cap/formatters/duration_spec.rb +64 -0
- data/spec/lib/pop_cap/formatters/filesize_spec.rb +89 -0
- data/spec/lib/pop_cap/formatters_spec.rb +36 -0
- data/spec/lib/pop_cap/helper_spec.rb +42 -0
- data/spec/lib/pop_cap/tag_key_spec.rb +48 -0
- data/spec/lib/pop_cap/tag_line_spec.rb +26 -0
- data/spec/lib/pop_cap/tag_struct_spec.rb +50 -0
- data/spec/lib/pop_cap/taggable_spec.rb +62 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/support/popcap_spec_helper.rb +86 -0
- data/spec/support/reek_spec.rb +8 -0
- data/spec/support/sample.flac +0 -0
- metadata +163 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pop_cap/commander'
|
3
|
+
|
4
|
+
module PopCap
|
5
|
+
describe Commander do
|
6
|
+
let(:commander) { Commander.new(%W{ls},%W{-l},%W{/tmp}) }
|
7
|
+
|
8
|
+
context "#new" do
|
9
|
+
it 'raises an error if no arguments' do
|
10
|
+
expect{Commander.new}.to raise_error(ArgumentError)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'sets @executed to empty' do
|
14
|
+
expect(commander.instance_variable_get('@executed')).to be_empty
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'escapes @command' do
|
18
|
+
expect(commander.instance_variable_get('@command')).
|
19
|
+
to eq([['ls'],['-l'],['/tmp']])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context '#execute' do
|
24
|
+
it 'executes a shell command' do
|
25
|
+
Open3.should_receive(:capture3).with(['ls'],['-l'],['/tmp'])
|
26
|
+
commander.execute
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns itself' do
|
30
|
+
Open3.should_receive(:capture3).with('blah')
|
31
|
+
command = Commander.new('blah')
|
32
|
+
expect(command.execute).to be_a(Commander)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns stdout' do
|
37
|
+
Open3.should_receive(:capture3).with('blah') { ['foo',nil,nil] }
|
38
|
+
command = Commander.new('blah').execute
|
39
|
+
expect(command.stdout).to eq('foo')
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns stderr' do
|
43
|
+
Open3.should_receive(:capture3).with('blah') { [nil,'bar',nil] }
|
44
|
+
command = Commander.new('blah').execute
|
45
|
+
expect(command.stderr).to eq('bar')
|
46
|
+
end
|
47
|
+
|
48
|
+
context '#success' do
|
49
|
+
it 'is true if it succeeds' do
|
50
|
+
succeeded = double('status', success?: true)
|
51
|
+
Open3.should_receive(:capture3).with('blah') { [nil,nil,succeeded] }
|
52
|
+
command = Commander.new('blah').execute
|
53
|
+
expect(command.success?).to be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'is false if it fails' do
|
57
|
+
failed = double('status', success?: false)
|
58
|
+
Open3.should_receive(:capture3).with('blah') { [nil,nil,failed] }
|
59
|
+
command = Commander.new('blah').execute
|
60
|
+
expect(command.success?).to be_false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pop_cap/converter'
|
3
|
+
|
4
|
+
module PopCap
|
5
|
+
describe Converter do
|
6
|
+
class FakeClass
|
7
|
+
include Converter
|
8
|
+
def filepath
|
9
|
+
'path/to/file.flac'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:bitrate) { %W{-ab 192k}}
|
14
|
+
let(:filepath) { 'path/to/file.flac' }
|
15
|
+
let(:fk) { FakeClass.new }
|
16
|
+
let(:input) { %W{ffmpeg -i #{filepath}} }
|
17
|
+
|
18
|
+
context '#command' do
|
19
|
+
let(:output_path) { %W{path/to/file.ogg} }
|
20
|
+
|
21
|
+
it 'builds a command' do
|
22
|
+
expect(fk.convert('ogg')).to eq(input + bitrate + output_path)
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'format' do
|
26
|
+
it 'handles symbol' do
|
27
|
+
expect(fk.convert(:ogg)).to eq(input + bitrate + output_path)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'is case insenstive' do
|
31
|
+
expect(fk.convert('OGG')).to eq(input + bitrate + output_path)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'bitrate' do
|
36
|
+
let(:output_path) { %W{path/to/file.mp3} }
|
37
|
+
|
38
|
+
it 'defaults to 192 kb/s' do
|
39
|
+
expect(fk.convert(:mp3)).to eq(input + %W{-ab 192k} + output_path)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'takes an optional bitrate' do
|
43
|
+
expect(fk.convert(:mp3, 64)).to eq(input + %W{-ab 64k} + output_path)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'handles bitrate as string' do
|
47
|
+
expect(fk.convert(:mp3, '128')).
|
48
|
+
to eq(input + %W{-ab 128k} + output_path)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'm4a' do
|
53
|
+
let(:output_path) { %W{path/to/file.m4a} }
|
54
|
+
|
55
|
+
it 'uses strict mode' do
|
56
|
+
expect(fk.convert(:m4a)).
|
57
|
+
to eq(input + %W{-strict -2} + bitrate + output_path)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'ignores strict mode if not m4a' do
|
61
|
+
expect(fk.convert(:ogg)).
|
62
|
+
to eq(input + bitrate + %W{path/to/file.ogg})
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/popcap_spec_helper'
|
3
|
+
require 'pop_cap/ffmpeg'
|
4
|
+
|
5
|
+
module PopCap
|
6
|
+
describe FFmpeg do
|
7
|
+
before { PopCapSpecHelper.setup }
|
8
|
+
after { PopCapSpecHelper.teardown }
|
9
|
+
|
10
|
+
let(:commander) { double('Commander') }
|
11
|
+
let(:filepath) { File.realpath('spec/support/sample.flac') }
|
12
|
+
let(:ffmpeg) { FFmpeg.new(filepath) }
|
13
|
+
|
14
|
+
it 'returns its filepath' do
|
15
|
+
expect(ffmpeg.filepath).to eq filepath
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'raises error if FFmpeg not installed' do
|
19
|
+
error_message = 'No such file or directory - FFmpeg is not installed.'
|
20
|
+
expect do
|
21
|
+
Open3.stub(:capture3).with('ffmpeg').and_raise(Errno::ENOENT)
|
22
|
+
FFmpeg.new('filepath')
|
23
|
+
end.to raise_error(MissingDependency, error_message)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'includes Fileable' do
|
27
|
+
expect(FFmpeg.included_modules).to include Fileable
|
28
|
+
end
|
29
|
+
|
30
|
+
context '#read_tags' do
|
31
|
+
let(:output) { double('output') }
|
32
|
+
|
33
|
+
it 'sends a read command to Commander' do
|
34
|
+
expect(ffmpeg.read_tags).to eq PopCapSpecHelper.raw_tags
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'encodes invalid byte strings as UTF-8' do
|
38
|
+
Commander.stub_chain(:new, :execute) { output }
|
39
|
+
output.should_receive(:success?) { true }
|
40
|
+
output.should_receive(:stdout) { output }
|
41
|
+
output.stub(:valid_encoding?) { false }
|
42
|
+
output.stub_chain(:encoding, :name) { 'UTF-8' }
|
43
|
+
output.should_receive(:encode!).
|
44
|
+
with('UTF-16', 'UTF-8', undef: :replace, invalid: :replace)
|
45
|
+
output.should_receive(:encode!).with('UTF-8')
|
46
|
+
ffmpeg.read_tags
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'raises error if could not read tags' do
|
50
|
+
expect do
|
51
|
+
failed = double('commander', :success? => false)
|
52
|
+
Commander.stub_chain(:new, :execute) { failed }
|
53
|
+
ffmpeg.read_tags
|
54
|
+
end.
|
55
|
+
to raise_error(FFmpegError, 'Error reading ' + filepath)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context '#update_tags' do
|
60
|
+
let(:new_tags) { {artist: 'UPDATEDARTIST'} }
|
61
|
+
|
62
|
+
it 'updates tags on a temp file & copies temp file to original' do
|
63
|
+
expect(ffmpeg.read_tags).to match /Sample Artist/
|
64
|
+
ffmpeg.update_tags(new_tags)
|
65
|
+
expect(ffmpeg.read_tags).to match /UPDATEDARTIST/
|
66
|
+
expect(ffmpeg.read_tags).not_to match /Sample Artist/
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'reloads read_tags' do
|
70
|
+
ffmpeg.read_tags
|
71
|
+
Commander.stub_chain(:new, :execute).
|
72
|
+
and_return(double('output', success?: true))
|
73
|
+
ffmpeg.stub(:restore)
|
74
|
+
ffmpeg.update_tags({})
|
75
|
+
expect(ffmpeg.instance_variable_get('@stdout')).to be_nil
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'raises error if could not update tags' do
|
79
|
+
expect do
|
80
|
+
failed = double('commander', :success? => false)
|
81
|
+
Commander.stub_chain(:new, :execute) { failed }
|
82
|
+
ffmpeg.update_tags({})
|
83
|
+
end.
|
84
|
+
to raise_error(FFmpegError, 'Error updating ' + filepath)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context '#convert' do
|
89
|
+
it 'converts from input format to specified output format & bitrate' do
|
90
|
+
ffmpeg.convert(:mp3, 64)
|
91
|
+
mp3 = FFmpeg.new(File.realpath('spec/support/sample.mp3'))
|
92
|
+
expect(mp3.read_tags).to match /format_name=mp3/
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pop_cap/fileable'
|
3
|
+
require 'support/popcap_spec_helper'
|
4
|
+
|
5
|
+
module PopCap
|
6
|
+
describe Fileable do
|
7
|
+
class FileClass
|
8
|
+
attr_accessor :filepath
|
9
|
+
|
10
|
+
include Fileable
|
11
|
+
def initialize(filepath)
|
12
|
+
@filepath = filepath
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:filepath) { 'spec/support/sample.flac' }
|
17
|
+
let(:fc) { FileClass.new(filepath) }
|
18
|
+
|
19
|
+
context '#filename' do
|
20
|
+
it 'returns the basename' do
|
21
|
+
expect(fc.filename).to eq 'sample.flac'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context '#backup' do
|
26
|
+
it 'backs up file to directory' do
|
27
|
+
FileUtils.should_receive(:cp).with(filepath, '/usr/sample.flac')
|
28
|
+
fc.backup('/usr')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'backup directory defaults to "/tmp"' do
|
32
|
+
FileUtils.should_receive(:cp).with(filepath, '/tmp/sample.flac')
|
33
|
+
fc.backup
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context '#backup_path' do
|
38
|
+
it 'returns path from backup' do
|
39
|
+
FileUtils.should_receive(:cp).with(filepath, '/tmp/sample.flac')
|
40
|
+
fc.backup('/tmp')
|
41
|
+
expect(fc.backup_path).to eq '/tmp/sample.flac'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'raises an error if not backed up' do
|
45
|
+
expect{fc.backup_path}.to raise_error(PathError)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context '#restore' do
|
50
|
+
it 'takes an optional path' do
|
51
|
+
FileUtils.should_receive(:mv).with('/tmp/sample.flac', filepath)
|
52
|
+
fc.restore('/tmp')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'defaults to the backup path' do
|
56
|
+
FileUtils.should_receive(:cp).with(filepath, '/tmp/sample.flac')
|
57
|
+
fc.backup('/tmp')
|
58
|
+
FileUtils.should_receive(:mv).with('/tmp/sample.flac', filepath)
|
59
|
+
fc.restore
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context '#tmppath' do
|
64
|
+
it 'returns a temporary path' do
|
65
|
+
expect(fc.tmppath).to eq '/tmp/sample.flac'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context '#destroy' do
|
70
|
+
it 'removes a file' do
|
71
|
+
FileUtils.should_receive(:rm_f).with(filepath)
|
72
|
+
fc.destroy
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'blanks out filepath' do
|
76
|
+
FileUtils.should_receive(:rm_f).with(filepath)
|
77
|
+
fc.destroy
|
78
|
+
expect(fc.filepath).to be_nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context '#rename' do
|
83
|
+
it 'removes a file with specified name' do
|
84
|
+
new_name = 'spec/support/example.file'
|
85
|
+
FileUtils.should_receive(:mv).with(filepath, new_name)
|
86
|
+
fc.rename('example.file')
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'updates filepath' do
|
90
|
+
new_name = 'spec/support/example.file'
|
91
|
+
FileUtils.should_receive(:mv).with(filepath, new_name)
|
92
|
+
fc.rename('example.file')
|
93
|
+
expect(fc.filepath).to eq('spec/support/example.file')
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context '#move' do
|
98
|
+
let(:destination) { '/tmp' }
|
99
|
+
|
100
|
+
it 'moves a file to directory' do
|
101
|
+
FileUtils.should_receive(:mv).with(filepath, destination)
|
102
|
+
fc.move('/tmp')
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'updates filepath' do
|
106
|
+
FileUtils.should_receive(:mv).with(filepath, destination)
|
107
|
+
fc.move('/tmp')
|
108
|
+
expect(fc.filepath).to eq('/tmp/sample.flac')
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context '#directory' do
|
113
|
+
it 'returns the directory path for the file' do
|
114
|
+
expect(fc.directory).to eq 'spec/support'
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'pop_cap/formatters/bit_rate'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module PopCap
|
5
|
+
module Formatters
|
6
|
+
describe BitRate do
|
7
|
+
it 'makes bitrate human readable' do
|
8
|
+
br = BitRate.new(128456)
|
9
|
+
expect(br.format).to eq('128 kb/s')
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'handles strings' do
|
13
|
+
br = BitRate.new('128456')
|
14
|
+
expect(br.format).to eq('128 kb/s')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'handles long bitrates' do
|
18
|
+
br = BitRate.new('1284567')
|
19
|
+
expect(br.format).to eq('1284 kb/s')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'handles short bitrates' do
|
23
|
+
br = BitRate.new('64845')
|
24
|
+
expect(br.format).to eq('64 kb/s')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'formats as kilobytes' do
|
28
|
+
br = BitRate.new('64845')
|
29
|
+
expect(br.format).to match(/kb\/s/)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'is nil if empty string' do
|
33
|
+
br = BitRate.new('')
|
34
|
+
expect(br.format).to be_nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'is nil if nil' do
|
38
|
+
br = BitRate.new(nil)
|
39
|
+
expect(br.format).to be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'is nil if not a number' do
|
43
|
+
br = BitRate.new('foo')
|
44
|
+
expect(br.format).to be_nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'is not greater than zero' do
|
48
|
+
br = BitRate.new(0)
|
49
|
+
expect(br.format).to be_nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'pop_cap/formatters/date'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module PopCap
|
5
|
+
module Formatters
|
6
|
+
describe Date do
|
7
|
+
it 'handles dates like "October 5, 1975"' do
|
8
|
+
dm = Date.new('October 5, 1975')
|
9
|
+
expect(dm.format).to eq 1975
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'handles dates like "10-05-1984"' do
|
13
|
+
dm = Date.new('10-05-1984')
|
14
|
+
expect(dm.format).to eq 1984
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'handles dates like "1993/10/05"' do
|
18
|
+
dm = Date.new('1993/10/05')
|
19
|
+
expect(dm.format).to eq 1993
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'handles dates like 2012' do
|
23
|
+
dm = Date.new(2012)
|
24
|
+
expect(dm.format).to eq 2012
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'returns an integer' do
|
28
|
+
dm = Date.new(1953)
|
29
|
+
expect(dm.format).to be_a Integer
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'date range' do
|
33
|
+
it 'takes an optional start date range' do
|
34
|
+
dm = Date.new(1961, {start_date: 1900})
|
35
|
+
expect(dm.start_date).to eq(1900)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'takes an optional end date range' do
|
39
|
+
dm = Date.new(1961, {end_date: 3000})
|
40
|
+
expect(dm.end_date).to eq(3000)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'defaults to 1800 for start date' do
|
44
|
+
dm = Date.new(1961)
|
45
|
+
expect(dm.start_date).to eq(1800)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'defaults to 2100 for end date' do
|
49
|
+
dm = Date.new(1961)
|
50
|
+
expect(dm.end_date).to eq(2100)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'returns nil if beyond date range' do
|
54
|
+
dm = Date.new(1799)
|
55
|
+
expect(dm.format).to be_nil
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'returns nil if under date range' do
|
59
|
+
dm = Date.new(2101)
|
60
|
+
expect(dm.format).to be_nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
it 'returns nil if no match' do
|
64
|
+
dm = Date.new(123)
|
65
|
+
expect(dm.format).to be_nil
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'returns nil if ambiguous' do
|
69
|
+
dm = Date.new(19751005)
|
70
|
+
expect(dm.format).to be_nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'pop_cap/formatters/duration'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module PopCap
|
5
|
+
module Formatters
|
6
|
+
describe Duration do
|
7
|
+
it 'formats duration as HH:MM:SS' do
|
8
|
+
dur = Duration.new('40003')
|
9
|
+
expect(dur.format).to eq('11:06:43')
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'removes leading zeroes for hours' do
|
13
|
+
dur = Duration.new('750')
|
14
|
+
expect(dur.format).to eq('12:30')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'removes leading zeroes for minutes' do
|
18
|
+
dur = Duration.new('420')
|
19
|
+
expect(dur.format).to eq('7:00')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'removes leading zeroes for seconds' do
|
23
|
+
dur = Duration.new('8')
|
24
|
+
expect(dur.format).to eq('8')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'handles integers' do
|
28
|
+
dur = Duration.new(12345)
|
29
|
+
expect(dur.format).to eq('3:25:45')
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'handles floats' do
|
33
|
+
dur = Duration.new(1234.5678)
|
34
|
+
expect(dur.format).to eq('20:34')
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'returns warning if time greater than 24 hours' do
|
38
|
+
dur = Duration.new('86400')
|
39
|
+
expect(dur.format).
|
40
|
+
to eq('Warning: Time is greater than 24 hours.')
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'is nil if invalid number' do
|
44
|
+
dur = Duration.new('string')
|
45
|
+
expect(dur.format).to be_nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'is nil if nil' do
|
49
|
+
dur = Duration.new(nil)
|
50
|
+
expect(dur.format).to be_nil
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'is nil if empty string' do
|
54
|
+
dur = Duration.new('')
|
55
|
+
expect(dur.format).to be_nil
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'is nil if zero' do
|
59
|
+
dur = Duration.new(0)
|
60
|
+
expect(dur.format).to be_nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pop_cap/formatters/filesize'
|
3
|
+
|
4
|
+
module PopCap
|
5
|
+
module Formatters
|
6
|
+
describe Filesize do
|
7
|
+
it 'uses binary conversion' do
|
8
|
+
fs = Filesize.new('1024')
|
9
|
+
expect(fs.format).to eq '1K'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'uses "B" for bytes' do
|
13
|
+
fs = Filesize.new(204)
|
14
|
+
expect(fs.format).to eq '204B'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'uses "K" for kilobytes' do
|
18
|
+
fs = Filesize.new(2048)
|
19
|
+
expect(fs.format).to eq '2K'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'uses "M" for megabytes' do
|
23
|
+
fs = Filesize.new(2048000)
|
24
|
+
expect(fs.format).to eq '2M'
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'uses "G" for gigabytes' do
|
28
|
+
fs = Filesize.new(2148000000)
|
29
|
+
expect(fs.format).to eq '2G'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'uses "T" for terabytes' do
|
33
|
+
fs = Filesize.new(2148000000000)
|
34
|
+
expect(fs.format).to eq '2T'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'returns a warning if larger than 999 terabytes' do
|
38
|
+
fs = Filesize.new(1099500000000000)
|
39
|
+
expect(fs.format).
|
40
|
+
to eq 'Warning: Number is larger than 999 terabytes.'
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'rounds to nearest tenth' do
|
44
|
+
fs = Filesize.new(63587)
|
45
|
+
expect(fs.format).to eq '62.1K'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'drops tenth if it is zero' do
|
49
|
+
fs = Filesize.new(4096)
|
50
|
+
expect(fs.format).to eq '4K'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'handles strings' do
|
54
|
+
fs = Filesize.new('123456')
|
55
|
+
expect(fs.format).to eq '120.6K'
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'handles integers' do
|
59
|
+
fs = Filesize.new(123456)
|
60
|
+
expect(fs.format).to eq '120.6K'
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'handles floats' do
|
64
|
+
fs = Filesize.new(1234.56)
|
65
|
+
expect(fs.format).to eq '1.2K'
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'returns nil for nil' do
|
69
|
+
fs = Filesize.new(nil)
|
70
|
+
expect(fs.format).to be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'returns nil for empty string' do
|
74
|
+
fs = Filesize.new('')
|
75
|
+
expect(fs.format).to be_nil
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'returns nil if not a number' do
|
79
|
+
fs = Filesize.new('hello')
|
80
|
+
expect(fs.format).to be_nil
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'returns nil if zero' do
|
84
|
+
fs = Filesize.new(0)
|
85
|
+
expect(fs.format).to be_nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pop_cap/formatters'
|
3
|
+
|
4
|
+
module PopCap
|
5
|
+
describe Formatters do
|
6
|
+
|
7
|
+
let(:formatter_files) { Dir['lib/pop_cap/formatters/*.rb'] }
|
8
|
+
|
9
|
+
it 'requires all formatters in "formatter/"' do
|
10
|
+
loaded_files = $LOADED_FEATURES
|
11
|
+
formatter_files.each do |path|
|
12
|
+
expect(loaded_files).to include(File.realpath(path))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "#included_formatters" do
|
17
|
+
it 'builds a hash of included formatters' do
|
18
|
+
expect(::INCLUDED_FORMATTERS).to be_a Hash
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'has formatter names as keys' do
|
22
|
+
formatter_files.each do |file|
|
23
|
+
formatter_key = File.basename(file, '.rb').to_sym
|
24
|
+
expect(::INCLUDED_FORMATTERS.keys).to include(formatter_key)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'has the formatter path as the value' do
|
29
|
+
formatter_files.each do |file|
|
30
|
+
value = file.sub(%r(^lib\/),'').sub(%r(\.rb$),'')
|
31
|
+
expect(::INCLUDED_FORMATTERS.values).to include(value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|