# encoding: utf-8 require "test_helper" require "stringex" if RUBY_VERSION.to_f < 1.9 $KCODE = "U" end # Try loading RedCloth but it's okay if it's not there begin gem 'RedCloth' rescue LoadError puts puts ">> Could not load RedCloth. String#to_html was not tested." puts ">> Please gem install RedCloth if you'd like to use this functionality." puts end class StringExtensionsTest < Test::Unit::TestCase def setup Stringex::Localization.reset! end def test_to_url { "
This has 100% too much formatting
" => "this-has-100-percent-too-much-formatting", "Tea & crumpets & crêpes for me!" => "tea-and-crumpets-and-crepes-for-me", "The Suspense... Is... Killing Me" => "the-suspense-dot-dot-dot-is-dot-dot-dot-killing-me", "How to use attr_accessible and attr_protected" => "how-to-use-attr-accessible-and-attr-protected", "I'm just making sure there's nothing wrong with things!" => "im-just-making-sure-theres-nothing-wrong-with-things", "foo = bar and bar=foo" => "foo-equals-bar-and-bar-equals-foo", "Period.period" => "period-dot-period", "Will…This Work?" => "will-dot-dot-dot-this-work", "We check the D.N.A for matches" => "we-check-the-dna-for-matches", "We check the D.N.A. for matches" => "we-check-the-dna-for-matches", "Go to the Y.M.C.America" => "go-to-the-ymcamerica", "He shops at J.C. Penney" => "he-shops-at-jc-penney", "¼ pound with cheese" => "one-fourth-pound-with-cheese", "Will's Ferrel" => "wills-ferrel", "Капитал" => "kapital", "Ελλάδα" => "ellada", "中文" => "zhong-wen", "Paul Cézanne" => "paul-cezanne", "21'17ʼ51" => "21-17-51", "ITCZ÷1 (21°17ʼ51.78”N / 89°35ʼ28.18”O / 26-04-08 / 09:00 am)" => "itcz-divided-by-1-21-degrees-17-51-dot-78-n-slash-89-degrees-35-28-dot-18-o-slash-26-04-08-slash-09-00-am", "/" => "slash", "私はガラスを食べられます。それは私を傷つけません。" => "si-hagarasuwoshi-beraremasu-sorehasi-woshang-tukemasen", "ǝ is a magical string" => "at-is-a-magical-string", "either | or" => "either-or", "La Maison d`Uliva" => "la-maison-duliva", "カッページ・テラスに日系カフェ&バー、店内にDJブースも - シンガポール経済新聞" => "katupeziterasuniri-xi-kahue-and-ba-dian-nei-nidjbusumo-singaporujing-ji-xin-wen", "AVアンプ、ホームシアターシステム、スピーカーシステム等" => "avanpu-homusiatasisutemu-supikasisutemudeng", "У лукоморья дуб зеленый" => "u-lukomoria-dub-zielienyi", "Here are some {braces}" => "here-are-some-braces", "$1,000" => "1000-dollars", "«zut alors»" => "zut-alors", "Rabbits aren't real" => "rabbits-arent-real", "R$ isn't R" => "reais-isnt-r", "Last Friday Night (T.G.I.F.)" => "last-friday-night-tgif", "foo` bar" => "foo-bar", "foo~ bar" => "foo-bar" }.each do |html, plain| assert_equal plain, html.to_url end end def test_to_url_with_danish_characters Stringex::Localization.locale = :da assert_equal "roedgroed-med-floede", "Rødgrød med fløde".to_url end def test_to_url_with_excludes assert_equal "So Fucking Special", "So Fucking Special".to_url(:exclude => "So Fucking Special") end def test_to_url_without_forcing_downcase assert_equal "I-have-CAPS", "I have CAPS".to_url(:force_downcase => false) end def test_to_url_with_limit assert_equal "I am much too long".to_url(:limit => 13), "i-am-much-too" end def test_to_url_with_alternate_whitespace_replacement assert_equal "under_scores", "Under Scores".to_url(:replace_whitespace_with => "_") end def test_to_url_with_alternate_whitespace_replacement_and_with_limit_and_with_truncate_words_disabled assert_equal "i_am_much", "I am much too long".to_url(:replace_whitespace_with => "_", :limit => 12, :truncate_words => false) end def test_remove_formatting { "This has 100% too much formatting
" => "This has 100 percent too much formatting", "Tea & crumpets & crêpes for me!" => "Tea and crumpets and crepes for me" }.each do |html, plain| assert_equal plain, html.remove_formatting end end def test_strip_html_tags { "This is invalid XHTML but valid HTML, right?" => "This is invalid XHTML but valid HTML, right?", "
Everything goes!
" => "Everything goes!", "I hated wrapping textilize around a string.
\nIt always felt dirty.
", "I think _this_ is awesome" => "I think this is awesome
", "Um... _*really*_, man" => "Um… really, man
" }.each do |plain, html| assert_equal html, plain.to_html end end def test_to_html_lite { "I have no pee on me" => "I have no pee on me", "But I _do_ get Textile!" => "But I do get Textile!" }.each do |plain, html| assert_equal html, plain.to_html(:lite) end end end end