Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distance_of_time_in_words returns a string instead of text element #781

Merged
merged 9 commits into from Jun 1, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,6 +1,7 @@
### Changes since v0.13

- Change `Lucky::BaseApp` to `Lucky::BaseAppServer`
- Many text helpers now return a `String` instead of appending to the view (`cycle`, `excerpt`, `highlight`, `pluralize`, `time_ago_in_words`, `to_sentence`, `word_wrap`) [#781](https://github.com/luckyframework/lucky/pull/781)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth putting in bold **Breaking change** - Many text... since this will definitely break stuff using it


### Changes since v0.12

Expand Down
76 changes: 38 additions & 38 deletions spec/lucky/text_helpers/cycle_spec.cr
Expand Up @@ -7,7 +7,7 @@ describe Lucky::TextHelpers do

describe "cycle" do
describe Lucky::TextHelpers::Cycle do
it "cycles" do
it "cycles when converted to a string" do
value = Lucky::TextHelpers::Cycle.new("one", 2, "3")
value.to_s.should eq "one"
value.to_s.should eq "2"
Expand All @@ -21,38 +21,38 @@ describe Lucky::TextHelpers do
end

it "cycles" do
view.cycle("one", 2, "3").to_s.should eq "one"
view.cycle("one", 2, "3").to_s.should eq "2"
view.cycle("one", 2, "3").to_s.should eq "3"
view.cycle("one", 2, "3").to_s.should eq "one"
view.cycle("one", 2, "3").to_s.should eq "2"
view.cycle("one", 2, "3").to_s.should eq "3"
view.cycle("one", 2, "3").should eq "one"
view.cycle("one", 2, "3").should eq "2"
view.cycle("one", 2, "3").should eq "3"
view.cycle("one", 2, "3").should eq "one"
view.cycle("one", 2, "3").should eq "2"
view.cycle("one", 2, "3").should eq "3"
end

it "cycles with array" do
array = [1, 2, 3]
view.cycle(array).to_s.should eq "1"
view.cycle(array).to_s.should eq "2"
view.cycle(array).to_s.should eq "3"
view.cycle(array).should eq "1"
view.cycle(array).should eq "2"
view.cycle(array).should eq "3"
end

it "cycle resets with new values" do
view.cycle("even", "odd").to_s.should eq "even"
view.cycle("even", "odd").to_s.should eq "odd"
view.cycle("even", "odd").to_s.should eq "even"
view.cycle(1, 2, 3).to_s.should eq "1"
view.cycle(1, 2, 3).to_s.should eq "2"
view.cycle(1, 2, 3).to_s.should eq "3"
view.cycle(1, 2, 3).to_s.should eq "1"
view.cycle("even", "odd").should eq "even"
view.cycle("even", "odd").should eq "odd"
view.cycle("even", "odd").should eq "even"
view.cycle(1, 2, 3).should eq "1"
view.cycle(1, 2, 3).should eq "2"
view.cycle(1, 2, 3).should eq "3"
view.cycle(1, 2, 3).should eq "1"
end

it "cycles named cycles" do
view.cycle(1, 2, 3, name: "numbers").to_s.should eq "1"
view.cycle("red", "blue", name: "colors").to_s.should eq "red"
view.cycle(1, 2, 3, name: "numbers").to_s.should eq "2"
view.cycle("red", "blue", name: "colors").to_s.should eq "blue"
view.cycle(1, 2, 3, name: "numbers").to_s.should eq "3"
view.cycle("red", "blue", name: "colors").to_s.should eq "red"
view.cycle(1, 2, 3, name: "numbers").should eq "1"
view.cycle("red", "blue", name: "colors").should eq "red"
view.cycle(1, 2, 3, name: "numbers").should eq "2"
view.cycle("red", "blue", name: "colors").should eq "blue"
view.cycle(1, 2, 3, name: "numbers").should eq "3"
view.cycle("red", "blue", name: "colors").should eq "red"
end

it "gets current cycle with default name" do
Expand All @@ -66,11 +66,11 @@ describe Lucky::TextHelpers do

it "gets current cycle with named cycles" do
view.cycle("red", "blue", name: "colors")
view.current_cycle("colors").to_s.should eq "red"
view.current_cycle("colors").should eq "red"
view.cycle("red", "blue", name: "colors")
view.current_cycle("colors").to_s.should eq "blue"
view.current_cycle("colors").should eq "blue"
view.cycle("red", "blue", name: "colors")
view.current_cycle("colors").to_s.should eq "red"
view.current_cycle("colors").should eq "red"
end

it "gets current cycle with no exceptions" do
Expand All @@ -90,30 +90,30 @@ describe Lucky::TextHelpers do
end

it "cycles with default named" do
view.cycle(1, 2, 3).to_s.should eq "1"
view.cycle(1, 2, 3, name: "default").to_s.should eq "2"
view.cycle(1, 2, 3).to_s.should eq "3"
view.cycle(1, 2, 3).should eq "1"
view.cycle(1, 2, 3, name: "default").should eq "2"
view.cycle(1, 2, 3).should eq "3"
end

it "resets cycle" do
view.cycle(1, 2, 3).to_s.should eq "1"
view.cycle(1, 2, 3).to_s.should eq "2"
view.cycle(1, 2, 3).should eq "1"
view.cycle(1, 2, 3).should eq "2"
view.reset_cycle
view.cycle(1, 2, 3).to_s.should eq "1"
view.cycle(1, 2, 3).should eq "1"
end

it "resets unknown cycle" do
view.reset_cycle("colors")
end

it "resets named cycle" do
view.cycle(1, 2, 3, name: "numbers").to_s.should eq "1"
view.cycle("red", "blue", name: "colors").to_s.should eq "red"
view.cycle(1, 2, 3, name: "numbers").should eq "1"
view.cycle("red", "blue", name: "colors").should eq "red"
view.reset_cycle("numbers")
view.cycle(1, 2, 3, name: "numbers").to_s.should eq "1"
view.cycle("red", "blue", name: "colors").to_s.should eq "blue"
view.cycle(1, 2, 3, name: "numbers").to_s.should eq "2"
view.cycle("red", "blue", name: "colors").to_s.should eq "red"
view.cycle(1, 2, 3, name: "numbers").should eq "1"
view.cycle("red", "blue", name: "colors").should eq "blue"
view.cycle(1, 2, 3, name: "numbers").should eq "2"
view.cycle("red", "blue", name: "colors").should eq "red"
end
end
end
58 changes: 29 additions & 29 deletions spec/lucky/text_helpers/excerpts_spec.cr
Expand Up @@ -3,51 +3,51 @@ require "./text_helpers_spec"
describe Lucky::TextHelpers do
describe "excerpt" do
it "excerpts" do
view.excerpt("This is a beautiful morning", "beautiful", radius: 5).to_s.should eq "...is a beautiful morn..."
view.excerpt("This is a beautiful morning", "this", radius: 5).to_s.should eq "This is a..."
view.excerpt("This is a beautiful morning", "morning", radius: 5).to_s.should eq "...iful morning"
view.excerpt("This is a beautiful morning", "day").should be_nil
view.excerpt("This is a beautiful morning", "beautiful", radius: 5).should eq "...is a beautiful morn..."
view.excerpt("This is a beautiful morning", "this", radius: 5).should eq "This is a..."
view.excerpt("This is a beautiful morning", "morning", radius: 5).should eq "...iful morning"
view.excerpt("This is a beautiful morning", "day").should eq ""
end

it "excerpts with regex" do
view.excerpt("This is a beautiful! morning", "beautiful", radius: 5).to_s.should eq "...is a beautiful! mor..."
view.excerpt("This is a beautiful? morning", "beautiful", radius: 5).to_s.should eq "...is a beautiful? mor..."
view.excerpt("This is a beautiful? morning", /\bbeau\w*\b/i, radius: 5).to_s.should eq "...is a beautiful? mor..."
view.excerpt("This is a beautiful? morning", /\b(beau\w*)\b/i, radius: 5).to_s.should eq "...is a beautiful? mor..."
view.excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, radius: 5).to_s.should eq "...udge Allen and..."
view.excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, radius: 1, separator: " ").to_s.should eq "...judge Allen and..."
view.excerpt("This day was challenging for judge Allen and his colleagues.", /\b(\w*allen\w*)\b/i, radius: 5).to_s.should eq "...was challenging for..."
view.excerpt("This is a beautiful! morning", "beautiful", radius: 5).should eq "...is a beautiful! mor..."
view.excerpt("This is a beautiful? morning", "beautiful", radius: 5).should eq "...is a beautiful? mor..."
view.excerpt("This is a beautiful? morning", /\bbeau\w*\b/i, radius: 5).should eq "...is a beautiful? mor..."
view.excerpt("This is a beautiful? morning", /\b(beau\w*)\b/i, radius: 5).should eq "...is a beautiful? mor..."
view.excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, radius: 5).should eq "...udge Allen and..."
view.excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, radius: 1, separator: " ").should eq "...judge Allen and..."
view.excerpt("This day was challenging for judge Allen and his colleagues.", /\b(\w*allen\w*)\b/i, radius: 5).should eq "...was challenging for..."
end

