File: puts_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (112 lines) | stat: -rw-r--r-- 3,383 bytes parent folder | download
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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

# TODO: need to find a better way to test this. Too fragile to set expectations
# to each write call. Only care that all the characters are sent not the number
# or write calls. Also, these tests do not make sure the ordering of the write calls
# are correct.
describe "IO#puts" do
  before :each do
    @name = tmp("io_puts.txt")
    @io = new_io @name
  end

  after :each do
    @io.close unless @io.closed?
    rm_r @name
  end

  it "writes just a newline when given no args" do
    @io.should_receive(:write).with("\n")
    @io.puts.should == nil
  end

  it "writes just a newline when given just a newline" do
    lambda { $stdout.puts "\n" }.should output_to_fd("\n", STDOUT)
  end

  ruby_version_is "" ... "1.9" do
    it "writes nil with a newline when given nil as an arg" do
      @io.should_receive(:write).with("nil")
      @io.should_receive(:write).with("\n")
      @io.puts(nil).should == nil
    end
  end

  ruby_version_is "1.9" do
    it "writes empty string with a newline when given nil as an arg" do
      @io.should_receive(:write).with("")
      @io.should_receive(:write).with("\n")
      @io.puts(nil).should == nil
    end

    it "writes empty string with a newline when when given nil as multiple args" do
      @io.should_receive(:write).with("").twice
      @io.should_receive(:write).with("\n").twice
      @io.puts(nil, nil).should == nil
    end
  end

  it "calls to_s before writing non-string objects" do
    object = mock('hola')
    object.should_receive(:to_s).and_return("hola")

    @io.should_receive(:write).with("hola")
    @io.should_receive(:write).with("\n")
    @io.puts(object).should == nil
  end

  it "writes each arg if given several" do
    @io.should_receive(:write).with("1")
    @io.should_receive(:write).with("two")
    @io.should_receive(:write).with("3")
    @io.should_receive(:write).with("\n").exactly(3).times
    @io.puts(1, "two", 3).should == nil
  end

  it "flattens a nested array before writing it" do
    @io.should_receive(:write).with("1")
    @io.should_receive(:write).with("2")
    @io.should_receive(:write).with("3")
    @io.should_receive(:write).with("\n").exactly(3).times
    @io.puts([1, 2, [3]]).should == nil
  end

  it "writes nothing for an empty array" do
    x = []
    @io.should_receive(:write).exactly(0).times
    @io.puts(x).should == nil
  end

  it "writes [...] for a recursive array arg" do
    x = []
    x << 2 << x
    @io.should_receive(:write).with("2")
    @io.should_receive(:write).with("[...]")
    @io.should_receive(:write).with("\n").exactly(2).times
    @io.puts(x).should == nil
  end

  it "writes a newline after objects that do not end in newlines" do
    @io.should_receive(:write).with("5")
    @io.should_receive(:write).with("\n")
    @io.puts(5).should == nil
  end

  it "does not write a newline after objects that end in newlines" do
    @io.should_receive(:write).with("5\n")
    @io.puts("5\n").should == nil
  end

  it "ignores the $/ separator global" do
    $/ = ":"
    @io.should_receive(:write).with("5")
    @io.should_receive(:write).with("\n")
    @io.puts(5).should == nil
    $/ = "\n"
  end

  it "raises IOError on closed stream" do
    lambda { IOSpecs.closed_io.puts("stuff") }.should raise_error(IOError)
  end
end