ptools 1.2.2-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ #####################################################################
2
+ # test_middle.rb
3
+ #
4
+ # Test case for the File.middle method. You should run this test
5
+ # via the 'rake test_middle' task.
6
+ #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'ptools'
12
+
13
+ class TC_FileMiddle < Test::Unit::TestCase
14
+ def self.startup
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
17
+ end
18
+
19
+ def setup
20
+ @test_file = 'test_file1.txt'
21
+
22
+ @expected_middle1 = ["line10\n", "line11\n", "line12\n", "line13\n", "line14\n"]
23
+ @expected_middle1.push("line15\n","line16\n", "line17\n", "line18\n")
24
+ @expected_middle1.push("line19\n","line20\n")
25
+
26
+ @expected_middle2 = ["line14\n","line15\n","line16\n","line17\n"]
27
+ @expected_middle2.push("line18\n","line19\n","line20\n")
28
+
29
+ @expected_middle3 = ["line5\n","line6\n","line7\n"]
30
+ @expected_middle3.push("line8\n","line9\n","line10\n")
31
+ end
32
+
33
+ def test_method_basic
34
+ assert_respond_to(File, :middle)
35
+ assert_nothing_raised{ File.middle(@test_file) }
36
+ assert_nothing_raised{ File.middle(@test_file, 14) }
37
+ assert_nothing_raised{ File.middle(@test_file, 5, 10) }
38
+ assert_nothing_raised{ File.middle(@test_file){} }
39
+ end
40
+
41
+ def test_middle_expected_results
42
+ assert_kind_of(Array, File.middle(@test_file))
43
+ assert_equal(@expected_middle1, File.middle(@test_file))
44
+ assert_equal(@expected_middle2, File.middle(@test_file, 14))
45
+ assert_equal(@expected_middle3, File.middle(@test_file, 5, 10))
46
+ end
47
+
48
+ def test_middle_expected_errors
49
+ assert_raises(ArgumentError){ File.middle }
50
+ assert_raises(ArgumentError){ File.middle(@test_file, 5, 10, 15) }
51
+ assert_raises(NoMethodError){ File.middle(@test_file, "foo") }
52
+ assert_raises(Errno::ENOENT){ File.middle("bogus") }
53
+ end
54
+
55
+ def teardown
56
+ @test_file = nil
57
+ @expected_middle1 = nil
58
+ @expected_middle2 = nil
59
+ @expected_middle3 = nil
60
+ end
61
+ end
@@ -0,0 +1,99 @@
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 'test-unit'
9
+ require 'ptools'
10
+
11
+ class TC_Ptools_NLConvert < Test::Unit::TestCase
12
+ def self.startup
13
+ Dir.chdir('test') if File.exists?('test')
14
+ @@test_file1 = 'test_nl_convert1.txt'
15
+ @@test_file2 = 'test_nl_convert2.txt'
16
+ File.open(@@test_file1, 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
17
+ File.open(@@test_file2, 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
18
+ end
19
+
20
+ def setup
21
+ @test_file1 = 'test_nl_convert1.txt'
22
+ @test_file2 = 'test_nl_convert2.txt'
23
+ @dos_file = 'dos_test_file.txt'
24
+ @mac_file = 'mac_test_file.txt'
25
+ @unix_file = 'nix_test_file.txt'
26
+ end
27
+
28
+ test "nl_convert basic functionality" do
29
+ assert_respond_to(File, :nl_convert)
30
+ end
31
+
32
+ test "nl_convert accepts one, two or three arguments" do
33
+ assert_nothing_raised{ File.nl_convert(@test_file2) }
34
+ assert_nothing_raised{ File.nl_convert(@test_file2, @test_file2) }
35
+ assert_nothing_raised{ File.nl_convert(@test_file2, @test_file2, "unix") }
36
+ end
37
+
38
+ test "nl_convert with dos platform argument works as expected" do
39
+ msg = "dos file should be larger, but isn't"
40
+
41
+ assert_nothing_raised{ File.nl_convert(@test_file1, @dos_file, "dos") }
42
+ assert_true(File.size(@dos_file) > File.size(@test_file1), msg)
43
+ assert_equal(["\cM","\cJ"], IO.readlines(@dos_file).first.split("")[-2..-1])
44
+ end
45
+
46
+ test "nl_convert with mac platform argument works as expected" do
47
+ assert_nothing_raised{ File.nl_convert(@test_file1, @mac_file, 'mac') }
48
+ assert_equal("\cM", IO.readlines(@mac_file).first.split("").last)
49
+
50
+ omit_if(File::ALT_SEPARATOR)
51
+ msg = "=> Mac file should be the same size (or larger), but isn't"
52
+ assert_true(File.size(@mac_file) == File.size(@test_file1), msg)
53
+ end
54
+
55
+ test "nl_convert with unix platform argument works as expected" do
56
+ msg = "unix file should be the same size (or smaller), but isn't"
57
+
58
+ assert_nothing_raised{ File.nl_convert(@test_file1, @unix_file, "unix") }
59
+ assert_equal("\n", IO.readlines(@unix_file).first.split("").last)
60
+
61
+ if File::ALT_SEPARATOR
62
+ assert_true(File.size(@unix_file) >= File.size(@test_file1), msg)
63
+ else
64
+ assert_true(File.size(@unix_file) <= File.size(@test_file1), msg)
65
+ end
66
+ end
67
+
68
+ test "nl_convert requires at least one argument" do
69
+ assert_raise(ArgumentError){ File.nl_convert }
70
+ end
71
+
72
+ test "nl_convert requires a valid platform string" do
73
+ assert_raise(ArgumentError){ File.nl_convert(@test_file1, "bogus.txt", "blah") }
74
+ end
75
+
76
+ test "nl_convert accepts a maximum of three arguments" do
77
+ assert_raise(ArgumentError){ File.nl_convert(@test_file1, @test_file2, 'dos', 1) }
78
+ end
79
+
80
+ test "nl_convert will fail on anything but plain files" do
81
+ assert_raise(ArgumentError){ File.nl_convert(File.null_device, @test_file1) }
82
+ end
83
+
84
+ def teardown
85
+ [@dos_file, @mac_file, @unix_file].each{ |file|
86
+ File.delete(file) if File.exists?(file)
87
+ }
88
+ @dos_file = nil
89
+ @mac_file = nil
90
+ @unix_file = nil
91
+ @test_file1 = nil
92
+ @test_file2 = nil
93
+ end
94
+
95
+ def self.shutdown
96
+ File.delete(@@test_file1) if File.exists?(@@test_file1)
97
+ File.delete(@@test_file2) if File.exists?(@@test_file2)
98
+ end
99
+ end
data/test/test_null.rb ADDED
@@ -0,0 +1,40 @@
1
+ #####################################################################
2
+ # test_null.rb
3
+ #
4
+ # Test case for the File.null method. You should run this test via
5
+ # the 'rake test_null' task.
6
+ #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'ptools'
12
+
13
+ class TC_FileNull < Test::Unit::TestCase
14
+ def setup
15
+ @nulls = ['/dev/null', 'NUL', 'NIL:', 'NL:']
16
+ end
17
+
18
+ test "null method basic functionality" do
19
+ assert_respond_to(File, :null)
20
+ assert_nothing_raised{ File.null }
21
+ end
22
+
23
+ test "null method returns expected results" do
24
+ assert_kind_of(String, File.null)
25
+ assert(@nulls.include?(File.null))
26
+ end
27
+
28
+ test "null method does not accept any arguments" do
29
+ assert_raises(ArgumentError){ File.null(1) }
30
+ end
31
+
32
+ test "null_device is an alias for null" do
33
+ assert_respond_to(File, :null_device)
34
+ assert_alias_method(File, :null_device, :null)
35
+ end
36
+
37
+ def teardown
38
+ @nulls = nil
39
+ end
40
+ end
data/test/test_tail.rb ADDED
@@ -0,0 +1,56 @@
1
+ #####################################################################
2
+ # test_tail.rb
3
+ #
4
+ # Test case for the File.tail method. This test should be run via
5
+ # the 'rake test_tail' task.
6
+ #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'ptools'
12
+
13
+ class TC_FileTail < Test::Unit::TestCase
14
+ def self.startup
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
17
+ end
18
+
19
+ def setup
20
+ @test_file = 'test_file1.txt'
21
+
22
+ @expected_tail1 = ["line16\n","line17\n","line18\n","line19\n"]
23
+ @expected_tail1.push("line20\n","line21\n","line22\n", "line23\n")
24
+ @expected_tail1.push("line24\n","line25\n")
25
+
26
+ @expected_tail2 = ["line21\n","line22\n","line23\n","line24\n","line25\n"]
27
+ end
28
+
29
+ def test_tail_basic
30
+ assert_respond_to(File, :tail)
31
+ assert_nothing_raised{ File.tail(@test_file) }
32
+ assert_nothing_raised{ File.tail(@test_file, 5) }
33
+ assert_nothing_raised{ File.tail(@test_file){} }
34
+ end
35
+
36
+ def test_tail_expected_return_values
37
+ assert_kind_of(Array, File.tail(@test_file))
38
+ assert_equal(@expected_tail1, File.tail(@test_file))
39
+ assert_equal(@expected_tail2, File.tail(@test_file, 5))
40
+ end
41
+
42
+ def test_tail_expected_errors
43
+ assert_raises(ArgumentError){ File.tail }
44
+ assert_raises(ArgumentError){ File.tail(@test_file, 5, 5) }
45
+ end
46
+
47
+ def teardown
48
+ @test_file = nil
49
+ @expected_tail1 = nil
50
+ @expected_tail2 = nil
51
+ end
52
+
53
+ def self.shutdown
54
+ File.delete('test_file1.txt') if File.exists?('test_file1.txt')
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ #####################################################################
2
+ # tc_touch.rb
3
+ #
4
+ # Test case for the File.touch method. This test should be run
5
+ # via the 'rake test_touch task'.
6
+ #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'ptools'
12
+
13
+ class TC_FileTouch < Test::Unit::TestCase
14
+ def self.startup
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 10.times{ |n| fh.puts "line #{n}" } }
17
+ end
18
+
19
+ def setup
20
+ @test_file = 'delete.this'
21
+ @xfile = 'test_file1.txt'
22
+ end
23
+
24
+ def test_touch_basic
25
+ assert_respond_to(File, :touch)
26
+ assert_nothing_raised{ File.touch(@test_file) }
27
+ end
28
+
29
+ def test_touch_expected_results
30
+ assert_equal(File, File.touch(@test_file))
31
+ assert_equal(true, File.exists?(@test_file))
32
+ assert_equal(0, File.size(@test_file))
33
+ end
34
+
35
+ def test_touch_existing_file
36
+ stat = File.stat(@xfile)
37
+ sleep 1
38
+ assert_nothing_raised{ File.touch(@xfile) }
39
+ assert_equal(true, File.size(@xfile) == stat.size)
40
+ assert_equal(false, File.mtime(@xfile) == stat.mtime)
41
+ end
42
+
43
+ def test_touch_expected_errors
44
+ assert_raises(ArgumentError){ File.touch }
45
+ end
46
+
47
+ def teardown
48
+ File.delete(@test_file) if File.exists?(@test_file)
49
+ @test_file = nil
50
+ end
51
+
52
+ def self.shutdown
53
+ File.delete('test_file1.txt') if File.exists?('test_file1.txt')
54
+ end
55
+ end
data/test/test_wc.rb ADDED
@@ -0,0 +1,76 @@
1
+ #####################################################################
2
+ # test_wc.rb
3
+ #
4
+ # Test case for the File.wc method. This test should be run via
5
+ # the 'rake test_wc' task.
6
+ #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'ptools'
12
+
13
+ class TC_FileWC < Test::Unit::TestCase
14
+ def self.startup
15
+ Dir.chdir('test') if File.exists?('test')
16
+ File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
17
+ @@test_file = 'test_file1.txt'
18
+ end
19
+
20
+ def setup
21
+ @test_file = 'test_file1.txt'
22
+ end
23
+
24
+ test "wc method basic functionality" do
25
+ assert_respond_to(File, :wc)
26
+ assert_nothing_raised{ File.wc(@test_file) }
27
+ end
28
+
29
+ test "wc accepts specific optional arguments" do
30
+ assert_nothing_raised{ File.wc(@test_file, 'bytes') }
31
+ assert_nothing_raised{ File.wc(@test_file, 'chars') }
32
+ assert_nothing_raised{ File.wc(@test_file, 'words') }
33
+ assert_nothing_raised{ File.wc(@test_file, 'lines') }
34
+ end
35
+
36
+ test "argument to wc ignores the case of the option argument" do
37
+ assert_nothing_raised{ File.wc(@test_file, 'LINES') }
38
+ end
39
+
40
+ test "wc with no option returns expected results" do
41
+ assert_kind_of(Array, File.wc(@test_file))
42
+ assert_equal([166,166,25,25], File.wc(@test_file))
43
+ end
44
+
45
+ test "wc with bytes option returns the expected result" do
46
+ assert_equal(166, File.wc(@test_file, 'bytes'), "Wrong number of bytes")
47
+ end
48
+
49
+ test "wc with chars option returns the expected result" do
50
+ assert_equal(166, File.wc(@test_file, 'chars'), "Wrong number of chars")
51
+ end
52
+
53
+ test "wc with words option returns the expected result" do
54
+ assert_equal(25, File.wc(@test_file, 'words'), "Wrong number of words")
55
+ end
56
+
57
+ test "wc with lines option returns the expected result" do
58
+ assert_equal(25, File.wc(@test_file, 'lines'), "Wrong number of lines")
59
+ end
60
+
61
+ test "wc requires at least on argument" do
62
+ assert_raises(ArgumentError){ File.wc }
63
+ end
64
+
65
+ test "an invalid option raises an error" do
66
+ assert_raises(ArgumentError){ File.wc(@test_file, 'bogus') }
67
+ end
68
+
69
+ def teardown
70
+ @test_file = nil
71
+ end
72
+
73
+ def self.shutdown
74
+ File.delete(@@test_file) if File.exists?(@@test_file)
75
+ end
76
+ end
@@ -0,0 +1,98 @@
1
+ ######################################################################
2
+ # test_whereis.rb
3
+ #
4
+ # Tests for the File.whereis method.
5
+ ######################################################################
6
+ require 'rubygems'
7
+ require 'test-unit'
8
+ require 'ptools'
9
+ require 'rbconfig'
10
+
11
+ class TC_Ptools_Whereis < Test::Unit::TestCase
12
+ def self.startup
13
+ @@windows = File::ALT_SEPARATOR
14
+ @@ruby = RUBY_PLATFORM == 'java' ? 'jruby' : 'ruby'
15
+ end
16
+
17
+ def setup
18
+ @bin_dir = RbConfig::CONFIG['bindir']
19
+ @expected_locs = [File.join(@bin_dir, @@ruby)]
20
+
21
+ if @@windows
22
+ @expected_locs[0] << '.exe'
23
+ @expected_locs[0].tr!("/", "\\")
24
+ end
25
+
26
+ unless @@windows
27
+ @expected_locs << "/usr/local/bin/#{@@ruby}"
28
+ @expected_locs << "/opt/sfw/bin/#{@@ruby}"
29
+ @expected_locs << "/opt/bin/#{@@ruby}"
30
+ @expected_locs << "/usr/bin/#{@@ruby}"
31
+ end
32
+
33
+ @actual_locs = nil
34
+ end
35
+
36
+ test "whereis basic functionality" do
37
+ assert_respond_to(File, :whereis)
38
+ assert_nothing_raised{ File.whereis('ruby') }
39
+ assert_kind_of([Array, NilClass], File.whereis('ruby'))
40
+ end
41
+
42
+ test "whereis accepts an optional second argument" do
43
+ assert_nothing_raised{ File.whereis('ruby', '/usr/bin:/usr/local/bin') }
44
+ end
45
+
46
+ test "whereis returns expected values" do
47
+ assert_nothing_raised{ @actual_locs = File.whereis(@@ruby) }
48
+ assert_kind_of(Array, @actual_locs)
49
+ assert_true((@expected_locs & @actual_locs).size > 0)
50
+ end
51
+
52
+ test "whereis returns nil if program not found" do
53
+ assert_nil(File.whereis('xxxyyy'))
54
+ end
55
+
56
+ test "whereis returns nil if program cannot be found in provided path" do
57
+ assert_nil(File.whereis(@@ruby, '/foo/bar'))
58
+ end
59
+
60
+ test "whereis returns single element array or nil if absolute path is provided" do
61
+ absolute = File.join(@bin_dir, @@ruby)
62
+ absolute << '.exe' if @@windows
63
+
64
+ assert_equal([absolute], File.whereis(absolute))
65
+ assert_nil(File.whereis("/foo/bar/baz/#{@@ruby}"))
66
+ end
67
+
68
+ test "whereis works with an explicit extension on ms windows" do
69
+ omit_unless(@@windows, 'test skipped except on MS Windows')
70
+ assert_not_nil(File.whereis(@@ruby + '.exe'))
71
+ end
72
+
73
+ test "whereis requires at least one argument" do
74
+ assert_raise(ArgumentError){ File.whereis }
75
+ end
76
+
77
+ test "whereis returns unique paths only" do
78
+ assert_true(File.whereis(@@ruby) == File.whereis(@@ruby).uniq)
79
+ end
80
+
81
+ test "whereis accepts a maximum of two arguments" do
82
+ assert_raise(ArgumentError){ File.whereis(@@ruby, 'foo', 'bar') }
83
+ end
84
+
85
+ test "the second argument to whereis cannot be nil or empty" do
86
+ assert_raise(ArgumentError){ File.whereis(@@ruby, nil) }
87
+ assert_raise(ArgumentError){ File.whereis(@@ruby, '') }
88
+ end
89
+
90
+ def teardown
91
+ @expected_locs = nil
92
+ @actual_locs = nil
93
+ end
94
+
95
+ def self.shutdown
96
+ @@windows = nil
97
+ end
98
+ end
@@ -0,0 +1,113 @@
1
+ #####################################################################
2
+ # test_which.rb
3
+ #
4
+ # Test case for the File.which method. You should run this test
5
+ # via the 'rake test_which' rake task.
6
+ #
7
+ # NOTE: I make the assumption that Ruby (or JRuby) is in your
8
+ # PATH for these tests.
9
+ #####################################################################
10
+ require 'rubygems'
11
+ gem 'test-unit'
12
+
13
+ require 'test/unit'
14
+ require 'rbconfig'
15
+ require 'fileutils'
16
+ require 'ptools'
17
+
18
+ class TC_FileWhich < Test::Unit::TestCase
19
+ def self.startup
20
+ @@windows = File::ALT_SEPARATOR
21
+ @@dir = File.join(Dir.pwd, 'tempdir')
22
+ @@non_exe = File.join(Dir.pwd, 'tempfile')
23
+
24
+ Dir.mkdir(@@dir) unless File.exists?(@@dir)
25
+ FileUtils.touch(@@non_exe)
26
+ File.chmod(775, @@dir)
27
+ File.chmod(644, @@non_exe)
28
+ end
29
+
30
+ def setup
31
+ @ruby = RUBY_PLATFORM.match('java') ? 'jruby' : 'ruby'
32
+ @ruby = 'rbx' if defined?(Rubinius)
33
+
34
+ @exe = File.join(
35
+ RbConfig::CONFIG['bindir'],
36
+ RbConfig::CONFIG['ruby_install_name']
37
+ )
38
+
39
+ if @@windows
40
+ @exe.tr!('/','\\')
41
+ @exe << ".exe"
42
+ end
43
+ end
44
+
45
+ test "which method basic functionality" do
46
+ assert_respond_to(File, :which)
47
+ assert_nothing_raised{ File.which(@ruby) }
48
+ assert_kind_of(String, File.which(@ruby))
49
+ end
50
+
51
+ test "which accepts an optional path to search" do
52
+ assert_nothing_raised{ File.which(@ruby, "/usr/bin:/usr/local/bin") }
53
+ end
54
+
55
+ test "which returns nil if not found" do
56
+ assert_equal(nil, File.which(@ruby, '/bogus/path'))
57
+ assert_equal(nil, File.which('blahblahblah'))
58
+ end
59
+
60
+ test "which handles executables without extensions on windows" do
61
+ omit_unless(@@windows, "test skipped unless MS Windows")
62
+ assert_not_nil(File.which('ruby'))
63
+ assert_not_nil(File.which('notepad'))
64
+ end
65
+
66
+ test "which handles executables that already contain extensions on windows" do
67
+ omit_unless(@@windows, "test skipped unless MS Windows")
68
+ assert_not_nil(File.which('ruby.exe'))
69
+ assert_not_nil(File.which('notepad.exe'))
70
+ end
71
+
72
+ test "which returns argument if an existent absolute path is provided" do
73
+ assert_equal(@exe, File.which(@ruby), "=> May fail on a symlink")
74
+ end
75
+
76
+ test "which returns nil if a non-existent absolute path is provided" do
77
+ assert_nil(File.which('/foo/bar/baz/ruby'))
78
+ end
79
+
80
+ test "which does not pickup files that are not executable" do
81
+ assert_nil(File.which(@@non_exe))
82
+ end
83
+
84
+ test "which does not pickup executable directories" do
85
+ assert_nil(File.which(@@dir))
86
+ end
87
+
88
+ test "which accepts a minimum of one argument" do
89
+ assert_raises(ArgumentError){ File.which }
90
+ end
91
+
92
+ test "which accepts a maximum of two arguments" do
93
+ assert_raises(ArgumentError){ File.which(@ruby, "foo", "bar") }
94
+ end
95
+
96
+ test "the second argument cannot be nil or empty" do
97
+ assert_raises(ArgumentError){ File.which(@ruby, nil) }
98
+ assert_raises(ArgumentError){ File.which(@ruby, '') }
99
+ end
100
+
101
+ def teardown
102
+ @exe = nil
103
+ @ruby = nil
104
+ end
105
+
106
+ def self.shutdown
107
+ FileUtils.rm(@@non_exe)
108
+ FileUtils.rm_rf(@@dir)
109
+ @@windows = nil
110
+ @@dir = nil
111
+ @@non_exe = nil
112
+ end
113
+ end