it "excerpts in borderline cases" do
view.excerpt("", "", radius: 0).to_s.should eq ""
view.excerpt("a", "a", radius: 0).to_s.should eq "a"
view.excerpt("abc", "b", radius: 0).to_s.should eq "...b..."
view.excerpt("abc", "b", radius: 1).to_s.should eq "abc"
view.excerpt("abcd", "b", radius: 1).to_s.should eq "abc..."
view.excerpt("zabc", "b", radius: 1).to_s.should eq "...abc"
view.excerpt("zabcd", "b", radius: 1).to_s.should eq "...abc..."
view.excerpt("zabcd", "b", radius: 2).to_s.should eq "zabcd"
view.excerpt("", "", radius: 0).should eq ""
view.excerpt("a", "a", radius: 0).should eq "a"
view.excerpt("abc", "b", radius: 0).should eq "...b..."
view.excerpt("abc", "b", radius: 1).should eq "abc"
view.excerpt("abcd", "b", radius: 1).should eq "abc..."
view.excerpt("zabc", "b", radius: 1).should eq "...abc"
view.excerpt("zabcd", "b", radius: 1).should eq "...abc..."
view.excerpt("zabcd", "b", radius: 2).should eq "zabcd"

# excerpt strips the resulting string before ap-/prepending excerpt_string.
# whether this behavior is meaningful when excerpt_string is not to be
# appended is questionable.
view.excerpt(" zabcd ", "b", radius: 4).to_s.should eq "zabcd"
view.excerpt("z abc d", "b", radius: 1).to_s.should eq "...abc..."
view.excerpt(" zabcd ", "b", radius: 4).should eq "zabcd"
view.excerpt("z abc d", "b", radius: 1).should eq "...abc..."
end

