summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/buffer/readonly_spec.rb
blob: 0014a876ed743ee594da156c2aeac6978ad3d57a (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require_relative '../../../spec_helper'

describe "IO::Buffer#readonly?" do
  after :each do
    @buffer&.free
    @buffer = nil
  end

  context "with a buffer created with .new" do
    it "is false for an internal buffer" do
      @buffer = IO::Buffer.new(4, IO::Buffer::INTERNAL)
      @buffer.readonly?.should be_false
    end

    it "is false for a mapped buffer" do
      @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED)
      @buffer.readonly?.should be_false
    end
  end

  context "with a file-backed buffer created with .map" do
    it "is false for a writable mapping" do
      File.open(__FILE__, "r+") do |file|
        @buffer = IO::Buffer.map(file)
        @buffer.readonly?.should be_false
      end
    end

    it "is true for a readonly mapping" do
      File.open(__FILE__, "r") do |file|
        @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY)
        @buffer.readonly?.should be_true
      end
    end

    ruby_version_is "3.3" do
      it "is false for a private mapping" do
        File.open(__FILE__, "r") do |file|
          @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::PRIVATE)
          @buffer.readonly?.should be_false
        end
      end
    end
  end

  context "with a String-backed buffer created with .for" do
    it "is true for a buffer created without a block" do
      @buffer = IO::Buffer.for(+"test")
      @buffer.readonly?.should be_true
    end

    it "is false for a buffer created with a block" do
      IO::Buffer.for(+"test") do |buffer|
        buffer.readonly?.should be_false
      end
    end

    it "is true for a buffer created with a block from a frozen string" do
      IO::Buffer.for(-"test") do |buffer|
        buffer.readonly?.should be_true
      end
    end
  end

  ruby_version_is "3.3" do
    context "with a String-backed buffer created with .string" do
      it "is false" do
        IO::Buffer.string(4) do |buffer|
          buffer.readonly?.should be_false
        end
      end
    end
  end

  # This seems to be the only flag propagated from the source buffer to the slice.
  context "with a slice of a buffer" do
    context "created with .new" do
      it "is false when slicing an internal buffer" do
        @buffer = IO::Buffer.new(4)
        @buffer.slice.readonly?.should be_false
      end

      it "is false when slicing a mapped buffer" do
        @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED)
        @buffer.slice.readonly?.should be_false
      end
    end

    context "created with .map" do
      it "is false when slicing a read-write file-backed buffer" do
        File.open(__FILE__, "r+") do |file|
          @buffer = IO::Buffer.map(file)
          @buffer.slice.readonly?.should be_false
        end
      end

      it "is true when slicing a readonly file-backed buffer" do
        File.open(__FILE__, "r") do |file|
          @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY)
          @buffer.slice.readonly?.should be_true
        end
      end

      ruby_version_is "3.3" do
        it "is false when slicing a private file-backed buffer" do
          File.open(__FILE__, "r") do |file|
            @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::PRIVATE)
            @buffer.slice.readonly?.should be_false
          end
        end
      end
    end

    context "created with .for" do
      it "is true when slicing a buffer created without a block" do
        @buffer = IO::Buffer.for(+"test")
        @buffer.slice.readonly?.should be_true
      end

      it "is false when slicing a buffer created with a block" do
        IO::Buffer.for(+"test") do |buffer|
          buffer.slice.readonly?.should be_false
        end
      end

      it "is true when slicing a buffer created with a block from a frozen string" do
        IO::Buffer.for(-"test") do |buffer|
          buffer.slice.readonly?.should be_true
        end
      end
    end

    ruby_version_is "3.3" do
      context "created with .string" do
        it "is false" do
          IO::Buffer.string(4) do |buffer|
            buffer.slice.readonly?.should be_false
          end
        end
      end
    end
  end
end