Skip to content

Commit

Permalink
Fix all styling offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscz committed Sep 30, 2020
1 parent f44c716 commit def9cee
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ Rake::TestTask.new(:test) do |t|
t.warning = false
end


task default: [:rubocop, :spec]
44 changes: 21 additions & 23 deletions lib/active_event_store/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@

module ActiveEventStore
module TestHelper

class EventPublishedMatcher
attr_reader :attributes,
:matching_events
:matching_events

def initialize(expected_event_class, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, refute: false)
@event_class = expected_event_class
@store = store || ActiveEventStore.event_store
@attributes = with
@refute = refute
@store = store || ActiveEventStore.event_store
@attributes = with
@refute = refute

count_expectations = {
exactly: exactly,
at_most: at_most,
at_least: at_least,
exactly: exactly,
at_most: at_most,
at_least: at_least
}.reject { |_, v| v.nil? }

if count_expectations.length > 1
raise ArgumentError("Only one of :exactly, :at_least or :at_most can be specified")
elsif count_expectations.length == 0
@count_expectation_kind = :at_least
@expected_count = 1
@expected_count = 1
else
@count_expectation_kind = count_expectations.keys.first
@expected_count = count_expectations.values.first
@expected_count = count_expectations.values.first
end
end

Expand Down Expand Up @@ -60,7 +59,7 @@ def matches?(block)

expectations << "with attributes #{attributes.inspect}" unless attributes.nil?

expectations << expectations.pop + ', but'
expectations << expectations.pop + ", but"

expectations << if @unmatching_events.any?
@unmatching_events.inject("published the following events instead:") do |msg, unmatching_event|
Expand All @@ -70,7 +69,7 @@ def matches?(block)
"hasn't published anything"
end

return expectations.join(' ')
return expectations.join(" ")
end

nil
Expand Down Expand Up @@ -133,37 +132,36 @@ def count_mismatch_message(actual_count)
raise ArgumentError, "Unrecognized expectation kind: #{@count_expectation_kind}"
end
end

end

# Asserts that the given event was published `exactly`, `at_least` or `at_most` number of times
# to a specific `store` `with` a particular hash of attributes.
def assert_event_published(expected_event, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, &block)
matcher = EventPublishedMatcher.new(
expected_event,
store: store,
with: with,
exactly: exactly,
store: store,
with: with,
exactly: exactly,
at_least: at_least,
at_most: at_most,
at_most: at_most
)

if (msg = matcher.matches?(block))
fail(msg)
end

return matcher.matching_events
matcher.matching_events
end

def refute_event_published(expected_event, store: nil, with: nil, exactly: nil, at_least: nil, at_most: nil, &block)
matcher = EventPublishedMatcher.new(
expected_event,
store: store,
with: with,
exactly: exactly,
store: store,
with: with,
exactly: exactly,
at_least: at_least,
at_most: at_most,
refute: true
at_most: at_most,
refute: true
)

if (msg = matcher.matches?(block))
Expand Down
30 changes: 15 additions & 15 deletions test/active_event_store/test_helper_test.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require 'test_helper'
require 'active_event_store/test_helper'
require_relative '../../spec/support/test_events'
# frozen_string_literal: true

require "test_helper"
require "active_event_store/test_helper"
require_relative "../../spec/support/test_events"

class TestHelperTest < Minitest::Test
include ActiveEventStore::TestHelper
attr_reader :event_class,
:event,
:event2
:event,
:event2

def setup
super
@event_class = ActiveEventStore::TestEvent
@event = event_class.new(user_id: 25, action_type: "birth")
@event2 = event_class.new(user_id: 1, action_type: "death")
@event = event_class.new(user_id: 25, action_type: "birth")
@event2 = event_class.new(user_id: 1, action_type: "death")
end

def test_assert_event_published
Expand All @@ -22,17 +24,16 @@ def test_assert_event_published

assert events.length == 1
assert_includes events, event

end

def test_assert_event_published_with_one_attribute
assert_event_published(ActiveEventStore::TestEvent, with: { user_id: 25 }) do
assert_event_published(ActiveEventStore::TestEvent, with: {user_id: 25}) do
ActiveEventStore.publish(event)
end
end

def test_assert_event_published_with_multiple_attributes
assert_event_published(ActiveEventStore::TestEvent, with: { user_id: 25, action_type: "birth" }) do
assert_event_published(ActiveEventStore::TestEvent, with: {user_id: 25, action_type: "birth"}) do
ActiveEventStore.publish(event)
end
end
Expand All @@ -47,13 +48,12 @@ def test_assert_event_published_with_count
# == Failure cases

def test_assert_event_published_with_no_events

e = assert_raises do
assert_event_published(ActiveEventStore::TestEvent, exactly: 1) do
end
end

assert_match /exactly 1 test_event to have been published, but hasn't published anything/, e.message
assert_match(/exactly 1 test_event to have been published, but hasn't published anything/, e.message)
end

def test_assert_event_published_with_class_mismatch
Expand All @@ -63,16 +63,16 @@ def test_assert_event_published_with_class_mismatch
end
end

assert_match /at least 1 active_event_store.another_test_event to have been published.*published the following events instead/, e.message
assert_match(/at least 1 active_event_store.another_test_event to have been published.*published the following events instead/, e.message)
end

def test_assert_event_published_with_attribute_mismatch
e = assert_raises do
assert_event_published(ActiveEventStore::TestEvent, with: { user_id: 25, action_type: "death" }) do
assert_event_published(ActiveEventStore::TestEvent, with: {user_id: 25, action_type: "death"}) do
ActiveEventStore.publish(event)
end
end

assert_match /at least 1 test_event.*with attributes.*:user_id=>25.*:action_type=>"death".*but published(.|[\n])*:user_id=>25.*:action_type=>"birth"/s, e.message
assert_match(/at least 1 test_event.*with attributes.*:user_id=>25.*:action_type=>"death".*but published(.|[\n])*:user_id=>25.*:action_type=>"birth"/s, e.message)
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

ENV["RAILS_ENV"] = "test"

require 'bundler'
require "bundler"
Bundler.require :default, :test

begin
Expand Down

0 comments on commit def9cee

Please sign in to comment.