Sha256: fc9f9c83bdbcc84e6c5dc98bf83d67d2e29ead4d1c59fe1a66138af5f6b4dd68

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

# encoding: utf-8
require 'spec_helper'
require 'attr_enumerable'

describe AttrEnumerable do
  context :compact_attr do
    class AttrEnumerablePerson
      attr_reader :name, :age
      def initialize(name, age)
        @name, @age = name, age
      end
    end

    class AttrEnumerablePersons
      attr_reader :attr_enumerable_persons
      include AttrEnumerable
      def initialize(attr_enumerable_persons = [])
        @attr_enumerable_persons = attr_enumerable_persons
      end

      def <<(person)
        @attr_enumerable_persons << attr_enumerable_persons
      end
    end

    cases = [
      {
        case_no: 1,
        case_title: 'name case',
        klass: AttrEnumerablePersons.new(
          [
            AttrEnumerablePerson.new('tanaka', 84),
            AttrEnumerablePerson.new(nil, 99),
            AttrEnumerablePerson.new('suzuki', nil)
          ]
        ),
        method: :compact_name,
        expected: ['tanaka', 'suzuki']
      },
      {
        case_no: 2,
        case_title: 'age case',
        klass: AttrEnumerablePersons.new(
          [
            AttrEnumerablePerson.new('tanaka', 84),
            AttrEnumerablePerson.new(nil, 99),
            AttrEnumerablePerson.new('suzuki', nil)
          ]
        ),
        method: :compact_age,
        expected: [84, 99]
      },
      {
        case_no: 3,
        case_title: 'empty case',
        klass: AttrEnumerablePersons.new([]),
        method: :compact_name,
        expected: []
      }
    ]

    cases.each do |c|
      it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
        begin
          case_before c

          # -- given --
          # nothing

          # -- when --
          actual = c[:klass].send c[:method]

          # -- then --
          expect(actual).to eq(c[:expected])
        ensure
          case_after c
        end
      end

      def case_before(c)
        # implement each case before
      end

      def case_after(c)
        # implement each case after
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.150 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.149 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.148 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.147 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.146 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.145 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.144 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.143 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.142 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.141 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.140 spec/attr_enumerable/compact_attr_spec.rb
tbpgr_utils-0.0.139 spec/attr_enumerable/compact_attr_spec.rb