# :nodoc: all require "test/unit" require "n/utils/string" class TC_StringUtilsUtils < Test::Unit::TestCase # :nodoc: all include N def setup @s = "this is any cool test" end def test_valid? assert_equal(true, StringUtils.valid?("test")) assert_equal(false, StringUtils.valid?("")) assert_equal(false, StringUtils.valid?(nil)) end def test_head assert_equal(nil, StringUtils.head(nil)) assert_equal("this...", StringUtils.head(@s, 3)) assert_equal("this+++", StringUtils.head(@s, 3, false, "+++")) assert_equal("thi...", StringUtils.head(@s, 3, true)) assert_equal("this is...", StringUtils.head(@s, 8)) assert_equal(nil, StringUtils.head(@s, 0)) assert_equal(nil, StringUtils.head(@s, 0, true)) assert_equal("this is...", StringUtils.head(@s, 8, true)) end def test_rewrite rules = [ [/\/\$/, "../n1"], [/\$/, "../n1"], [/^games/, "../../games"], [/^tmp/, "/n/theseas/var/tmp"], ] string = StringUtils.rewrite("games/arkanoid.html", rules) assert_equal("../../games/arkanoid.html", string) string = StringUtils.rewrite("tmp/garbage.html", rules) assert_equal("/n/theseas/var/tmp/garbage.html", string) string = StringUtils.rewrite("root/index.html", rules) assert_equal("root/index.html", string) assert_equal(nil, StringUtils.rewrite(nil, rules)) assert_equal("", StringUtils.rewrite("", rules)) assert_raises(ArgumentError) { assert_equal("koko", StringUtils.rewrite("koko", nil)) } # bug: should keep order s = StringUtils.rewrite("/$/koko.html", rules) assert_equal("../n1/koko.html", s) end def test_wrap s = "1234567890abcdefghijklmnopqrstu" r = "1234 5678 90ab cdef ghij klmn opqr stu" assert_equal(r, StringUtils::wrap(s, 4, " ")) s = "111111111111111111111111111111111111111111111111" r = "1111111111 1111111111 1111111111 1111111111 11111111" assert_equal(r, StringUtils::wrap(s, 10, " ")) s = "jackdaws love my big sphinx of quartz" r = "jackdaws love my big sphinx of quartz" assert_equal(r, StringUtils::wrap(s, 10, " ")) s = "jackdaws love my big sphinx of quartz" r = "jack daws love my big sphi nx of quar tz" assert_equal(r, StringUtils::wrap(s, 4, " ")) s = "jack.daws love my big sphinx of quartz" r = "jack .daw s love my big sphi nx of quar tz" assert_equal(r, StringUtils::wrap(s, 4, " ")) assert_equal("", StringUtils::wrap("", 4, " ")) assert_equal(nil, StringUtils::wrap(nil, 4, " ")) end def test_extension_from_path assert_equal(nil, StringUtils.extension_from_path(nil)) assert_equal(nil, StringUtils.extension_from_path("this is a dymy text!")) assert_equal("gif", StringUtils.extension_from_path("gmosx.gif")) assert_equal("gif", StringUtils.extension_from_path("/n/images/gmosx.gif")) assert_equal(nil, StringUtils.extension_from_path("/n/images/gmosx.")) assert_equal("rx", StringUtils.extension_from_path("/n/images/gmosx.gif.rx")) assert_equal("rx", StringUtils.extension_from_path("*parts/polls/poll-box.rx")) assert_equal("rx", StringUtils.extension_from_path("../n1/web/parts/polls/poll-box.rx")) end def test_filename_from_path assert_equal("gmosx", StringUtils.filename_from_path("gmosx.gif")) filename = StringUtils.filename_from_path("/n/images/gmosx.gif") assert_equal("/n/images/gmosx", filename) filename = StringUtils.filename_from_path(nil) assert_equal("", filename) filename = StringUtils.filename_from_path("/n/images/gmosx") assert_equal("/n/images/gmosx", filename) end def test_file_from_path file = StringUtils.file_from_path("/n/images/gmosx.gif") assert_equal("gmosx.gif", file) file = StringUtils.file_from_path(nil) assert_equal("", file) file = StringUtils.file_from_path("/n/images/gmosx") assert_equal("gmosx", file) end def test_directory_from_path directory = StringUtils.directory_from_path("/n/images/gmosx.gif") assert_equal("/n/images", directory) directory = StringUtils.directory_from_path(nil) assert_equal("", directory) directory = StringUtils.directory_from_path("/n/images/gmosx") assert_equal("/n/images", directory) end def test_rationalize_filename filename = StringUtils.rationalize_filename("hello my friend!.gif") assert_equal("hello-my-friend.gif", filename) filename = StringUtils.rationalize_filename("παμε παλι.gif") assert_equal("pame-pali.gif", filename) filename = StringUtils.rationalize_filename("τί λές.gif") assert_equal("ti-les.gif", filename) # bug: filename = StringUtils.rationalize_filename("image-(10).gif") assert_equal("image-10.gif", filename) end def test_random_string s1 = StringUtils.random() s2 = StringUtils.random() assert_not_equal(s1, s2) assert(s1.size == s2.size) end def test_unicode_to_iso88597 # check that dup works. s = "Original" assert_equal("Original", StringUtils.unicode_to_iso88597(s)) end def teardown @s = nil end end