forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_line_endings.rb
115 lines (101 loc) · 2.57 KB
/
test_line_endings.rb
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
require 'test/unit'
require 'rbconfig'
require 'tempfile'
class TestLineEndings < Test::Unit::TestCase
WINDOWS = Config::CONFIG['host_os'] =~ /Windows|mswin/
def temp_file(name, flags = "w", &block)
@tmpfile = Tempfile.new name
@tmpfile.close
if block
File.open(@tmpfile.path, flags, &block)
File.new(@tmpfile.path)
else
File.open(@tmpfile.path, flags)
end
end
def existing_file
"test/test_line_endings#{WINDOWS ? '-windows' : ''}.txt"
end
def teardown
@tmpfile.unlink if @tmpfile
end
def test_readlines_eats_carriage_returns
lines = IO.readlines(existing_file)
assert_equal ["here is line 1\n", "here is line 2\n"], lines
end
def test_gets_eats_carriage_returns
File.open(existing_file) do |f|
assert_equal "here is line 1\n", f.gets
assert_equal "here is line 2\n", f.gets
end
end
def test_puts_adds_carriage_returns_on_windows
io = temp_file("puts-line-endings")
io.puts "hi"
io.close
File.open(io.path, "rb") do |f|
expected = "hi"
expected << "\r" if WINDOWS
expected << "\n"
assert_equal expected, f.read
end
end
def a_b_temp_file
temp_file('foo2.txt') do |c|
c.puts 'A'
c.puts 'B'
end
end
# JRUBY-61
def test_puts_file_size
f = a_b_temp_file
size = 4
size = 6 if WINDOWS
assert_equal(size, File.size(f.path))
end
def test_each_byte_reads_crlf_in_binary_mode
f = a_b_temp_file
f.close
f = File.new(f.path, "rb")
count=-1
contents = [65,13,10,66,13,10]
contents.delete_if {|x| x == 13 && !WINDOWS}
f.each_byte {|x| assert_equal(contents[count+=1], x) }
f.close
end
def test_each_byte_reads_lf_in_text_mode
f = a_b_temp_file
count=-1
f.each_byte {|x| assert_equal([65,10,66,10][count+=1], x) }
f.close
end
def test_io_getc_reads_cr_in_binary_mode
f = a_b_temp_file
f.close
f = File.new(f.path, "rb")
count=0
positions = [0,1,2,3,4,5]
contents = [65,13,10,66,13,10]
positions = positions[0..3] unless WINDOWS
contents.delete_if {|x| x == 13 && !WINDOWS}
while not f.eof?
assert_equal(positions[count], f.pos)
assert_equal(contents[count], f.getc)
count += 1
end
f.close
end
def test_io_getc_eats_cr_in_text_mode
f = a_b_temp_file
count=0
positions = [0,1,2,3]
positions = [0,1,3,4] if WINDOWS
contents = [65,10,66,10]
while not f.eof?
assert_equal(positions[count], f.pos)
assert_equal(contents[count], f.getc)
count += 1
end
f.close
end
end