Sha256: 776f86a2a1811a2662d60ba5c0e846d438c0b851b17ba388aa1120e2ee0d4dd4

Contents?: true

Size: 1.95 KB

Versions: 29

Compression:

Stored size: 1.95 KB

Contents

module Metacosm
  module TestHarness
    class GivenWhenThen < Struct.new(:given_events,:when_command,:then_event_class)
      include RSpec::Matchers

      def when(*commands)
        @when_commands ||= []
        commands.each do |command|
          @when_commands.push command
        end
        self
      end

      def expect_events(evts)
        @then_events = evts
        verify!
        self
      end

      def expect_query(query, to_find:)
        @query = query
        @expected_query_results = to_find
        verify!
        self
      end

      protected

      def verify!
        clean_slate!
        receive_events!
        fire_commands!

        validate_events!
        validate_query!

        self
      end

      private

      def clean_slate!
        PassiveRecord.drop_all
        Simulation.current.clear!
        self
      end

      def receive_events!
        unless self.given_events.nil?
          self.given_events.each do |evt| 
            sim.receive(evt, record: false)
          end
        end
        self
      end

      def fire_commands!
        unless @when_commands.nil?
          @when_commands.each do |cmd|
            sim.apply(cmd)
          end
        end
        self
      end

      def validate_events!
        if @then_event_class
          expect(@then_event_class).to eq(sim.events.last.class) 
        end

        if @then_events
          expect(@then_events).to match_array(sim.events) 
        end

        if @then_event_attrs
          @then_event_attrs.each do |k,v|
            expect(sim.events.last.send(k)).to eq(v)
          end
        end

        self
      end

      def validate_query!
        if @query
          expect(@query.execute).to eq(@expected_query_results)
        end
        self
      end

      def sim
        @sim ||= Simulation.current
      end
    end

    def given_no_activity
      GivenWhenThen.new
    end

    def given_events(events)
      GivenWhenThen.new(events)
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
metacosm-0.3.8 lib/metacosm/support/test_harness.rb
metacosm-0.3.7 lib/metacosm/support/test_harness.rb
metacosm-0.3.6 lib/metacosm/support/test_harness.rb
metacosm-0.3.5 lib/metacosm/support/test_harness.rb
metacosm-0.3.4 lib/metacosm/support/test_harness.rb
metacosm-0.3.3 lib/metacosm/support/test_harness.rb
metacosm-0.3.2 lib/metacosm/support/test_harness.rb
metacosm-0.3.1 lib/metacosm/support/test_harness.rb
metacosm-0.3.0 lib/metacosm/support/test_harness.rb
metacosm-0.2.14 lib/metacosm/support/test_harness.rb
metacosm-0.2.13 lib/metacosm/support/test_harness.rb
metacosm-0.2.12 lib/metacosm/support/test_harness.rb
metacosm-0.2.11 lib/metacosm/support/test_harness.rb
metacosm-0.2.10 lib/metacosm/support/test_harness.rb
metacosm-0.2.9 lib/metacosm/support/test_harness.rb
metacosm-0.2.8 lib/metacosm/support/test_harness.rb
metacosm-0.2.7 lib/metacosm/support/test_harness.rb
metacosm-0.2.6 lib/metacosm/support/test_harness.rb
metacosm-0.2.5 lib/metacosm/support/test_harness.rb
metacosm-0.2.4 lib/metacosm/support/test_harness.rb