require 'test/unit' require 'facet/string/blank?' require 'facet/string/braket' require 'facet/string/camelcase' require 'facet/string/case?' require 'facet/string/center_lines' require 'facet/string/chars' require 'facet/string/clump' require 'facet/string/cmp' require 'facet/string/each_word' require 'facet/string/first&last' require 'facet/string/index_all' require 'facet/string/line_to_char_map' require 'facet/string/line_wrap' require 'facet/string/mscan' require 'facet/string/natcmp' require 'facet/string/rand_letter' require 'facet/string/range' require 'facet/string/shatter' require 'facet/string/tab' require 'facet/string/to_b' require 'facet/string/to_const' require 'facet/string/to_proc' require 'facet/string/to_re' require 'facet/string/unpack' require 'facet/string/word_filter!' require 'facet/string/word_wrap' class TC_String < Test::Unit::TestCase #blank? def test_blank? assert(! "xyz".blank?) assert(" ".blank?) end # braket def test_braket assert_equal( '#X#', 'X'.braket('#') ) assert_equal( 'xX!', 'X'.braket('x','!') ) assert_equal( '{X}', 'X'.braket('{','}') ) assert_equal( '', 'X'.braket('<') ) assert_equal( '(X)', 'X'.braket('(') ) assert_equal( '[X]', 'X'.braket('[') ) assert_equal( '{X}', 'X'.braket('{') ) end # camelcase def test_camelcase assert_equal( "abcXyz", "abc_xyz".camelCase ) assert_equal( "abcXyz", "abc xyz".camelCase ) assert_equal( "abcXyz", "abc xyz".camelCase ) assert_equal( "abcXyz", "abc\txyz".camelCase ) assert_equal( "abcXyz", "abc\nxyz".camelCase ) assert_equal( "abcXyz", "abc____xyz".camelCase ) end def test_camelcase_true assert_equal( "AbcXyz", "abc_xyz".camelcase(true) ) assert_equal( "AbcXyz", "abc xyz".camelcase(true) ) assert_equal( "AbcXyz", "abc xyz".camelcase(true) ) assert_equal( "AbcXyz", "abc\txyz".camelcase(true) ) assert_equal( "AbcXyz", "abc\nxyz".camelcase(true) ) end # case? def test_case? assert( 'ABC'.upcase? ) assert( 'ABC'.uppercase? ) assert( 'abc'.downcase? ) assert( 'abc'.lowercase? ) assert( 'Abc'.capitalized? ) end # center_lines def test_center_lines i = "this\ntest" e = " this \n test \n" assert_equal( e, i.center_lines(10) ) end # chars def test_chars assert_equal( ["a","b","c"], "abc".chars ) assert_equal( ["a","b","\n","c"], "ab\nc".chars ) end # clump def test_clump s = "This is\na test.\n\nIt clumps\nlines of text." o = "This is a test.\n\nIt clumps lines of text." assert_equal( o, s.clump ) end # cmp def test_cmp assert_equal( -1, "abc".cmp("abcd") ) assert_equal( 1, "abcd".cmp("abc") ) assert_equal( -1, "abc".cmp("bcd") ) assert_equal( 1, "bcd".cmp("abc") ) end # each_word def test_each_word a = [] i = "this is a test" i.each_word{ |w| a << w } assert_equal( ['this', 'is', 'a', 'test'], a ) end # first&last def test_first assert_equal( "a", "a b c".first(1,nil) ) assert_equal( "a\nb", "a\nb\nc".first(2,'') ) assert_equal( "abc", "abcxyz".first_char(3) ) end def test_last assert_equal( "c", "a b c".last(1,nil) ) assert_equal( "b c", "a b c".last(2,nil) ) assert_equal( "b c", "a b c".last(2,nil) ) assert_equal( "b\nc", "a\nb\nc".last(2,'') ) assert_equal( "xyz", "abcxyz".last_char(3) ) end def test_first! s = "abc" s.first! "123" assert_equal( "123abc", s ) s = "a\nbc" s.first! "123" assert_equal( "123a\nbc", s ) end def test_last! s = "abc" s.last! "123" assert_equal( "abc123", s ) s = "a\nbc" s.last! "123" assert_equal( "a\nbc123", s ) end def test_first_eq $/ = "\n" s = "a\nbc" s.first = "123" assert_equal( "123\nbc", s ) end def test_last_eq_3 $/ = "\n" s = "a\nbc" s.last = "123" assert_equal( "a\n123", s ) end # index_all def index_all assert_equal( [0,4,8], "a123a567a9".index_all(/a/) ) end # line_wrap def test_line_wrap assert_equal "abc\n123\n", "abc123".line_wrap(3) assert_equal "abcd\n123\n", "abcd123".line_wrap(4) end # line_to_char_map def test line_to_char_map a = "0123\n456\n78" ltcm = line_to_char_map(a) assert_equal( [0..4, 5..8, 9..10], ltcm ) end # mscan def test_mscan r = 'abc,def,gh'.mscan(/[,]/) assert( r.all?{ |md| MatchData === md } ) assert_equal( 2, r.to_a.length ) assert_equal( ',', r[0][0] ) assert_equal( ',', r[1][0] ) end # natcmp def test_natcmp assert( -1, "my_prog_v1.1.0".natcmp("my_prog_v1.2.0") ) assert( -1, "my_prog_v1.2.0".natcmp("my_prog_v1.10.0") ) assert( 1, "my_prog_v1.2.0".natcmp("my_prog_v1.1.0") ) assert( 1, "my_prog_v1.10.0".natcmp("my_prog_v1.2.0") ) assert( 0, "my_prog_v1.0.0".natcmp("my_prog_v1.0.0") ) end # rand_letter def test_rand_letter 100.times { |i| assert( /[a-zA-z]/ =~ String.rand_letter ) } end # range def test_range assert_equal( (1..3), "a123a567a9".range(/123/) ) assert_equal( (0..0), "a123a567a9".range(/a/) ) end def test_range_all assert_equal( [ (1..3), (5..7) ], "a123a123a9".range_all(/123/) ) assert_equal( [ (0..0), (4..4), (8..8) ], "a123a567a9".range_all(/a/) ) end # shatter def test_shatter s = "