it "excerpts with omission" do
view.excerpt("This is a beautiful morning", "beautiful", omission: "[...]", radius: 5).to_s.should eq "[...]is a beautiful morn[...]"
view.excerpt("This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome temperatures. So what are you gonna do about it?", "very", omission: "[...]").to_s.should eq "This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]"
view.excerpt("This is a beautiful morning", "beautiful", omission: "[...]", radius: 5).should eq "[...]is a beautiful morn[...]"
view.excerpt("This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome temperatures. So what are you gonna do about it?", "very", omission: "[...]").should eq "This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]"
end

it "excerpts with separator" do
view.excerpt("This is a very beautiful morning", "very", separator: " ", radius: 1).to_s.should eq "...a very beautiful..."
view.excerpt("This is a very beautiful morning", "this", separator: " ", radius: 1).to_s.should eq "This is..."
view.excerpt("This is a very beautiful morning", "morning", separator: " ", radius: 1).to_s.should eq "...beautiful morning"
view.excerpt("my very\nvery\nvery long\nstring", "long", separator: "\n", radius: 0).to_s.should eq "...very long..."
view.excerpt("my very\nvery\nvery long\nstring", "long", separator: "\n", radius: 1).to_s.should eq "...very\nvery long\nstring"
view.excerpt("This is a beautiful morning", "a", separator: "").to_s.should eq view.excerpt("This is a beautiful morning", "a").to_s
view.excerpt("This is a very beautiful morning", "very", separator: " ", radius: 1).should eq "...a very beautiful..."
view.excerpt("This is a very beautiful morning", "this", separator: " ", radius: 1).should eq "This is..."
view.excerpt("This is a very beautiful morning", "morning", separator: " ", radius: 1).should eq "...beautiful morning"
view.excerpt("my very\nvery\nvery long\nstring", "long", separator: "\n", radius: 0).should eq "...very long..."
view.excerpt("my very\nvery\nvery long\nstring", "long", separator: "\n", radius: 1).should eq "...very\nvery long\nstring"
view.excerpt("This is a beautiful morning", "a", separator: "").should eq view.excerpt("This is a beautiful morning", "a")
end
end
end
36 changes: 18 additions & 18 deletions spec/lucky/text_helpers/highlight_spec.cr
Expand Up @@ -13,42 +13,42 @@ end
describe Lucky::TextHelpers do
describe "highlight" do
it "highlights" do
view.highlight("This is a beautiful morning", "beautiful").to_s.should eq "This is a <mark>beautiful</mark> morning"
view.highlight("This is a beautiful morning, but also a beautiful day", "beautiful").to_s.should eq "This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day"
view.highlight("This is a beautiful morning, but also a beautiful day", "beautiful", highlighter: "<b>\\1</b>").to_s.should eq "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day"
view.highlight("This text is not changed because we supplied an empty phrase", "").to_s.should eq "This text is not changed because we supplied an empty phrase"
view.highlight("This is a beautiful morning", "beautiful").should eq "This is a <mark>beautiful</mark> morning"
view.highlight("This is a beautiful morning, but also a beautiful day", "beautiful").should eq "This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day"
view.highlight("This is a beautiful morning, but also a beautiful day", "beautiful", highlighter: "<b>\\1</b>").should eq "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day"
view.highlight("This text is not changed because we supplied an empty phrase", "").should eq "This text is not changed because we supplied an empty phrase"
end

