summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/shared/binwrite.rb
blob: e51093329b45a645458caa9c0db7bf95456c12f3 (plain)
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
require_relative '../fixtures/classes'

describe :io_binwrite, shared: true do
  before :each do
    @filename = tmp("IO_binwrite_file") + $$.to_s
    File.open(@filename, "w") do |file|
      file << "012345678901234567890123456789"
    end
  end

  after :each do
    rm_r @filename
  end

  it "coerces the argument to a string using to_s" do
    (obj = mock('test')).should_receive(:to_s).and_return('a string')
    IO.send(@method, @filename, obj)
  end

  it "returns the number of bytes written" do
    IO.send(@method, @filename, "abcde").should == 5
  end

  it "accepts options as a keyword argument" do
    IO.send(@method, @filename, "hi", 0, flags: File::CREAT).should == 2

    -> {
      IO.send(@method, @filename, "hi", 0, {flags: File::CREAT})
    }.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 2..3)")
  end

  it "creates a file if missing" do
    fn = @filename + "xxx"
    begin
      File.should_not.exist?(fn)
      IO.send(@method, fn, "test")
      File.should.exist?(fn)
    ensure
      rm_r fn
    end
  end

  it "creates file if missing even if offset given" do
    fn = @filename + "xxx"
    begin
      File.should_not.exist?(fn)
      IO.send(@method, fn, "test", 0)
      File.should.exist?(fn)
    ensure
      rm_r fn
    end
  end

  it "truncates the file and writes the given string" do
    IO.send(@method, @filename, "hello, world!")
    File.read(@filename).should == "hello, world!"
  end

  it "doesn't truncate the file and writes the given string if an offset is given" do
    IO.send(@method, @filename, "hello, world!", 0)
    File.read(@filename).should == "hello, world!34567890123456789"
    IO.send(@method, @filename, "hello, world!", 20)
    File.read(@filename).should == "hello, world!3456789hello, world!"
  end

  it "doesn't truncate and writes at the given offset after passing empty opts" do
    IO.send(@method, @filename, "hello world!", 1, **{})
    File.read(@filename).should == "0hello world!34567890123456789"
  end

  it "accepts a :mode option" do
    IO.send(@method, @filename, "hello, world!", mode: 'a')
    File.read(@filename).should == "012345678901234567890123456789hello, world!"
    IO.send(@method, @filename, "foo", 2, mode: 'w')
    File.read(@filename).should == "\0\0foo"
  end

  it "accepts a :flags option without :mode one" do
    IO.send(@method, @filename, "hello, world!", flags: File::CREAT)
    File.read(@filename).should == "hello, world!"
  end

  it "raises an error if readonly mode is specified" do
    -> { IO.send(@method, @filename, "abcde", mode: "r") }.should raise_error(IOError)
  end

  it "truncates if empty :opts provided and offset skipped" do
    IO.send(@method, @filename, "hello, world!", **{})
    File.read(@filename).should == "hello, world!"
  end
end