diff --git a/lib/elife_parser/text.rb b/lib/elife_parser/text.rb index 7391519..cbf7ad8 100644 --- a/lib/elife_parser/text.rb +++ b/lib/elife_parser/text.rb @@ -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 @@ -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 diff --git a/spec/elife_parser/text_spec.rb b/spec/elife_parser/text_spec.rb index 50f7efd..9749d9b 100644 --- a/spec/elife_parser/text_spec.rb +++ b/spec/elife_parser/text_spec.rb @@ -9,10 +9,57 @@ 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 @@ -20,11 +67,10 @@ 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