it "does not highlight empty text" do
view.highlight(" ", "blank text is returned verbatim").to_s.should eq " "
view.highlight(" ", "blank text is returned verbatim").should eq " "
end

it "highlights with regexp" do
view.highlight("This is a beautiful! morning", "beautiful!").to_s.should eq "This is a <mark>beautiful!</mark> morning"
view.highlight("This is a beautiful! morning", "beautiful! morning").to_s.should eq "This is a <mark>beautiful! morning</mark>"
view.highlight("This is a beautiful? morning", "beautiful? morning").to_s.should eq "This is a <mark>beautiful? morning</mark>"
view.highlight("This is a beautiful! morning", "beautiful!").should eq "This is a <mark>beautiful!</mark> morning"
view.highlight("This is a beautiful! morning", "beautiful! morning").should eq "This is a <mark>beautiful! morning</mark>"
view.highlight("This is a beautiful? morning", "beautiful? morning").should eq "This is a <mark>beautiful? morning</mark>"
end

it "highlights accepts regexp" do
view.highlight("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i).to_s.should eq "This day was challenging for judge <mark>Allen</mark> and his colleagues."
view.highlight("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i).should eq "This day was challenging for judge <mark>Allen</mark> and his colleagues."
end

it "highlights with multiple phrases in one pass" do
view.highlight("wow em", %w(wow em), highlighter: "<em>\\1</em>").to_s.should eq %(<em>wow</em> <em>em</em>)
view.highlight("wow em", %w(wow em), highlighter: "<em>\\1</em>").should eq %(<em>wow</em> <em>em</em>)
end

it "highlights with html" do
view.highlight("<p>This is a beautiful morning, but also a beautiful day</p>", "beautiful").to_s.should eq "<p>This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day</p>"
view.highlight("<p>This is a <em>beautiful</em> morning, but also a beautiful day</p>", "beautiful").to_s.should eq "<p>This is a <em><mark>beautiful</mark></em> morning, but also a <mark>beautiful</mark> day</p>"
view.highlight("<p>This is a <em class=\"error\">beautiful</em> morning, but also a beautiful <span class=\"last\">day</span></p>", "beautiful").to_s.should eq "<p>This is a <em class=\"error\"><mark>beautiful</mark></em> morning, but also a <mark>beautiful</mark> <span class=\"last\">day</span></p>"
view.highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful").to_s.should eq "<p class=\"beautiful\">This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day</p>"
view.highlight("<p>This is a beautiful <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a beautiful day</p>", "beautiful").to_s.should eq "<p>This is a <mark>beautiful</mark> <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a <mark>beautiful</mark> day</p>"
view.highlight("<div>abc div</div>", "div", highlighter: "<b>\\1</b>").to_s.should eq "<div>abc <b>div</b></div>"
view.highlight("<p>This is a beautiful morning, but also a beautiful day</p>", "beautiful").should eq "<p>This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day</p>"
view.highlight("<p>This is a <em>beautiful</em> morning, but also a beautiful day</p>", "beautiful").should eq "<p>This is a <em><mark>beautiful</mark></em> morning, but also a <mark>beautiful</mark> day</p>"
view.highlight("<p>This is a <em class=\"error\">beautiful</em> morning, but also a beautiful <span class=\"last\">day</span></p>", "beautiful").should eq "<p>This is a <em class=\"error\"><mark>beautiful</mark></em> morning, but also a <mark>beautiful</mark> <span class=\"last\">day</span></p>"
view.highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful").should eq "<p class=\"beautiful\">This is a <mark>beautiful</mark> morning, but also a <mark>beautiful</mark> day</p>"
view.highlight("<p>This is a beautiful <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a beautiful day</p>", "beautiful").should eq "<p>This is a <mark>beautiful</mark> <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a <mark>beautiful</mark> day</p>"
view.highlight("<div>abc div</div>", "div", highlighter: "<b>\\1</b>").should eq "<div>abc <b>div</b></div>"
end

it "highlights with block" do
view.highlight("one two three", ["one", "two", "three"]) { |word| "<b>#{word}</b>" }.to_s.should eq "<b>one</b> <b>two</b> <b>three</b>"
view.test_highlight.to_s.should eq "This is a <span data-highlight-word=\"beautiful\" data-color=\"yellow\" data-español=\"bello\">beautiful</span> morning, but also a <span data-highlight-word=\"beautiful\" data-color=\"yellow\" data-español=\"bello\">beautiful</span> day"
view.highlight("one two three", ["one", "two", "three"]) { |word| "<b>#{word}</b>" }.should eq "<b>one</b> <b>two</b> <b>three</b>"
view.test_highlight.should eq "This is a <span data-highlight-word=\"beautiful\" data-color=\"yellow\" data-español=\"bello\">beautiful</span> morning, but also a <span data-highlight-word=\"beautiful\" data-color=\"yellow\" data-español=\"bello\">beautiful</span> day"
end
end
end