Last active
December 16, 2015 01:18
-
-
Save jhammann/5353556 to your computer and use it in GitHub Desktop.
A helper that checks the length of a string and which adds three dots if that string exceeds a given length.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TextLengthHelper | |
def text_length(string, max_length) | |
return string if string.length <= max_length | |
string[0..(max_length - 1)] + "..." | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'text_length_helper' | |
describe TextLengthHelper do | |
include TextLengthHelper | |
full_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." | |
short_text = "Lorem ipsum dolor sit amet, consectetur adipiscing..." | |
it "should stay the same when a text is shorter than 100" do | |
text_length(full_text, 100).should eq full_text | |
end | |
it "should return a text with dots given a string longer than 50" do | |
text_length(full_text, 50).should eq short_text | |
end | |
it "should not have dots when given an empty string" do | |
text_length("", 50).should eq "" | |
end | |
end |
Tnx, Ik pas hem aan!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nog wat compacter :)