require 'test/unit' require 'glue/string' class TCStringUtilsUtils < Test::Unit::TestCase # :nodoc: all include Glue 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 =begin 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 =end def test_random_string s1 = StringUtils.random() s2 = StringUtils.random() assert_not_equal(s1, s2) assert(s1.size == s2.size) end def teardown @s = nil end end