Sha256: dbd3a89bec5794e8eede10d4c5ab8337c2086b9e65e58efbad3f48eb9d5cdf06

Contents?: true

Size: 1.95 KB

Versions: 10

Compression:

Stored size: 1.95 KB

Contents

require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestAccumulator < Test::Unit::TestCase
  
  def test_accumulator_saves_objs
    a = Tracksperanto::Accumulator.new
    values = [3, {:foo => "bar"}, "foo"]
    values.map(&a.method(:push))
    
    assert_equal 3, a.size
    assert_equal values, a.map{|e| e }, "Should return the same elements from the storage"
  end
  
  def test_accumulator_saves_shitload_of_objs
    a = Tracksperanto::Accumulator.new
    50_000.times { a.push("A string" => rand) }
    assert_equal 50_000, a.size
  end
  
  def test_accumulator_saves_shitload_of_objs
    a = Tracksperanto::Accumulator.new
    4.times { a.push("A \tstring") }
    a.each {|e| assert_equal "A \tstring", e }
  end
  
  def test_accumulator_empty
    a = Tracksperanto::Accumulator.new
    assert a.empty?
    a.push(1)
    assert !a.empty?
  end
  
  def test_accumulator_supports_nested_iteration
    a = Tracksperanto::Accumulator.new
    ("A".."Z").each{|e| a << e}
    
    accumulated = []
    seen_g = false
    a.each do | first_level |
      if first_level == "G"
        seen_g = true
        # Force a nested iteration and break it midway
        a.each do | second_level |
          accumulated.push(second_level)
          break if second_level == "E"
        end
      elsif seen_g
        assert_equal "H", first_level
        return
      end
    end
  end
  
  def test_random_access
    a = Tracksperanto::Accumulator.new
    letters = ("A".."Z").map{|e| "#{e}\r\nWow!"}.to_a
    letters.map(&a.method(:push))
    
    assert_equal "B\r\nWow!", a[1]
    assert_equal "E\r\nWow!", a[4]
  end
  
  def test_clear_calls_close_on_buffer
    io = Tracksperanto::BufferIO.new
    flexmock(io).should_receive(:close!)
    flexmock(Tracksperanto::BufferIO).should_receive(:new).once.and_return(io)
    
    a = Tracksperanto::Accumulator.new
    40.times { a.push("A string" => rand) }
    assert_equal 40, a.size
    a.clear
    assert_equal 0, a.size
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
tracksperanto-2.6.3 test/test_accumulator.rb
tracksperanto-2.6.2 test/test_accumulator.rb
tracksperanto-2.6.1 test/test_accumulator.rb
tracksperanto-2.6.0 test/test_accumulator.rb
tracksperanto-2.5.0 test/test_accumulator.rb
tracksperanto-2.4.1 test/test_accumulator.rb
tracksperanto-2.3.3 test/test_accumulator.rb
tracksperanto-2.3.2 test/test_accumulator.rb
tracksperanto-2.3.1 test/test_accumulator.rb
tracksperanto-2.3.0 test/test_accumulator.rb