1- # frozen_string_literal: true
1+ # frozen_string_literal: false
2+
23describe Stupidedi ::Reader ::Pointer do
34 using Stupidedi ::Refinements
45
5- def pointer ( value )
6- Stupidedi ::Reader ::Pointer . build ( value )
6+ def pointer ( string , frozen : nil )
7+ frozen = [ true , false ] . sample if frozen . nil?
8+ string . freeze if frozen and not string . frozen?
9+ string = string . dup if not frozen and string . frozen?
10+ Stupidedi ::Reader ::Pointer . build ( string )
711 end
812
13+
914 def pointer_ ( *args )
1015 Stupidedi ::Reader ::Pointer . new ( args )
1116 end
@@ -23,12 +28,12 @@ def suffix(pointer)
2328
2429 describe ".build" do
2530 context "when value is a String" do
26- specify { expect ( pointer ( "abc " ) ) . to be_a ( Stupidedi ::Reader ::Pointer ) }
31+ specify { expect ( pointer ( "111 " ) ) . to be_a ( Stupidedi ::Reader ::Pointer ) }
2732
2833 allocation do
2934 ignore = nil
30- storage = [ 1 , 1 , 1 ]
31- expect { ignore = pointer ( storage ) } . to allocate ( Stupidedi ::Reader ::Pointer => 1 )
35+ storage = "111"
36+ expect { ignore = pointer ( storage , frozen : true ) } . to allocate ( Stupidedi ::Reader ::Substring => 1 )
3237 end
3338 end
3439
@@ -37,7 +42,7 @@ def suffix(pointer)
3742
3843 allocation do
3944 storage = [ 1 , 2 ]
40- expect { pointer ( storage ) } . to allocate ( Stupidedi ::Reader ::Pointer => 1 )
45+ expect { pointer ( storage , frozen : true ) } . to allocate ( Stupidedi ::Reader ::Pointer => 1 )
4146 end
4247 end
4348
@@ -76,24 +81,39 @@ def suffix(pointer)
7681
7782 describe "#reify" do
7883 context "when storage is frozen" do
79- let ( :abcdef ) { pointer ( "abcdef" . freeze ) }
84+ let ( :p ) { pointer ( "abcdef" , frozen : true ) }
8085
8186 context "and storage spans entire storage" do
82- allocation { p = abcdef ; expect { p . send ( :reify ) } . to allocate ( String : 0 ) }
83- allocation { p = abcdef ; expect { p . send ( :reify , false ) } . to allocate ( String : 0 ) }
87+ allocation { p ; expect { p . send ( :reify ) } . to allocate ( String : 0 ) }
88+ allocation { p ; expect { p . send ( :reify , false ) } . to allocate ( String : 0 ) }
8489
8590 context "but always_allocate is true" do
86- allocation { p = abcdef ; expect { p . send ( :reify , true ) } . to allocate ( String : 1 ) }
91+ allocation { p ; expect { p . send ( :reify , true ) } . to allocate ( String : 1 ) }
8792 end
8893 end
8994
9095 context "and storage does not span entire storage" do
91- allocate { p = abcdef . drop ( 1 ) ; expect { p . send ( :reify ) } . to allocate ( String : 1 ) }
92- allocate { p = abcdef . take ( 1 ) ; expect { p . send ( :reify ) } . to allocate ( String : 1 ) }
96+ allocate { p = p . drop ( 1 ) ; expect { p . send ( :reify ) } . to allocate ( String : 1 ) }
97+ allocate { p = p . take ( 1 ) ; expect { p . send ( :reify ) } . to allocate ( String : 1 ) }
9398 end
9499 end
95100
96- todo "when storage is not frozen" do
101+ context "when storage is not frozen" do
102+ let ( :p ) { pointer ( "abcdef" , frozen : false ) }
103+
104+ context "and storage spans entire storage" do
105+ allocation { p ; expect { p . send ( :reify ) } . to allocate ( String : 1 ) }
106+ allocation { p ; expect { p . send ( :reify , false ) } . to allocate ( String : 1 ) }
107+
108+ context "but always_allocate is true" do
109+ allocation { p ; expect { p . send ( :reify , true ) } . to allocate ( String : 1 ) }
110+ end
111+ end
112+
113+ context "and storage does not span entire storage" do
114+ allocate { p = p . drop ( 1 ) ; expect { p . send ( :reify ) } . to allocate ( String : 1 ) }
115+ allocate { p = p . take ( 1 ) ; expect { p . send ( :reify ) } . to allocate ( String : 1 ) }
116+ end
97117 end
98118 end
99119
@@ -159,11 +179,10 @@ def suffix(pointer)
159179 end
160180
161181 describe "+" do
162- let ( :a ) { pointer ( "abcdefghi" . dup ) }
163-
164182 context "when argument is a non-pointer value" do
165183 context "when pointer suffix starts with argument" do
166184 specify do
185+ a = pointer ( "abcdefghi" )
167186 b = a . drop ( 3 ) . take ( 3 )
168187 c = "gh"
169188
@@ -178,15 +197,17 @@ def suffix(pointer)
178197 end
179198
180199 allocation do
200+ a = pointer ( "abcdefghi" )
181201 b = a . drop ( 3 ) . take ( 3 )
182202 c = "gh"
183203 expect ( suffix ( b ) ) . to start_with ( c )
184- expect { b + c } . to allocate ( String : 0 , a . class => 1 )
204+ expect { b + c } . to allocate ( b . class => 1 )
185205 end
186206 end
187207
188208 context "when argument is pointer suffix plus more" do
189209 specify do
210+ a = pointer ( "abcdefghi" )
190211 b = a . drop ( 3 ) . take ( 3 )
191212 c = "ghijkl"
192213
@@ -203,6 +224,7 @@ def suffix(pointer)
203224 end
204225
205226 allocation do
227+ a = pointer ( "abcdefghi" )
206228 b = a . drop ( 3 ) . take ( 3 )
207229 c = "ghijkl"
208230 expect ( c ) . to start_with ( suffix ( b ) )
@@ -213,6 +235,7 @@ def suffix(pointer)
213235
214236 context "when argument is not pointer suffix" do
215237 specify do
238+ a = pointer ( "abcdefghi" )
216239 b = a . take ( 6 )
217240 c = "xxx"
218241
@@ -228,6 +251,7 @@ def suffix(pointer)
228251 end
229252
230253 allocation do
254+ a = pointer ( "abcdefghi" )
231255 b = a . take ( 6 )
232256 c = "xxx"
233257 expect ( a . storage ) . to be_frozen
@@ -239,6 +263,7 @@ def suffix(pointer)
239263 context "when argument is a string pointer" do
240264 context "when pointer suffix starts with argument" do
241265 specify do
266+ a = pointer ( "abcdefghi" )
242267 b = a . drop ( 3 ) . take ( 3 )
243268 c = pointer ( "gh" )
244269
@@ -254,15 +279,17 @@ def suffix(pointer)
254279 end
255280
256281 allocation do
282+ a = pointer ( "abcdefghi" )
257283 b = a . drop ( 3 ) . take ( 3 )
258284 c = pointer ( "gh" )
259285 expect ( suffix ( b ) ) . to start_with ( c )
260- expect { b + c } . to allocate ( a . class => 1 )
286+ expect { b + c } . to allocate ( b . class => 1 )
261287 end
262288 end
263289
264290 context "when argument is pointer suffix plus more" do
265291 specify do
292+ a = pointer ( "abcdefghi" )
266293 b = a . drop ( 3 ) . take ( 3 )
267294 c = pointer ( "ghijkl" )
268295
@@ -279,6 +306,7 @@ def suffix(pointer)
279306 end
280307
281308 allocation do
309+ a = pointer ( "abcdefghi" )
282310 b = a . drop ( 3 ) . take ( 3 )
283311 c = pointer ( "ghijkl" )
284312 expect ( c ) . to start_with ( suffix ( b ) )
@@ -289,6 +317,7 @@ def suffix(pointer)
289317
290318 context "when argument is not pointer suffix" do
291319 specify do
320+ a = pointer ( "abcdefghi" )
292321 b = a . take ( 6 )
293322 c = pointer ( "xxx" )
294323
@@ -304,51 +333,35 @@ def suffix(pointer)
304333 end
305334
306335 allocation do
336+ a = pointer ( "abcdefghi" )
307337 b = a . take ( 6 )
308- c = pointer ( "xxx" )
338+ c = pointer ( "xxx" , frozen : true )
309339 expect ( suffix ( b ) ) . to_not start_with ( c )
310- expect { b + c } . to allocate ( String : 1 )
340+ expect { b + c } . to allocate ( String : 1 + ( c . storage . frozen? && 0 || 1 ) )
341+ end
342+
343+ allocation do
344+ a = pointer ( "abcdefghi" )
345+ b = a . take ( 6 )
346+ c = pointer ( "xxx" , frozen : false )
347+ expect ( suffix ( b ) ) . to_not start_with ( c )
348+ expect { b + c } . to allocate ( String : 1 + ( c . storage . frozen? && 0 || 1 ) )
311349 end
312350 end
313351 end
314352 end
315353
316- describe "#head" do
317- end
318-
319- describe "#last" do
320- end
321-
322- describe "#defined_at?" do
323- end
324-
325- describe "#at" do
326- end
327-
328- describe "#tail" do
329- end
330-
331- describe "#[]" do
332- end
333-
334- describe "#drop" do
335- end
336-
337- describe "#drop!" do
338- end
339-
340- describe "take" do
341- end
342-
343- context "#take!" do
344- end
345-
346- describe "#drop_take" do
347- end
348-
349- describe "#split_at" do
350- end
351-
352- describe ".build" do
353- end
354+ todo "#head"
355+ todo "#last"
356+ todo "#defined_at?"
357+ todo "#at"
358+ todo "#tail"
359+ todo "#[]"
360+ todo "#drop"
361+ todo "#drop!"
362+ todo "take"
363+ todo "#take!"
364+ todo "#drop_take"
365+ todo "#split_at"
366+ todo ".build"
354367end
0 commit comments