Skip to content

Commit

Permalink
Add chomp option to gets, lines, each_line
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Dec 15, 2016
1 parent 366aec3 commit 942b0f6
Show file tree
Hide file tree
Showing 21 changed files with 417 additions and 129 deletions.
2 changes: 1 addition & 1 deletion spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe "macro methods" do
end

it "executes lines" do
assert_macro "x", %({{x.lines}}), [StringLiteral.new("1\n2\n3")] of ASTNode, %(["1\\n", "2\\n", "3"])
assert_macro "x", %({{x.lines}}), [StringLiteral.new("1\n2\n3")] of ASTNode, %(["1", "2", "3"])
end

it "executes size" do
Expand Down
28 changes: 28 additions & 0 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,29 @@ describe "File" do
it "reads lines from file" do
lines = File.read_lines "#{__DIR__}/data/test_file.txt"
lines.size.should eq(20)
lines.first.should eq("Hello World")
end

it "reads lines from file with chomp = false" do
lines = File.read_lines "#{__DIR__}/data/test_file.txt", chomp: false
lines.size.should eq(20)
lines.first.should eq("Hello World\n")
end

it "reads lines from file with each" do
idx = 0
File.each_line("#{__DIR__}/data/test_file.txt") do |line|
if idx == 0
line.should eq("Hello World")
end
idx += 1
end
idx.should eq(20)
end

it "reads lines from file with each, chomp = false" do
idx = 0
File.each_line("#{__DIR__}/data/test_file.txt", chomp: false) do |line|
if idx == 0
line.should eq("Hello World\n")
end
Expand All @@ -69,6 +86,17 @@ describe "File" do
it "reads lines from file with each as iterator" do
idx = 0
File.each_line("#{__DIR__}/data/test_file.txt").each do |line|
if idx == 0
line.should eq("Hello World")
end
idx += 1
end
idx.should eq(20)
end

it "reads lines from file with each as iterator, chomp = false" do
idx = 0
File.each_line("#{__DIR__}/data/test_file.txt", chomp: false).each do |line|
if idx == 0
line.should eq("Hello World\n")
end
Expand Down
22 changes: 16 additions & 6 deletions spec/std/io/argf_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ describe IO::ARGF do
stdin = IO::Memory.new("hello\nworld\n")

argf = IO::ARGF.new argv, stdin
argf.gets.should eq("hello\n")
argf.gets.should eq("world\n")
argf.gets.should eq("hello")
argf.gets.should eq("world")
argf.gets.should be_nil
end

it "reads from STDIN if ARGV isn't specified, chomp = false" do
argv = [] of String
stdin = IO::Memory.new("hello\nworld\n")

argf = IO::ARGF.new argv, stdin
argf.gets(chomp: false).should eq("hello\n")
argf.gets(chomp: false).should eq("world\n")
argf.gets(chomp: false).should be_nil
end

it "reads from ARGV if specified" do
path1 = "#{__DIR__}/../data/argf_test_file_1.txt"
path2 = "#{__DIR__}/../data/argf_test_file_2.txt"
Expand All @@ -58,16 +68,16 @@ describe IO::ARGF do
argf = IO::ARGF.new argv, stdin
argv.should eq([path1, path2])

argf.gets.should eq("12345\n")
argf.gets(chomp: false).should eq("12345\n")
argv.should eq([path2])

argf.gets.should eq("67890\n")
argf.gets(chomp: false).should eq("67890\n")
argv.empty?.should be_true

argf.gets.should be_nil
argf.gets(chomp: false).should be_nil

argv << path1
str = argf.gets
str = argf.gets(chomp: false)
str.should eq("12345\n")
end
end
Expand Down
55 changes: 42 additions & 13 deletions spec/std/io/buffered_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,35 @@ end

describe "IO::Buffered" do
it "does gets" do
io = BufferedWrapper.new(IO::Memory.new("hello\nworld\n"))
io.gets.should eq("hello\n")
io.gets.should eq("world\n")
io = BufferedWrapper.new(IO::Memory.new("hello\r\nworld\n"))
io.gets.should eq("hello")
io.gets.should eq("world")
io.gets.should be_nil
end

it "does gets with chomp = false" do
io = BufferedWrapper.new(IO::Memory.new("hello\nworld\n"))
io.gets(chomp: false).should eq("hello\n")
io.gets(chomp: false).should eq("world\n")
io.gets(chomp: false).should be_nil
end

it "does gets with big line" do
big_line = "a" * 20_000
io = BufferedWrapper.new(IO::Memory.new("#{big_line}\nworld\n"))
io.gets.should eq("#{big_line}\n")
io.gets.should eq(big_line)
end

it "does gets with big line and \\r\\n" do
big_line = "a" * 20_000
io = BufferedWrapper.new(IO::Memory.new("#{big_line}\r\nworld\n"))
io.gets.should eq(big_line)
end

it "does gets with big line and chomp = false" do
big_line = "a" * 20_000
io = BufferedWrapper.new(IO::Memory.new("#{big_line}\nworld\n"))
io.gets(chomp: false).should eq("#{big_line}\n")
end

it "does gets with char delimiter" do
Expand Down Expand Up @@ -174,9 +193,9 @@ describe "IO::Buffered" do
it "rewinds" do
str = IO::Memory.new("hello\nworld\n")
io = BufferedWrapper.new str
io.gets.should eq("hello\n")
io.gets.should eq("hello")
io.rewind
io.gets.should eq("hello\n")
io.gets.should eq("hello")
end

it "reads more than the buffer's internal capacity" do
Expand Down Expand Up @@ -302,9 +321,19 @@ describe "IO::Buffered" do
base_io = IO::Memory.new(str.encode("UCS-2LE"))
io = BufferedWrapper.new(base_io)
io.set_encoding("UCS-2LE")
io.gets.should eq("Hello world\n")
io.gets.should eq("Foo\n")
io.gets.should eq("Bar\n")
io.gets.should eq("Hello world")
io.gets.should eq("Foo")
io.gets.should eq("Bar")
end

it "gets with chomp = false" do
str = "Hello world\nFoo\nBar\n" + ("1234567890" * 1000)
base_io = IO::Memory.new(str.encode("UCS-2LE"))
io = BufferedWrapper.new(base_io)
io.set_encoding("UCS-2LE")
io.gets(chomp: false).should eq("Hello world\n")
io.gets(chomp: false).should eq("Foo\n")
io.gets(chomp: false).should eq("Bar\n")
end

it "gets big string" do
Expand All @@ -313,8 +342,8 @@ describe "IO::Buffered" do
io = BufferedWrapper.new(base_io)
io.set_encoding("UCS-2LE")
10_000.times do |i|
io.gets.should eq("Hello\n")
io.gets.should eq("World\n")
io.gets(chomp: false).should eq("Hello\n")
io.gets(chomp: false).should eq("World\n")
end
end

Expand All @@ -324,7 +353,7 @@ describe "IO::Buffered" do
io = BufferedWrapper.new(base_io)
io.set_encoding("GB2312")
1000.times do
io.gets.should eq("你好我是人\n")
io.gets(chomp: false).should eq("你好我是人\n")
end
end

Expand All @@ -333,7 +362,7 @@ describe "IO::Buffered" do
base_io = IO::Memory.new(str.encode("UCS-2LE"))
io = BufferedWrapper.new(base_io)
io.set_encoding("UCS-2LE")
io.gets.should eq("x\n")
io.gets(chomp: false).should eq("x\n")
str = str[2..-1]
str.each_char do |char|
io.read_char.should eq(char)
Expand Down
Loading

0 comments on commit 942b0f6

Please sign in to comment.