ptools 1.3.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ #####################################################################
2
+ # test_binary.rb
3
+ #
4
+ # Test case for the File.binary? method. You should run this test
5
+ # via the 'rake test_binary' task.
6
+ #####################################################################
7
+ require 'rubygems'
8
+ require 'rspec'
9
+ require 'ptools'
10
+
11
+ RSpec.describe File, :binary do
12
+ let(:dirname) { File.dirname(__FILE__) }
13
+ let(:bin_file) { File::ALT_SEPARATOR ? File.join(ENV['windir'], 'notepad.exe') : '/bin/ls' }
14
+
15
+ before do
16
+ @txt_file = File.join(dirname, 'txt', 'english.txt')
17
+ @emp_file = File.join(dirname, 'txt', 'empty.txt')
18
+ @uni_file = File.join(dirname, 'txt', 'korean.txt')
19
+ @utf_file = File.join(dirname, 'txt', 'english.utf16')
20
+ @png_file = File.join(dirname, 'img', 'test.png')
21
+ @jpg_file = File.join(dirname, 'img', 'test.jpg')
22
+ @gif_file = File.join(dirname, 'img', 'test.gif')
23
+ end
24
+
25
+ example "File.binary? basic functionality" do
26
+ expect(File).to respond_to(:binary?)
27
+ expect{ File.binary?(@txt_file) }.not_to raise_error
28
+ end
29
+
30
+ example "File.binary? returns true for binary files" do
31
+ expect(File.binary?(bin_file)).to be true
32
+ end
33
+
34
+ example "File.binary? returns false for text files" do
35
+ expect(File.binary?(@emp_file)).to be false
36
+ expect(File.binary?(@txt_file)).to be false
37
+ expect(File.binary?(@uni_file)).to be false
38
+ expect(File.binary?(@utf_file)).to be false
39
+ end
40
+
41
+ example "File.binary? returns false for image files" do
42
+ expect(File.binary?(@png_file)).to be false
43
+ expect(File.binary?(@jpg_file)).to be false
44
+ expect(File.binary?(@gif_file)).to be false
45
+ end
46
+
47
+ example "File.binary? accepts an optional percentage argument" do
48
+ expect(File.binary?(@txt_file, 0.50)).to be false
49
+ expect(File.binary?(@txt_file, 0.05)).to be true
50
+ end
51
+
52
+ example "File.binary? raises an error if the file cannot be found" do
53
+ expect{ File.binary?('bogus') }.to raise_error(SystemCallError)
54
+ end
55
+
56
+ example "File.binary? only accepts one argument" do
57
+ expect{ File.binary?(@txt_file, bin_file) }.to raise_error(ArgumentError)
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ ##############################################################################
2
+ # constants_spec.rb
3
+ #
4
+ # Specs for the constants that have been defined in the ptools library.
5
+ # This test case should be run via the 'rake spec:constants' task.
6
+ ##############################################################################
7
+ require 'rubygems'
8
+ require 'rspec'
9
+ require 'rbconfig'
10
+ require 'ptools'
11
+
12
+ RSpec.describe File, :constants do
13
+ let(:windows) { File::ALT_SEPARATOR }
14
+
15
+ example "PTOOLS_VERSION constant is set to expected value" do
16
+ expect(File::PTOOLS_VERSION).to eq('1.4.0')
17
+ expect(File::PTOOLS_VERSION.frozen?).to be true
18
+ end
19
+
20
+ example "IMAGE_EXT constant is set to array of values" do
21
+ expect(File::IMAGE_EXT.sort).to eq(%w[.bmp .gif .jpeg .jpg .png])
22
+ end
23
+
24
+ example "WINDOWS constant is defined on MS Windows" do
25
+ skip "skipped unless MS Windows" unless windows
26
+ expect(File::MSWINDOWS).not_to be_nil
27
+ end
28
+
29
+ example "WIN32EXTS constant is defined on MS Windows" do
30
+ skip "skipped unless MS Windows" unless windows
31
+ expect(File::WIN32EXTS).not_to be_nil
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ ######################################################################
2
+ # head_spec.rb
3
+ #
4
+ # Specs for the File.head method. These specs should be run via
5
+ # the 'rake spec:head' task.
6
+ ######################################################################
7
+ require 'rspec'
8
+ require 'ptools'
9
+
10
+ RSpec.describe File, :head do
11
+ let(:test_file) { 'test_file_head.txt' }
12
+
13
+ before do
14
+ File.open(test_file, 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
15
+ @expected_head1 = ["line1\n","line2\n","line3\n","line4\n","line5\n"]
16
+ @expected_head1.push("line6\n","line7\n","line8\n","line9\n","line10\n")
17
+ @expected_head2 = ["line1\n","line2\n","line3\n","line4\n","line5\n"]
18
+ end
19
+
20
+ example "head method basic functionality" do
21
+ expect(File).to respond_to(:head)
22
+ expect{ File.head(test_file) }.not_to raise_error
23
+ expect{ File.head(test_file, 5) }.not_to raise_error
24
+ expect{ File.head(test_file){} }.not_to raise_error
25
+ end
26
+
27
+ example "head method returns the expected results" do
28
+ expect(File.head(test_file)).to be_kind_of(Array)
29
+ expect(File.head(test_file)).to eq(@expected_head1)
30
+ expect(File.head(test_file, 5)).to eq(@expected_head2)
31
+ end
32
+
33
+ example "head method requires two arguments" do
34
+ expect{ File.head(test_file, 5, "foo") }.to raise_error(ArgumentError)
35
+ expect{ File.head("bogus") }.to raise_error(Errno::ENOENT)
36
+ end
37
+
38
+ after do
39
+ File.delete(test_file) if File.exists?(test_file)
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ #####################################################################
2
+ # image_spec.rb
3
+ #
4
+ # Specs for the File.image? method. You should run these specs via
5
+ # the 'rake spec:image' task.
6
+ #####################################################################
7
+ require 'rspec'
8
+ require 'ptools'
9
+
10
+ RSpec.describe File, :image do
11
+ before do
12
+ Dir.chdir('spec') if File.exist?('spec')
13
+ @txt_file = File.join(Dir.pwd, 'txt', 'english.txt')
14
+ @uni_file = File.join(Dir.pwd, 'txt', 'korean.txt')
15
+ @jpg_file = File.join(Dir.pwd, 'img', 'test.jpg')
16
+ @png_file = File.join(Dir.pwd, 'img', 'test.png')
17
+ @gif_file = File.join(Dir.pwd, 'img', 'test.gif')
18
+ @ico_file = File.join(Dir.pwd, 'img', 'test.ico')
19
+ end
20
+
21
+ example "image? method basic functionality" do
22
+ expect(File).to respond_to(:image?)
23
+ expect{ File.image?(@txt_file) }.not_to raise_error
24
+ expect(File.image?(@txt_file)).to be(true).or be(false)
25
+ end
26
+
27
+ example "image? method returns false for a text file" do
28
+ expect(File.image?(@txt_file)).to be false
29
+ expect(File.image?(@uni_file)).to be false
30
+ end
31
+
32
+ example "image? method returns true for a gif" do
33
+ expect(File.image?(@gif_file)).to be true
34
+ end
35
+
36
+ example "image? method returns true for a jpeg" do
37
+ expect(File.image?(@jpg_file)).to be true
38
+ end
39
+
40
+ example "image? method returns true for a png" do
41
+ expect(File.image?(@png_file)).to be true
42
+ end
43
+
44
+ example "image? method returns true for an ico" do
45
+ expect(File.image?(@ico_file)).to be true
46
+ end
47
+
48
+ example "image? method raises an error if the file does not exist" do
49
+ expect{ File.image?('bogus') }.to raise_error(Exception) # Errno::ENOENT or ArgumentError
50
+ end
51
+ end
File without changes
Binary file
File without changes
File without changes
@@ -0,0 +1,104 @@
1
+ #####################################################################
2
+ # test_nlconvert.rb
3
+ #
4
+ # Test case for the File.nl_convert method. You should run this
5
+ # test via the 'rake test_nlconvert' task.
6
+ #####################################################################
7
+ require 'rubygems'
8
+ require 'rspec'
9
+ require 'ptools'
10
+
11
+ RSpec.describe File, :nlconvert do
12
+ let(:windows) { File::ALT_SEPARATOR }
13
+ let(:dirname) { File.dirname(__FILE__) }
14
+ let(:test_file1) { File.join(dirname, 'test_nl_convert1.txt') }
15
+ let(:test_file2) { File.join(dirname, 'test_nl_convert2.txt') }
16
+
17
+ before do
18
+ File.open(test_file1, 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
19
+ File.open(test_file2, 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
20
+ @test_file1 = File.join(dirname, 'test_nl_convert1.txt')
21
+ @test_file2 = File.join(dirname, 'test_nl_convert2.txt')
22
+ @dos_file = File.join(dirname, 'dos_test_file.txt')
23
+ @mac_file = File.join(dirname, 'mac_test_file.txt')
24
+ @unix_file = 'unix_test_file.txt'
25
+ end
26
+
27
+ example "nl_for_platform basic functionality" do
28
+ expect(File).to respond_to(:nl_for_platform)
29
+ end
30
+
31
+ example "nl_for_platform returns expected results" do
32
+ expect(File.nl_for_platform('dos') ).to eq( "\cM\cJ")
33
+ expect(File.nl_for_platform('unix') ).to eq( "\cJ")
34
+ expect(File.nl_for_platform('mac') ).to eq( "\cM")
35
+
36
+ end
37
+
38
+ example "nl_for_platform with 'local' platform does not raise an error" do
39
+ expect{ File.nl_for_platform('local') }.not_to raise_error
40
+ end
41
+
42
+ example "nl_for_platform with unsupported platform raises an error" do
43
+ expect{ File.nl_for_platform('bogus') }.to raise_error(ArgumentError)
44
+ end
45
+
46
+ example "nl_convert basic functionality" do
47
+ expect(File).to respond_to(:nl_convert)
48
+ end
49
+
50
+ example "nl_convert accepts one, two or three arguments" do
51
+ expect{ File.nl_convert(@test_file2) }.not_to raise_error
52
+ expect{ File.nl_convert(@test_file2, @test_file2) }.not_to raise_error
53
+ expect{ File.nl_convert(@test_file2, @test_file2, "unix") }.not_to raise_error
54
+ end
55
+
56
+ example "nl_convert with dos platform argument works as expected" do
57
+ expect{ File.nl_convert(@test_file1, @dos_file, "dos") }.not_to raise_error
58
+ expect{ File.nl_convert(@test_file1, @dos_file, "dos") }.not_to raise_error
59
+ expect(File.size(@dos_file)).to be > File.size(@test_file1)
60
+ expect(IO.readlines(@dos_file).first.split("")[-2..-1]).to eq(["\cM","\cJ"])
61
+ end
62
+
63
+ example "nl_convert with mac platform argument works as expected" do
64
+ expect{ File.nl_convert(@test_file1, @mac_file, 'mac') }.not_to raise_error
65
+ expect(IO.readlines(@mac_file).first.split("").last).to eq("\cM")
66
+
67
+ skip if windows
68
+ expect(File.size(@mac_file)).to eq(File.size(@test_file1))
69
+ end
70
+
71
+ example "nl_convert with unix platform argument works as expected" do
72
+ expect{ File.nl_convert(@test_file1, @unix_file, "unix") }.not_to raise_error
73
+ expect(IO.readlines(@unix_file).first.split("").last).to eq("\n")
74
+
75
+ if windows
76
+ expect(File.size(@unix_file) >= File.size(@test_file1)).to be true
77
+ else
78
+ expect(File.size(@unix_file) <= File.size(@test_file1)).to be true
79
+ end
80
+ end
81
+
82
+ example "nl_convert requires at least one argument" do
83
+ expect{ File.nl_convert }.to raise_error(ArgumentError)
84
+ end
85
+
86
+ example "nl_convert requires a valid platform string" do
87
+ expect{ File.nl_convert(@test_file1, "bogus.txt", "blah") }.to raise_error(ArgumentError)
88
+ end
89
+
90
+ example "nl_convert accepts a maximum of three arguments" do
91
+ expect{ File.nl_convert(@example_file1, @test_file2, 'dos', 1) }.to raise_error(ArgumentError)
92
+ expect{ File.nl_convert(@test_file1, @test_file2, 'dos', 1) }.to raise_error(ArgumentError)
93
+ end
94
+
95
+ example "nl_convert will fail on anything but plain files" do
96
+ expect{ File.nl_convert(IO::NULL, @test_file1) }.to raise_error(ArgumentError)
97
+ end
98
+
99
+ after do
100
+ [@dos_file, @mac_file, @unix_file].each{ |file| File.delete(file) if File.exist?(file) }
101
+ File.delete(test_file1) if File.exist?(test_file1)
102
+ File.delete(test_file2) if File.exist?(test_file2)
103
+ end
104
+ end
@@ -0,0 +1,43 @@
1
+ #####################################################################
2
+ # test_is_sparse.rb
3
+ #
4
+ # Test case for the File.sparse? method. You should run this test
5
+ # via the 'rake test:is_sparse' task.
6
+ #####################################################################
7
+ require 'rspec'
8
+ require 'ptools'
9
+
10
+ RSpec.describe File, :sparse do
11
+ let(:windows) { File::ALT_SEPARATOR }
12
+ let(:osx) { RbConfig::CONFIG['host_os'] =~ /darwin|osx/i }
13
+ let(:non_sparse_file) { File.expand_path(File.basename(__FILE__)) }
14
+ let(:sparse_file) { 'test_sparse_file' }
15
+
16
+ before do
17
+ Dir.chdir("spec") if File.exist?("spec")
18
+ system("dd of=#{sparse_file} bs=1k seek=5120 count=0 2>/dev/null") unless windows
19
+ end
20
+
21
+ example "is_sparse basic functionality" do
22
+ skip "skipped on MS Windows or OSX" if windows || osx
23
+ expect(File).to respond_to(:sparse?)
24
+ expect{ File.sparse?(sparse_file) }.not_to raise_error
25
+ expect(File.sparse?(sparse_file)).to be(true).or be(false)
26
+ end
27
+
28
+ example "is_sparse returns the expected results" do
29
+ skip "skipped on MS Windows or OSX" if windows || osx
30
+ expect(File.sparse?(sparse_file)).to be true
31
+ expect(File.sparse?(non_sparse_file)).to be false
32
+ end
33
+
34
+ example "is_sparse only accepts one argument" do
35
+ skip if windows
36
+ expect{ File.sparse?(sparse_file, sparse_file) }.to raise_error(ArgumentError)
37
+ end
38
+
39
+ after do
40
+ Dir.chdir("spec") if File.exist?("spec")
41
+ File.delete(sparse_file) if File.exist?(sparse_file)
42
+ end
43
+ end
@@ -0,0 +1,107 @@
1
+ #####################################################################
2
+ # tail_spec.rb
3
+ #
4
+ # Tests for the File.tail method. This test should be run via
5
+ # the 'rake spec:tail' task.
6
+ #####################################################################
7
+ require 'rspec'
8
+ require 'ptools'
9
+
10
+ RSpec.describe File, :tail do
11
+ let(:dirname) { File.dirname(__FILE__) }
12
+ let(:test_file1) { File.join(dirname, 'test_file1.txt') }
13
+ let(:test_file64) { File.join(dirname, 'test_file64.txt') }
14
+ let(:test_file128) { File.join(dirname, 'test_file128.txt') }
15
+ let(:test_file_trail) { File.join(dirname, 'test_file_trail.txt') }
16
+ let(:test_file_trail_nl) { File.join(dirname, 'test_file_trail_nl.txt') }
17
+
18
+ before do
19
+ File.open(test_file1, 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
20
+
21
+ # Trailing newline test
22
+ File.open(test_file_trail, 'w'){ |fh|
23
+ 2.times{ |n| fh.puts "trail#{n+1}" }
24
+ fh.write "trail3"
25
+ }
26
+
27
+ File.open(test_file_trail_nl, 'w'){ |fh|
28
+ 3.times{ |n| fh.puts "trail#{n+1}" }
29
+ }
30
+
31
+ # Larger files
32
+ test_tail_fmt_str = "line data data data data data data data %5s"
33
+
34
+ File.open(test_file64, 'w'){ |fh|
35
+ 2000.times{ |n|
36
+ fh.puts test_tail_fmt_str % (n+1).to_s
37
+ }
38
+ }
39
+
40
+ File.open(test_file128, 'w'){ |fh|
41
+ 4500.times{ |n|
42
+ fh.puts test_tail_fmt_str % (n+1).to_s
43
+ }
44
+ }
45
+
46
+ @expected_tail1 = %w{
47
+ line16 line17 line18 line19 line20
48
+ line21 line22 line23 line24 line25
49
+ }
50
+
51
+ @expected_tail2 = ["line21","line22","line23","line24","line25"]
52
+
53
+ @expected_tail_more = []
54
+ 25.times{ |n| @expected_tail_more.push "line#{n+1}" }
55
+
56
+ @expected_tail_trail = %w{ trail2 trail3 }
57
+
58
+ @test_tail_fmt_str = "line data data data data data data data %5s"
59
+ end
60
+
61
+ example "tail basic functionality" do
62
+ expect(File).to respond_to(:tail)
63
+ expect{ File.tail(test_file1) }.not_to raise_error
64
+ expect{ File.tail(test_file1, 5) }.not_to raise_error
65
+ expect{ File.tail(test_file1){}.not_to raise_error }
66
+ end
67
+
68
+ example "tail returns the expected values" do
69
+ expect(File.tail(test_file1)).to be_kind_of(Array)
70
+ expect(File.tail(test_file1)).to eq(@expected_tail1)
71
+ expect(File.tail(test_file1, 5)).to eq(@expected_tail2)
72
+ end
73
+
74
+ example "specifying a number greater than the actual number of lines works as expected" do
75
+ expect(File.tail(test_file1, 30)).to eq(@expected_tail_more)
76
+ end
77
+
78
+ example "tail requires two arguments" do
79
+ expect{ File.tail }.to raise_error(ArgumentError)
80
+ expect{ File.tail(test_file1, 5, 5) }.to raise_error(ArgumentError)
81
+ end
82
+
83
+ example "tail works as expected when there is no trailing newline" do
84
+ expect(File.tail(test_file_trail, 2)).to eq(@expected_tail_trail)
85
+ expect(File.tail(test_file_trail_nl, 2)).to eq(@expected_tail_trail)
86
+ end
87
+
88
+ example "tail works as expected on a file larger than 64k" do
89
+ expected_tail_64k = []
90
+ 2000.times{ |n| expected_tail_64k.push(@test_tail_fmt_str % (n+1).to_s) }
91
+ expect(File.tail(test_file64, 2000)).to eq(expected_tail_64k)
92
+ end
93
+
94
+ example "tail works as expected on a file larger than 128k" do
95
+ expected_tail_128k = []
96
+ 4500.times{ |n| expected_tail_128k.push(@test_tail_fmt_str % (n+1).to_s) }
97
+ expect(File.tail(test_file128, 4500)).to eq(expected_tail_128k)
98
+ end
99
+
100
+ after do
101
+ File.delete(test_file1) if File.exist?(test_file1)
102
+ File.delete(test_file64) if File.exist?(test_file64)
103
+ File.delete(test_file128) if File.exist?(test_file128)
104
+ File.delete(test_file_trail_nl) if File.exist?(test_file_trail_nl)
105
+ File.delete(test_file_trail) if File.exist?(test_file_trail)
106
+ end
107
+ end