Triming string to fit a specific width.

hello all, I have a requirement to trim a string to fit a specific width. This data is displayed under some table column, and need to fit the column width. Since I can't tell the exact pixel in html. so we needn't be too exact about the trimLength. I want to do something like trimString(String source, int hintColumnWidth), but can I Unit Test it?( since the requirement is vague here.) Also the requirement says that , when it trims, it should display full string as tooltip , and that becomes <span title=...> etc, and then can I UT it? Thanks.

trucate will trucate your text, but you will need a helper to do the rest. Something like

def my_cell_value( a_string )

“<span title='#{a_string}>#{truncate( a_string, 23 )}”

end

in your helper file should do the job.

Hope that helps

Thanks Daniel , I know the general code. What I want to know is about testing. Since the requirement isn't very exact, so say, 3 columnWidthUnit = 1 strLengthUnit, so I write trim(str,columnWidthUnit/3) for that. so if the method is trimString(source, hintColumnWidth), Then in test, I just test trimString("helloworld", 3) produces "h...", Am I correct? And about tooltip, I test result str equals to "<span title='helloworld'>h...</span>", right? So If I will put in html other attributes, like <span id=... title=...>, then I use regex to test it? Right?

Sorri . Seems I misunderstood.

I wouldn’t use a regex directly for this. You can use something like assert_tag or even better HPricott

http://api.rubyonrails.org/classes/Test/Unit/Assertions.html#M000970

Luck Redpath has a small writeup on using HPricott to test views. It’s here

http://www.lukeredpath.co.uk/2006/7/7/testing-your-rails-views-with-hpricot

I hope that’s more along the lines of what your after.

Hi Daniel, that looks interesting.I'll check it up. I'd thought about selenium, but I feel in this small requirement using selenium to test that would be an overkill and too much effort to set up. Thanks for the hint.