funktional 3.0.6 → 3.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{funktional}
5
- s.version = "3.0.6"
5
+ s.version = "3.1.6"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Brent Greeff and Felix Clack"]
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
  "lib/funktional.rb",
15
15
  "lib/funktional/assertion.rb",
16
16
  "lib/funktional/assigned_assertion.rb",
17
+ "lib/funktional/email.rb",
17
18
  "lib/funktional/attribute_test_helper.rb",
18
19
  "lib/funktional/context.rb",
19
20
  "lib/funktional/context/assigned_should_block.rb",
@@ -47,6 +48,7 @@ Gem::Specification.new do |s|
47
48
  "lib/funktional.rb",
48
49
  "lib/funktional/assertion.rb",
49
50
  "lib/funktional/assigned_assertion.rb",
51
+ "lib/funktional/email.rb",
50
52
  "lib/funktional/attribute_test_helper.rb",
51
53
  "lib/funktional/context.rb",
52
54
  "lib/funktional/context/assigned_should_block.rb",
@@ -8,6 +8,7 @@ require 'funktional/test_class_methods'
8
8
  require 'funktional/assertion'
9
9
  require 'funktional/recursive_assertion'
10
10
  require 'funktional/assigned_assertion'
11
+ require 'funktional/email'
11
12
  require 'funktional/email_assertion'
12
13
  require 'funktional/flashed_assertion'
13
14
  require 'funktional/model_assertions'
@@ -0,0 +1,76 @@
1
+ module Funktional
2
+ class Email
3
+
4
+ def self.find_closest(emails, expected)
5
+ emails = emails.map { |email| new(email, expected) }
6
+ emails.sort.first
7
+ end
8
+
9
+ attr_accessor :original
10
+ attr_accessor :matches
11
+
12
+ def initialize(email, expected)
13
+ @original = email
14
+ @expected = expected
15
+ calculate_matches
16
+ end
17
+
18
+ def calculate_matches
19
+ @matches = {}
20
+ @expected.map { |expect| self.send "check_#{expect.first}" }
21
+ end
22
+
23
+ def <=>(other)
24
+ other.total_matches <=> total_matches
25
+ end
26
+
27
+ def total_matches
28
+ if @matches.has_key? :containing
29
+ @matches.size + (@matches[:containing].size - 1)
30
+ else
31
+ @matches.size
32
+ end
33
+ end
34
+
35
+ def check_from
36
+ @matches[:from] = from if from.present? and (@expected[:from] == from)
37
+ end
38
+
39
+ def check_to
40
+ @matches[:to] = to if to.present? and (@expected[:to] == to)
41
+ end
42
+
43
+ def check_subject
44
+ if subject.present? and (@expected[:subject] == subject)
45
+ @matches[:subject] = subject
46
+ end
47
+ end
48
+
49
+ def check_containing
50
+ should_contain = @expected[:containing]
51
+ should_contain = [should_contain] unless should_contain.is_a? Array
52
+ matches = []
53
+
54
+ should_contain.each do |should_i|
55
+ matches << should_i if body =~ /#{Regexp.escape(should_i)}/
56
+ end
57
+ @matches[:containing] = matches if matches.any?
58
+ end
59
+
60
+ def from
61
+ @original.from[0]
62
+ end
63
+
64
+ def to
65
+ @original.to[0]
66
+ end
67
+
68
+ def subject
69
+ @original.subject
70
+ end
71
+
72
+ def body
73
+ @original.body.raw_source
74
+ end
75
+ end
76
+ end
@@ -1,11 +1,12 @@
1
1
  module Funktional
2
2
  class EmailAssertion < Funktional::Assertion
3
+
3
4
  def initialize(expectations)
4
5
  if ActionMailer::Base.deliveries.size < 1
5
6
  flunk 'No emails have been sent'
6
7
  end
7
8
 
8
- email = ActionMailer::Base.deliveries.last
9
+ email = Email.find_closest(ActionMailer::Base.deliveries, expectations)
9
10
 
10
11
  expectations.each_key do |key|
11
12
  case key
@@ -16,7 +17,7 @@ module Funktional
16
17
  when :subject
17
18
  then assert_equal expectations[:subject], email.subject
18
19
  when :containing
19
- then check_containing(expectations[:containing], email.body.raw_source)
20
+ then check_containing(expectations[:containing], email.body)
20
21
  else
21
22
  flunk "Assertion key: [#{key}] not recognised"
22
23
  end
@@ -27,14 +28,14 @@ module Funktional
27
28
  if email_from.nil?
28
29
  flunk 'email is missing a [from]'
29
30
  end
30
- assert_equal expected_from, email_from[0]
31
+ assert_equal expected_from, email_from
31
32
  end
32
33
 
33
34
  def check_to(expected_to, email_to)
34
35
  if email_to.nil?
35
36
  flunk 'email is missing a [to]'
36
37
  end
37
- assert_equal expected_to, email_to[0]
38
+ assert_equal expected_to, email_to
38
39
  end
39
40
 
40
41
  def check_containing(should_contain, body)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: funktional
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
- - 0
8
+ - 1
9
9
  - 6
10
- version: 3.0.6
10
+ version: 3.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brent Greeff and Felix Clack
@@ -30,6 +30,7 @@ extra_rdoc_files:
30
30
  - lib/funktional.rb
31
31
  - lib/funktional/assertion.rb
32
32
  - lib/funktional/assigned_assertion.rb
33
+ - lib/funktional/email.rb
33
34
  - lib/funktional/attribute_test_helper.rb
34
35
  - lib/funktional/context.rb
35
36
  - lib/funktional/context/assigned_should_block.rb
@@ -62,6 +63,7 @@ files:
62
63
  - lib/funktional.rb
63
64
  - lib/funktional/assertion.rb
64
65
  - lib/funktional/assigned_assertion.rb
66
+ - lib/funktional/email.rb
65
67
  - lib/funktional/attribute_test_helper.rb
66
68
  - lib/funktional/context.rb
67
69
  - lib/funktional/context/assigned_should_block.rb