Skip to content

String is truncated even when shorter than length #54

@rafaelpetry

Description

@rafaelpetry

If I try to truncate a string using an omission, the string may get truncated even when it is shorter than the length. This happens when the combined length of the string and the omission is greater than the :length option. For instance, if you add the following test to html_truncator_spec.rb ...

it 'does not truncate a string shorter than length' do
  truncate('some string', length: 12, omission: '...').should == 'some string'
end

... it will fail with the following error:

1) TruncateHtml::HtmlTruncator does not truncate a string shorter than length
   Failure/Error: truncate('some string', length: 12, omission: '...').should == 'some string'
     expected: "some string"
          got: "some..." (using ==)
   # ./spec/truncate_html/html_truncator_spec.rb:62:in `block (2 levels) in <top (required)>'

This happens even though "some string" is shorter than 12 characters. Is this expected behavior? It differs from the behavior of truncate from Active Support, where "some string".truncate(12, omission: '...') == "some string".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions