Skip to content
This repository
Browse code

Use options_or_mode in new_fd and new_io helpers.

  • Loading branch information...
commit 64ece89b5c03e6d8dcf1574beb9ffa8a82f7e7b9 1 parent 474478d
Brian Shirai brixen authored
14 mspec/lib/mspec/helpers/io.rb
@@ -27,13 +27,23 @@ class Object
27 27 # with any Ruby object). The file descriptor can safely be passed
28 28 # to IO.new without creating a Ruby object alias to the fd.
29 29 def new_fd(name, mode="w:utf-8")
  30 + mode = options_or_mode(mode)
  31 +
  32 + if mode.kind_of? Hash
  33 + if mode.key? :mode
  34 + mode = mode[:mode]
  35 + else
  36 + raise ArgumentError, "new_fd options Hash must include :mode"
  37 + end
  38 + end
  39 +
30 40 IO.sysopen name, fmode(mode)
31 41 end
32 42
33 43 # Creates an IO instance for a temporary file name. The file
34 44 # must be deleted.
35 45 def new_io(name, mode="w:utf-8")
36   - IO.new new_fd(name, fmode(mode)), fmode(mode)
  46 + IO.new new_fd(name, options_or_mode(mode)), options_or_mode(mode)
37 47 end
38 48
39 49 # This helper simplifies passing file access modes regardless of
@@ -58,7 +68,7 @@ def options_or_mode(oom)
58 68 if FeatureGuard.enabled? :encoding
59 69 oom
60 70 else
61   - fmode(oom[:mode]) || "r"
  71 + fmode(oom[:mode] || "r:utf-8")
62 72 end
63 73 end
64 74 end
34 mspec/spec/helpers/io_spec.rb
@@ -50,7 +50,7 @@
50 50 end
51 51
52 52 after :each do
53   - @io.close unless @io.closed?
  53 + @io.close if @io and not @io.closed?
54 54 rm_r @name
55 55 end
56 56
@@ -64,6 +64,23 @@
64 64
65 65 IO.read(@name).should == "io data"
66 66 end
  67 +
  68 + it "accepts an options Hash" do
  69 + FeatureGuard.stub!(:enabled?).and_return(true)
  70 + fd = new_fd @name, { :mode => 'w:utf-8' }
  71 + fd.should be_an_instance_of(Fixnum)
  72 +
  73 + @io = IO.new fd, fmode('w:utf-8')
  74 + @io.sync = true
  75 + @io.print "io data"
  76 +
  77 + IO.read(@name).should == "io data"
  78 + end
  79 +
  80 + it "raises an ArgumentError if the options Hash does not include :mode" do
  81 + FeatureGuard.stub!(:enabled?).and_return(true)
  82 + lambda { new_fd @name, { :encoding => "utf-8" } }.should raise_error(ArgumentError)
  83 + end
67 84 end
68 85
69 86 describe Object, "#new_io" do
@@ -95,6 +112,21 @@
95 112 @io.print "io data"
96 113 IO.read(@name).should == "io data"
97 114 end
  115 +
  116 + it "opens the IO for reading if passed { :mode => 'r' }" do
  117 + touch(@name) { |f| f.print "io data" }
  118 + @io = new_io @name, { :mode => "r" }
  119 + @io.read.should == "io data"
  120 + lambda { @io.puts "more data" }.should raise_error(IOError)
  121 + end
  122 +
  123 + it "opens the IO for writing if passed { :mode => 'w' }" do
  124 + @io = new_io @name, { :mode => "w" }
  125 + @io.sync = true
  126 +
  127 + @io.print "io data"
  128 + IO.read(@name).should == "io data"
  129 + end
98 130 end
99 131
100 132 describe Object, "#fmode" do

0 comments on commit 64ece89

Please sign in to comment.
Something went wrong with that request. Please try again.