lib/rantly/shrinks.rb in rantly-1.2.0 vs lib/rantly/shrinks.rb in rantly-2.0.0

- old
+ new

@@ -1,20 +1,20 @@ # Integer : shrink to zero class Integer def shrink shrunk = if self > 8 - self / 2 - elsif self > 0 - self - 1 - elsif self < -8 - (self + 1) / 2 - elsif self < 0 - self + 1 - else - 0 + self / 2 + elsif self > 0 + self - 1 + elsif self < -8 + (self + 1) / 2 + elsif self < 0 + self + 1 + else + 0 end - return shrunk + shrunk end def retry? false end @@ -25,24 +25,24 @@ end # String : shrink to "" class String def shrink - shrunk = self.dup - if self.size > 0 - idx = Random::rand(self.size) - shrunk[idx] = "" + shrunk = dup + unless empty? + idx = Random.rand(size) + shrunk[idx] = '' end - return shrunk + shrunk end def retry? false end def shrinkable? - self != "" + self != '' end end # Array where elements can be shrunk but not removed class Tuple @@ -62,51 +62,48 @@ def length @array.length end def size - self.length + length end def to_s - @array.to_s.insert(1, "T ") + @array.to_s.insert(1, 'T ') end def inspect - self.to_s + to_s end def each(&block) @array.each(&block) end - def array - return @array - end + attr_reader :array def shrink shrunk = @array.dup while @position >= 0 e = @array.at(@position) - if e.respond_to?(:shrinkable?) && e.shrinkable? - break - end + break if e.respond_to?(:shrinkable?) && e.shrinkable? + @position -= 1 end if @position >= 0 shrunk[@position] = e.shrink @position -= 1 end - return Tuple.new(shrunk) + Tuple.new(shrunk) end def retry? @position >= 0 end def shrinkable? - @array.any? {|e| e.respond_to?(:shrinkable?) && e.shrinkable? } + @array.any? { |e| e.respond_to?(:shrinkable?) && e.shrinkable? } end end # Array where the elements that can't be shrunk are removed class Deflating @@ -126,28 +123,26 @@ def length @array.length end def size - self.length + length end def to_s - @array.to_s.insert(1, "D ") + @array.to_s.insert(1, 'D ') end def inspect - self.to_s + to_s end def each(&block) @array.each(&block) end - def array - return @array - end + attr_reader :array def shrink shrunk = @array.dup if @position >= 0 e = @array.at(@position) @@ -156,11 +151,11 @@ else shrunk.delete_at(@position) end @position -= 1 end - return Deflating.new(shrunk) + Deflating.new(shrunk) end def retry? @position >= 0 end @@ -170,27 +165,27 @@ end end class Hash def shrink - if self.any?{|_,v| v.respond_to?(:shrinkable?) && v.shrinkable? } - key,_ = self.detect{|_,v| v.respond_to?(:shrinkable?) && v.shrinkable? } - clone = self.dup + if any? { |_, v| v.respond_to?(:shrinkable?) && v.shrinkable? } + key, = detect { |_, v| v.respond_to?(:shrinkable?) && v.shrinkable? } + clone = dup clone[key] = clone[key].shrink - return clone - elsif !self.empty? - key = self.keys.first - h2 = self.dup + clone + elsif !empty? + key = keys.first + h2 = dup h2.delete(key) - return h2 + h2 else - return self + self end end def shrinkable? - self.any?{|_,v| v.respond_to?(:shrinkable?) && v.shrinkable? } || - !self.empty? + any? { |_, v| v.respond_to?(:shrinkable?) && v.shrinkable? } || + !empty? end def retry? false end