File: each_spec.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (65 lines) | stat: -rw-r--r-- 2,010 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'

describe Virtus::AttributeSet, '#each' do
  subject(:attribute_set) { described_class.new(parent, attributes) }

  let(:name)       { :name }
  let(:attribute)  { Virtus::Attribute.build(String, :name => :name) }
  let(:attributes) { [ attribute ] }
  let(:parent)     { described_class.new }
  let(:yields)     { Set[] }

  context 'with no block' do
    it 'returns an enumerator when block is not provided' do
      expect(attribute_set.each).to be_kind_of(Enumerator)
    end

    it 'yields the expected attributes' do
      result = []
      attribute_set.each { |attribute| result << attribute }
      expect(result).to eql(attributes)
    end
  end

  context 'with a block' do
    subject { attribute_set.each { |attribute| yields << attribute } }

    context 'when the parent has no attributes' do
      it { is_expected.to equal(attribute_set) }

      it 'yields the expected attributes' do
        expect { subject }.to change { yields.dup }.
          from(Set[]).
          to(attributes.to_set)
      end
    end

    context 'when the parent has attributes that are not duplicates' do
      let(:parent_attribute) { Virtus::Attribute.build(String, :name => :parent_name) }
      let(:parent)           { described_class.new([ parent_attribute ])       }

      it { is_expected.to equal(attribute_set) }

      it 'yields the expected attributes' do
        result = []

        attribute_set.each { |attribute| result << attribute }

        expect(result).to eql([parent_attribute, attribute])
      end
    end

    context 'when the parent has attributes that are duplicates' do
      let(:parent_attribute) { Virtus::Attribute.build(String, :name => name) }
      let(:parent)           { described_class.new([ parent_attribute ]) }

      it { is_expected.to equal(attribute_set) }

      it 'yields the expected attributes' do
        expect { subject }.to change { yields.dup }.
          from(Set[]).
          to(Set[ attribute ])
      end
    end
  end
end