Skip to content

Commit

Permalink
Add expect to specs and use latest ruby hash syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenofon Deligiannis committed Apr 18, 2019
1 parent 44a1798 commit 4c59cd6
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 72 deletions.
14 changes: 7 additions & 7 deletions spec/helpers/truncate_html_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ def truncator
end

it 'is included in ActionView::Base' do
ActionView::Base.included_modules.should include(TruncateHtmlHelper)
expect(ActionView::Base.included_modules).to include(TruncateHtmlHelper)
end

before(:each) do
@original_html = '<p>foo</p>'
@original_html.stub(:html_safe).and_return(@original_html)
allow(@original_html).to receive(:html_safe).and_return(@original_html)
end

it 'creates an instance of HtmlTruncator and calls truncate on it' do
truncator = double(truncate: @original_html)
TruncateHtml::HtmlTruncator.should_receive(:new).and_return(truncator)
expect(TruncateHtml::HtmlTruncator).to receive(:new).and_return(truncator)
truncator.truncate_html(@original_html)
end

it 'calls truncate on the HtmlTruncator object' do
truncator = double(truncate: @original_html)
TruncateHtml::HtmlTruncator.stub(:new).and_return(truncator)
truncator.should_receive(:truncate).and_return(@original_html)
allow(TruncateHtml::HtmlTruncator).to receive(:new).and_return(truncator)
expect(truncator).to receive(:truncate).and_return(@original_html)
truncator.truncate_html('foo')
end

context 'the input html is nil' do
it 'returns an empty string' do
truncator.truncate_html(nil).should be_empty
truncator.truncate_html(nil).should be_kind_of(String)
expect(truncator.truncate_html(nil)).to be_empty
expect(truncator.truncate_html(nil)).to be_kind_of(String)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/truncate_html/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
describe 'self.configure' do

it 'yields the configuration object' do
lambda do
expect do
TruncateHtml.configure do |config|
config.should be_kind_of(TruncateHtml::Configuration)
expect(config).to be_kind_of(TruncateHtml::Configuration)
throw :yay_it_yielded
end
end.should throw_symbol(:yay_it_yielded)
end.to throw_symbol(:yay_it_yielded)
end

end
Expand Down
36 changes: 19 additions & 17 deletions spec/truncate_html/html_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,53 @@ def html_string(original_string)
describe '#html_tokens' do
it 'returns each token in the string as an array element removing any consecutive whitespace from the string' do
html = '<h1>Hi there</h1> <p>This is sweet!</p> <p> squaremeter m² </p>'
html_string(html).html_tokens.should == ['<h1>', 'Hi', ' ', 'there', '</h1>', ' ', '<p>', 'This', ' ', 'is', ' ', 'sweet!', '</p>',
' ', '<p>', ' ', 'squaremeter', ' ', 'm²', ' ', '</p>']
expect(html_string(html).html_tokens).
to eq ['<h1>', 'Hi', ' ', 'there', '</h1>', ' ', '<p>', 'This', ' ',
'is', ' ', 'sweet!', '</p>', ' ', '<p>', ' ', 'squaremeter', ' ',
'm²', ' ', '</p>']
end
end

describe '#html_tag?' do
it 'returns false when the string parameter is not an html tag' do
html_string('no tags').should_not be_html_tag
expect(html_string('no tags')).not_to be_html_tag
end

it 'returns true when the string parameter is an html tag' do
html_string('<img src="foo">').should be_html_tag
html_string('</img>').should be_html_tag
expect(html_string('<img src="foo">')).to be_html_tag
expect(html_string('</img>')).to be_html_tag
end

it 'is false for html comments' do
html_string('<!-- hi -->').should_not be_html_tag
expect(html_string('<!-- hi -->')).not_to be_html_tag
end
end

describe '#open_tag?' do
it 'returns true if the tag is an open tag' do
html_string('<a>').should be_open_tag
expect(html_string('<a>')).to be_open_tag
end

context 'the tag is an open tag, and has whitespace and html properties' do
it 'returns true if it has single quotes' do
html_string(" <a href='http://awesomeful.net' >").should be_open_tag
expect(html_string(" <a href='http://awesomeful.net' >")).to be_open_tag
end

it 'returns true if it has double quotes' do
html_string(' <a href="http://awesomeful.net">').should be_open_tag
expect(html_string(' <a href="http://awesomeful.net">')).to be_open_tag
end
end

it 'returns false if the tag is a close tag' do
html_string('</a>').should_not be_open_tag
expect(html_string('</a>')).not_to be_open_tag
end

it 'returns false if the string is not an html tag' do
html_string('foo bar').should_not be_open_tag
expect(html_string('foo bar')).not_to be_open_tag
end

it 'returns false if it is a <script> tag' do
html_string('<script>').should_not be_open_tag
expect(html_string('<script>')).not_to be_open_tag
end
end

Expand All @@ -66,17 +68,17 @@ def html_string(original_string)

tag_pairs.each do |open_tag, close_tag|
it "closes a #{open_tag} and returns #{close_tag}" do
html_string(open_tag).matching_close_tag.should == close_tag
expect(html_string(open_tag).matching_close_tag).to eq close_tag
end
end
end

describe '#html_comment?' do
it 'is true for HTML comments' do
html_string('<!-- hi -->').should be_html_comment
html_string('<a>').should_not be_html_comment
html_string('</a>').should_not be_html_comment
html_string('foo').should_not be_html_comment
expect(html_string('<!-- hi -->')).to be_html_comment
expect(html_string('<a>')).not_to be_html_comment
expect(html_string('</a>')).not_to be_html_comment
expect(html_string('foo')).not_to be_html_comment
end
end
end
Loading

0 comments on commit 4c59cd6

Please sign in to comment.