Thisisa test.

" sh = s.shatter( /<.*?>/ ) e = ["", "

", "This", "", "is", "", "a test.", "

"] assert_equal(e, sh) end # tab def test_tab a = "xyz".indent(4) assert_equal( ' ', a[0..3] ) a = "xyz".tab(4) assert_equal( ' ', a[0..3] ) a = "xyz".tabto(4) assert_equal( ' ', a[0..3] ) # To do: need to expand on this end # to_b def test_to_b assert( 'true'.to_b ) assert( 'True'.to_b ) assert( 'yes'.to_b ) assert( 'YES'.to_b ) assert( 'on'.to_b ) assert( 'ON'.to_b ) assert( 't'.to_b ) assert( '1'.to_b ) assert( 'y'.to_b ) assert( 'Y'.to_b ) assert( '=='.to_b ) assert( ! 'nil'.to_b ) assert( ! 'false'.to_b ) assert( ! 'blahblahtrueblah'.to_b ) assert_equal( nil, 'nil'.to_b ) assert_equal( nil, 'null'.to_b ) end # to_const TestConst = 4 def test_to_const assert_equal( 4, "TC_String::TestConst".to_const ) end # to_proc def test_to_proc assert_nothing_raised { @add = '|x,y| x + y'.to_proc } assert_equal(4, @add.call(2,2)) @t = 3 @multi = '|y| @t * y'.to_proc(self) assert_equal(6, @multi.call(2)) x = 4 @div = '|y| x / y'.to_proc(binding) assert_equal(2, @div.call(2)) end # to_re def test_to_re assert_equal( /abc/, "abc".to_re ) assert_equal( /a\+bc/, "a+bc".to_re ) assert_equal( /a+bc/, "a+bc".to_re(false) ) end # upack def test_unpack str, fmt = "abc\0", "Z*" assert_equal "abc", str.unpack(fmt).first assert_equal "abc", str.unpack(fmt, 0).first assert_equal "bc", str.unpack(fmt, 1).first assert_equal "bc", str.unpack(fmt, -3).first assert_equal "c", str.unpack(fmt, 2).first assert_equal "c", str.unpack(fmt, -2).first assert_equal "", str.unpack(fmt, 3).first assert_equal "", str.unpack(fmt, -1).first end # word_filter! def test_word_filter! i = "this is a test" i.word_filter!{ |w| "#{w}1" } assert_equal( 'this1 is1 a1 test1', i ) end # word_wrap def test_word_wrap assert_equal "abcde\n12345\nxyzwu", "abcde 12345 xyzwu".word_wrap(5) assert_equal "abcd\n1234\nxyzw", "abcd 1234 xyzw".word_wrap(4) assert_equal "abc\n123", "abc 123".word_wrap(4) assert_equal "abc\n 123", "abc 123".word_wrap(4) assert_equal "abc\n 123", "abc 123".word_wrap(4) end # def test_word_wrap # assert_equal "abcde-\n12345-\nxyzwu\n", "abcde12345xyzwu".word_wrap(6,2) # assert_equal "abcd-\n1234-\nxyzw\n", "abcd1234xyzw".word_wrap(5,2) # assert_equal "abc \n123\n", "abc 123".word_wrap(4,2) # assert_equal "abc \n123\n", "abc 123".word_wrap(4,2) # assert_equal "abc \n123\n", "abc 123".word_wrap(4,2) # end end