Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: ✅ Add tests and check type implementation #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/elife_parser/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ class Text
attr_reader :content

def initialize content
@content = content
@content = Text.validate_content_type(content)
end

# Check content type and raise exception if it isn't a String.
def self.validate_content_type(content)
unless content.is_a? String
raise ArgumentError, 'Invalid content type'
end

content
end

def sanitized_text
Expand Down Expand Up @@ -47,7 +56,7 @@ def modified_text_with_plus

final_text = final_text.gsub(/\.+[\s+\$]/, " ")

# remove white spaces
# reduce white spaces
final_text.gsub(/\s\s+/, "\s")
end
end
Expand Down
52 changes: 49 additions & 3 deletions spec/elife_parser/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,68 @@
end
end

describe "Text creation" do

# Helper function to create and test Text with given content.
def create_and_check_text(content)
text = described_class.new(content)

expect(text.is_a? ElifeParser::Text).to eq(true)
expect(text.content).to eq(content)
end

it "returns Text for valid content type" do
# Normal text
create_and_check_text('some text.')

# Text with especial characters.
create_and_check_text('@some #text')

# Only blank spaces.
create_and_check_text(' ')

# Only special characters.
create_and_check_text('@@@&&&###')
end

it "returns failure message for invalid content type" do
expect { described_class.new(3) }.to raise_error(ArgumentError, 'Invalid content type')
expect { described_class.new(:hello) }.to raise_error(ArgumentError, 'Invalid content type')
expect { described_class.new(["some text"]) }.to raise_error(ArgumentError, 'Invalid content type')
expect { described_class.new({name: "David"}) }.to raise_error(ArgumentError, 'Invalid content type')
end
end

describe "modified_text_without_special_caracters" do

# TODO: refactor this test to only remove @ and #.
it "should remove @ and #" do
expect(subject.modified_text_without_special_caracters).to eql(" manoel quirino ")
text = described_class.new("@manoel #quirino")
expect(text.modified_text_without_special_caracters).to eql(" manoel quirino ")

text = described_class.new("@manoel 🔥& #quirino")
expect(text.modified_text_without_special_caracters).to eql(" manoel :fire: & quirino ")
end

# # Correct tests.
# it "should remove @ and #", :aggregate_failures do
# text = described_class.new("@manoel #quirino")
# expect(text.modified_text_without_special_caracters).to eql("manoel quirino")

# text = described_class.new("@manoel 🔥& #quirino")
# expect(text.modified_text_without_special_caracters).to eql("manoel 🔥& quirino")
# end
end

describe "modified_text" do
it "should remove special caracters and change emojis" do
expect(described_class.new("@livia.vilaca ##opa/&eba 🔥 🐷🐽.$ :? .$ - -a º ρε👽😀☂❤华み원❤")
.modified_text).to eql(" @livia.vilaca ##opa/&eba :fire: :pig: :pig_nose: a ρε :alien: :grinning: :heart: :heart: ")
end

it "should parse unicode heart and convert hearts" do
expect(described_class.new("❤️ e 🔥 e ❤")
.modified_text).to eql(" :heart: e :fire: e :heart: ")
end
end


end