Pull out RichMessageExtractor

No need to have it embedded
  when it should be unit tested!
This commit is contained in:
Matt Bridges 2013-07-18 18:34:07 -05:00
parent 519e3df4d4
commit ecdade33c3
3 changed files with 83 additions and 29 deletions

View file

@ -0,0 +1,28 @@
class RichMessageExtractor
RICH_MESSAGE_FIELDS_REGEX = /([^>@]*)@?([^>]*)>?(.*)/
def initialize(message)
@message = message
end
def description
fields[1].strip
end
def context
fields[2].strip
end
def project
stripped = fields[3].strip
stripped.blank? ? nil : stripped
end
private
def fields
@message.match(RICH_MESSAGE_FIELDS_REGEX)
end
end

View file

@ -1,34 +1,5 @@
class TodoFromRichMessage
class RichMessageExtractor
RICH_MESSAGE_FIELDS_REGEX = /([^>@]*)@?([^>]*)>?(.*)/
def initialize(message)
@message = message
end
def description
fields[1].strip
end
def context
fields[2].strip
end
def project
stripped = fields[3].strip
stripped.blank? ? nil : stripped
end
private
def fields
@message.match(RICH_MESSAGE_FIELDS_REGEX)
end
end
attr_reader :user, :default_context_id, :description, :notes
def initialize(user, default_context_id, description, notes)

View file

@ -0,0 +1,55 @@
require 'test/unit'
require 'active_support/core_ext/object/blank'
require_relative '../../app/services/rich_message_extractor.rb'
class RichMessageExtractorTest < Test::Unit::TestCase
def test_message_with_all_options
message = "ohai@some-context>in-this-project"
extractor = RichMessageExtractor.new(message)
assert_equal "ohai", extractor.description
assert_equal "some-context", extractor.context
assert_equal "in-this-project", extractor.project
end
def test_message_without_project
message = "ohai @ some-context"
extractor = RichMessageExtractor.new(message)
assert_equal "ohai", extractor.description
assert_equal "some-context", extractor.context
assert_equal nil, extractor.project
end
def test_message_without_project
message = " ohai @ some-context"
extractor = RichMessageExtractor.new(message)
assert_equal "ohai", extractor.description
assert_equal "some-context", extractor.context
assert_equal nil, extractor.project
end
def test_message_without_project_or_context
message = "ohai"
extractor = RichMessageExtractor.new(message)
assert_equal "ohai", extractor.description
assert_equal "", extractor.context
assert_equal nil, extractor.project
end
def test_message_without_anything
message = ""
extractor = RichMessageExtractor.new(message)
assert_equal "", extractor.description
assert_equal "", extractor.context
assert_equal nil, extractor.project
end
def test_message_with_just_a_context
message = "@some-context"
extractor = RichMessageExtractor.new(message)
assert_equal "", extractor.description
assert_equal "some-context", extractor.context
assert_equal nil, extractor.project
end
end