Sha256: 015c6074cd43f4ed9d9b8f408bd05b4ad2e6f406d9d646b4ab6f566e9474e968

Contents?: true

Size: 1.35 KB

Versions: 15

Compression:

Stored size: 1.35 KB

Contents

require_relative "helper"

scope do
  class Contact < Ohm::Model
    attribute :name
  end

  setup do
    john = Contact.create(name: "John Doe")
    jane = Contact.create(name: "Jane Doe")

    [john, jane]
  end

  test "Set#size doesn't do each" do
    set = Contact.all

    def set.each
      raise "Failed"
    end

    assert_equal 2, set.size
  end

  test "Set#each as an Enumerator" do |john, jane|
    enum = Contact.all.each

    enum.each do |c|
      assert c == john || c == jane
    end
  end

  test "select" do |john, jane|
    assert_equal 2, Contact.all.count
    assert_equal [john], Contact.all.select { |c| c.id == john.id }
  end
end

scope do
  class Comment < Ohm::Model
  end

  class Post < Ohm::Model
    list :comments, :Comment
  end

  setup do
    c1 = Comment.create
    c2 = Comment.create

    post = Post.create
    post.comments.push(c1)
    post.comments.push(c2)

    [post, c1, c2]
  end

  test "List#select" do |post, c1, c2|
    assert_equal [c1], post.comments.select { |comment| comment == c1 }
  end

  test "List#each as Enumerator" do |post, c1, c2|
    enum = post.comments.each

    enum.each do |comment|
      assert comment == c1 || comment == c2
    end
  end

  test "List#size doesn't do each" do |post, c1, c2|
    list = post.comments

    def list.each
      raise "Failed"
    end

    assert_equal 2, list.size
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
ohm_util-0.1 test/enumerable.rb
ohm-3.1.1 test/enumerable.rb
ohm-3.1.0 test/enumerable.rb
ohm-3.0.3 test/enumerable.rb
ohm-3.0.2 test/enumerable.rb
ohm-3.0.1 test/enumerable.rb
ohm-3.0.0 test/enumerable.rb
ohm-2.3.0 test/enumerable.rb
ohm-2.2.1 test/enumerable.rb
sohm-0.0.1 test/enumerable.rb
ohm-2.2.0 test/enumerable.rb
ohm-2.1.0 test/enumerable.rb
ohm-2.0.1 test/enumerable.rb
ohm-2.0.0 test/enumerable.rb
ohm-2.0.0.rc2 test/enumerable.rb