This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
covers 'facets/string/acronym'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :acronym do
|
||||
test do
|
||||
'abc'.acronym.assert == 'a'
|
||||
'abc def'.acronym.assert == 'ad'
|
||||
'abc def ghi'.acronym.assert == 'adg'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
covers 'facets/string/align'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :align do
|
||||
test do
|
||||
"xxx".align(:right, 9) == " xxx"
|
||||
"xxx".align(:left, 9).assert == "xxx "
|
||||
"xxx".align(:center, 9).assert == " xxx "
|
||||
end
|
||||
end
|
||||
|
||||
method :align_right do
|
||||
test do
|
||||
"xxx".align_right(9) == " xxx"
|
||||
end
|
||||
end
|
||||
|
||||
method :align_left do
|
||||
test do
|
||||
"xxx".align_left(9).assert == "xxx "
|
||||
end
|
||||
end
|
||||
|
||||
method :align_center do
|
||||
test do
|
||||
"xxx".align_center(9).assert == " xxx "
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
covers 'facets/string/ascii_only'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :ascii_only do
|
||||
test 'without any non-ascii' do
|
||||
'abc'.ascii_only.assert == 'abc'
|
||||
end
|
||||
|
||||
test 'with non-ascii' do
|
||||
'中文123'.ascii_only.assert == '123'
|
||||
end
|
||||
|
||||
test 'with non-ascii when passed alt' do
|
||||
'abc'.ascii_only('!').assert == 'abc'
|
||||
end
|
||||
|
||||
test 'with non-ascii when passed alt' do
|
||||
'中文123'.ascii_only('!').assert == '!!123'
|
||||
end
|
||||
end
|
||||
|
||||
method :ascii_only! do
|
||||
test 'without any non-ascii' do
|
||||
s = 'abc'
|
||||
s.ascii_only!
|
||||
s.assert == 'abc'
|
||||
end
|
||||
|
||||
test 'with non-ascii' do
|
||||
s = '中文123'
|
||||
s.ascii_only!
|
||||
s.assert == '123'
|
||||
end
|
||||
|
||||
test 'without any non-ascii' do
|
||||
s = 'abc'
|
||||
s.ascii_only!('!')
|
||||
s.assert == 'abc'
|
||||
end
|
||||
|
||||
test 'with non-ascii' do
|
||||
s = '中文123'
|
||||
s.ascii_only!('!')
|
||||
s.assert == '!!123'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
covers 'facets/string/bracket'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :bracket do
|
||||
test do
|
||||
'X'.bracket('#').assert == '#X#'
|
||||
'X'.bracket('x','!').assert == 'xX!'
|
||||
'X'.bracket('{','}').assert == '{X}'
|
||||
'X'.bracket('<').assert == '<X>'
|
||||
'X'.bracket('(').assert == '(X)'
|
||||
'X'.bracket('[').assert == '[X]'
|
||||
'X'.bracket('{').assert == '{X}'
|
||||
end
|
||||
end
|
||||
|
||||
method :bracket! do
|
||||
test do
|
||||
a = 'X' ; a.bracket!('#')
|
||||
a.assert == '#X#'
|
||||
a = 'X' ; a.bracket!('x','!')
|
||||
a.assert == 'xX!'
|
||||
a = 'X' ; a.bracket!('{','}')
|
||||
a.assert == '{X}'
|
||||
a = 'X' ; a.bracket!('<')
|
||||
a.assert == '<X>'
|
||||
a = 'X' ; a.bracket!('(')
|
||||
a.assert == '(X)'
|
||||
a = 'X' ; a.bracket!('[')
|
||||
a.assert == '[X]'
|
||||
a = 'X' ; a.bracket!('{')
|
||||
a.assert == '{X}'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
covers 'facets/string/camelcase'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :camelcase do
|
||||
|
||||
test do
|
||||
"Camel_case".camelcase.assert == "CamelCase"
|
||||
end
|
||||
|
||||
test "converts underscore to captialized" do
|
||||
"abc_xyz".camelcase.assert == "abcXyz"
|
||||
"abc____xyz".camelcase.assert == "abcXyz"
|
||||
end
|
||||
|
||||
test "can captialize at spaces" do
|
||||
"abc xyz".camelcase(' ').assert == "abcXyz"
|
||||
"abc xyz".camelcase(' ').assert == "abcXyz"
|
||||
end
|
||||
|
||||
test "can capitalize at arbitrary string" do
|
||||
"abc;xyz".camelcase(';').assert == "abcXyz"
|
||||
"abc//xyz".camelcase('//').assert == "abcXyz"
|
||||
end
|
||||
|
||||
test "can captialize at regexp" do
|
||||
"abc\nxyz".camelcase(/\s/).assert == "abcXyz"
|
||||
"abc\txyz".camelcase(/\s/).assert == "abcXyz"
|
||||
end
|
||||
|
||||
# this functionality is in String#modulize instead
|
||||
test "does not convert paths to module names" do
|
||||
'my_module/my_class'.camelcase.refute == 'MyModule::MyClass'
|
||||
'/my_module/my_class'.camelcase.refute == '::MyModule::MyClass'
|
||||
end
|
||||
|
||||
# this functionality is in String#modulize instead
|
||||
test "does not convert methods to module names" do
|
||||
'My_module__my_class'.camelcase.refute == 'MyModule::MyClass'
|
||||
'__My_module__my_class'.camelcase.refute == '::MyModule::MyClass'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :lower_camelcase do
|
||||
test do
|
||||
"abc_xyz".lower_camelcase.assert == "abcXyz"
|
||||
"Abc_xyz".lower_camelcase.assert == "abcXyz"
|
||||
end
|
||||
end
|
||||
|
||||
method :upper_camelcase do
|
||||
test do
|
||||
"abc_xyz".upper_camelcase.assert == "AbcXyz"
|
||||
"Abc_xyz".upper_camelcase.assert == "AbcXyz"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,36 @@
|
||||
covers 'facets/string/capitalized'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :capitalized? do
|
||||
test do
|
||||
'Abc'.assert.capitalized?
|
||||
end
|
||||
end
|
||||
|
||||
method :downcase? do
|
||||
test do
|
||||
'abc'.assert.downcase?
|
||||
end
|
||||
end
|
||||
|
||||
method :upcase? do
|
||||
test do
|
||||
'ABC'.assert.upcase?
|
||||
end
|
||||
end
|
||||
|
||||
#method :lowercase? do
|
||||
# test do
|
||||
# 'abc'.assert.lowercase?
|
||||
# end
|
||||
#end
|
||||
|
||||
#method :uppercase? do
|
||||
# test do
|
||||
# 'ABC'.assert.uppercase?
|
||||
# end
|
||||
#end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
covers 'facets/string/characters'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :characters do
|
||||
test do
|
||||
"abc".characters.assert == ["a","b","c"]
|
||||
"ab\nc".characters.assert == ["a","b","\n","c"]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
covers 'facets/string/cleanlines'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :cleanlines do
|
||||
test do
|
||||
"a \n b \nc".cleanlines.to_a.assert == ["a","b","c"]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
covers 'facets/string/cleave'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :cleave do
|
||||
|
||||
table = [
|
||||
[ 'no spaces short word' , 'whole' , ['whole', ''] ],
|
||||
[ 'no spaces long word' , 'Supercalifragilisticexpialidocious' , ['Supercalifragilisticexpialidocious', ''] ],
|
||||
[ 'exact middle two words' , 'fancy split' , ['fancy', 'split'] ],
|
||||
[ 'exact middle many words' , 'All good Rubyists know how to party', ['All good Rubyists', 'know how to party'] ],
|
||||
[ 'closer to start' , 'short splitter' , ['short', 'splitter'] ],
|
||||
[ 'closer to start' , 'Four score and seven years ago...' , ['Four score and', 'seven years ago...'] ],
|
||||
[ 'closer to start' , 'abc def ghijklm nop' , ['abc def', 'ghijklm nop'] ],
|
||||
[ 'closer to end' , 'extended split' , ['extended', 'split'] ],
|
||||
[ 'closer to end' , 'abc defghi jklm nop' , [ 'abc defghi', 'jklm nop'] ]
|
||||
]
|
||||
|
||||
table.each do |d, s, x|
|
||||
test d do
|
||||
s.cleave.assert == x
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,18 @@
|
||||
covers 'facets/string/cmp'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :cmp do
|
||||
|
||||
test 'on strings' do
|
||||
"abc".cmp("abc").assert == 0
|
||||
"abc".cmp("abcd").assert == -1
|
||||
"abcd".cmp("abc").assert == 1
|
||||
"abc".cmp("bcd").assert == -1
|
||||
"bcd".cmp("abc").assert == 1
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
covers 'facets/string/compress_lines'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :compress_lines do
|
||||
|
||||
test do
|
||||
string = <<-QUERY.compress_lines
|
||||
SELECT name
|
||||
FROM users
|
||||
QUERY
|
||||
|
||||
string.assert == "SELECT name FROM users"
|
||||
end
|
||||
|
||||
test do
|
||||
string = <<-QUERY.compress_lines(false)
|
||||
SELECT name
|
||||
FROM users
|
||||
QUERY
|
||||
|
||||
string.assert == "SELECT nameFROM users"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/string/crypt'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :crypt do
|
||||
test do
|
||||
"abc".crypt('ZZ').assert.start_with?('ZZ')
|
||||
end
|
||||
|
||||
test do
|
||||
encrypted = "abc".crypt
|
||||
salt = encrypted[0..1]
|
||||
"abc".crypt(salt).assert == encrypted
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/string/divide'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :divide do
|
||||
|
||||
test do
|
||||
s = "<p>This<b>is</b>a test.</p>"
|
||||
d = s.divide( /<.*?>/ )
|
||||
e = ["<p>This", "<b>is", "</b>a test.", "</p>"]
|
||||
d.assert == e
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/string/each_word.rb'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :each_word do
|
||||
|
||||
test do
|
||||
a = []
|
||||
i = "this is a test"
|
||||
i.each_word{ |w| a << w }
|
||||
a.assert == ['this', 'is', 'a', 'test']
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
covers 'facets/string/edit_distance'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :edit_distance do
|
||||
|
||||
test do
|
||||
d = "pretzel".edit_distance('pretzil')
|
||||
d.assert = 1
|
||||
end
|
||||
|
||||
test do
|
||||
d = "zretzel".edit_distance('pretzil')
|
||||
d.assert = 2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
covers 'facets/string/exclude'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :exclude? do
|
||||
|
||||
test do
|
||||
"aaaa".assert.exclude?('bb')
|
||||
"aaab".assert.exclude?('bb')
|
||||
"aaba".assert.exclude?('bb')
|
||||
"baaa".assert.exclude?('bb')
|
||||
end
|
||||
|
||||
test do
|
||||
"abba".refute.exclude?('bb')
|
||||
"aabb".refute.exclude?('bb')
|
||||
"bbaa".refute.exclude?('bb')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
covers 'facets/string/expand_tab'
|
||||
|
||||
test_case String do
|
||||
|
||||
setup do
|
||||
@tabs = <<-EOF
|
||||
|
||||
\tOne tab
|
||||
\tOne space and one tab
|
||||
\t Six spaces, a tab, and a space
|
||||
EOF
|
||||
end
|
||||
|
||||
method :expand_tabs do
|
||||
|
||||
test "0" do
|
||||
expected = <<-EOF
|
||||
|
||||
One tab
|
||||
One space and one tab
|
||||
Six spaces, a tab, and a space
|
||||
EOF
|
||||
@tabs.expand_tabs(0).assert == expected
|
||||
end
|
||||
|
||||
test "1" do
|
||||
expected = <<-EOF
|
||||
|
||||
One tab
|
||||
One space and one tab
|
||||
Six spaces, a tab, and a space
|
||||
EOF
|
||||
@tabs.expand_tabs(1).assert == expected
|
||||
end
|
||||
|
||||
test "4" do
|
||||
expected = <<-EOF
|
||||
|
||||
One tab
|
||||
One space and one tab
|
||||
Six spaces, a tab, and a space
|
||||
EOF
|
||||
@tabs.expand_tabs(4).assert == expected
|
||||
end
|
||||
|
||||
test "8" do
|
||||
expected = <<-EOF
|
||||
|
||||
One tab
|
||||
One space and one tab
|
||||
Six spaces, a tab, and a space
|
||||
EOF
|
||||
@tabs.expand_tabs.assert == expected
|
||||
@tabs.expand_tabs(8).assert == expected
|
||||
end
|
||||
|
||||
test "16" do
|
||||
expected = <<-EOF
|
||||
|
||||
One tab
|
||||
One space and one tab
|
||||
Six spaces, a tab, and a space
|
||||
EOF
|
||||
@tabs.expand_tabs(16).assert == expected
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :expand_tab do
|
||||
|
||||
test "0 (alias for #expand_tabs)" do
|
||||
expected = <<-EOF
|
||||
|
||||
One tab
|
||||
One space and one tab
|
||||
Six spaces, a tab, and a space
|
||||
EOF
|
||||
@tabs.expand_tab(0).assert == expected
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
covers 'facets/string/file'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :file do
|
||||
|
||||
test do
|
||||
"home/trans".file.dirname.assert == "home"
|
||||
"home/trans".file.basename.assert == "trans"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
covers 'facets/string/fold'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :fold do
|
||||
|
||||
test do
|
||||
s = "This is\na test.\n\nIt clumps\nlines of text."
|
||||
o = "This is a test.\n\nIt clumps lines of text."
|
||||
s.fold.assert == o
|
||||
end
|
||||
|
||||
test "leave spaces" do
|
||||
s = "This is\na test.\n\n This is pre.\n Leave alone.\n\nIt clumps\nlines of text."
|
||||
o = "This is a test.\n\n This is pre.\n Leave alone.\n\nIt clumps lines of text."
|
||||
s.fold(true).assert == o
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,90 @@
|
||||
covers 'facets/string/indent'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :indent do
|
||||
|
||||
test "positive" do
|
||||
"xyz".indent(4).assert == ' xyz'
|
||||
" xyz".indent(2).assert == ' xyz'
|
||||
end
|
||||
|
||||
test "multiline positive" do
|
||||
"abc\nxyz".indent(2).assert == " abc\n xyz"
|
||||
end
|
||||
|
||||
test "zero" do
|
||||
'xyz'.indent(0).assert == 'xyz'
|
||||
end
|
||||
|
||||
test "negative" do
|
||||
' xyz'.indent(-2).assert == ' xyz'
|
||||
' xyz'. indent(-2).assert == 'xyz'
|
||||
end
|
||||
|
||||
test "multiline negative" do
|
||||
" abc\n xyz".indent(-2).assert == " abc\n xyz"
|
||||
end
|
||||
|
||||
test "negative more than is possible" do
|
||||
' xyz'.indent(-3).assert == 'xyz'
|
||||
end
|
||||
|
||||
test "non-space positive" do
|
||||
"xyz".indent(4, '-').assert == '----xyz'
|
||||
end
|
||||
|
||||
test "non-space zero" do
|
||||
'xyz'.indent(0, '-').assert == 'xyz'
|
||||
end
|
||||
|
||||
test "non-space negative non-matching character" do
|
||||
' xyz'.indent(-2, '-').assert == ' xyz'
|
||||
' xyz'. indent(-2, '-').assert == ' xyz'
|
||||
end
|
||||
|
||||
test "non-space negative" do
|
||||
'----xyz'.indent(-2, '-').assert == '--xyz'
|
||||
'--xyz'.indent(-2, '-').assert == 'xyz'
|
||||
end
|
||||
|
||||
Concern "Special regular expresion characters are escaped."
|
||||
|
||||
test "ensure '.' is treated literally and not as wildcard" do
|
||||
' xyz'.indent(-2, '.').assert == ' xyz'
|
||||
'..xyz'.indent(-2, '.').assert == 'xyz'
|
||||
end
|
||||
|
||||
test "ensure '*' is treated literally and not as wildcard" do
|
||||
' xyz'.indent(-2, '*').assert == ' xyz'
|
||||
'**xyz'.indent(-2, '*').assert == 'xyz'
|
||||
end
|
||||
|
||||
test "ensure '^' is treated literally and not as line start" do
|
||||
' xyz'.indent(-2, '^').assert == ' xyz'
|
||||
'^^xyz'.indent(-2, '^').assert == 'xyz'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :indent! do
|
||||
|
||||
test "in place rendition of #unindent" do
|
||||
s = "xyz"
|
||||
s.indent!(4)
|
||||
s.assert == ' xyz'
|
||||
|
||||
s = " xyz"
|
||||
s.indent!(2)
|
||||
s.assert == ' xyz'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
#method :unindent do
|
||||
# test "is an alias for negative indent" do
|
||||
# ' xyz'.unindent(2).assert == 'xyz'
|
||||
# end
|
||||
#end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/string/index_all'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :index_all do
|
||||
|
||||
test "with string argument" do
|
||||
"axxxaxxxa".index_all('a').assert == [0,4,8]
|
||||
end
|
||||
|
||||
test "with regexp argument" do
|
||||
"a123a567a9".index_all(/a/).assert == [0,4,8]
|
||||
end
|
||||
|
||||
test "with resue flag as true" do
|
||||
"bbb".index_all('bb', false).assert == ([0])
|
||||
"bbb".index_all('bb', true).assert == ([0,1])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/string/interpolate'
|
||||
|
||||
test_case String do
|
||||
|
||||
class_method :interpolate do
|
||||
|
||||
test do
|
||||
a = 1
|
||||
String.interpolate{ 'this is #{a}' }.assert == 'this is 1'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
covers 'facets/string/lchomp'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :lchomp do
|
||||
|
||||
test do
|
||||
s = "xxboo"
|
||||
r = s.lchomp("xx")
|
||||
r.assert == "boo"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :lchomp! do
|
||||
|
||||
test do
|
||||
s = "xxboo"
|
||||
s.lchomp!("xx")
|
||||
s.assert == "boo"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
covers 'facets/string/line_wrap'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :line_wrap do
|
||||
|
||||
test do
|
||||
"abc123".line_wrap(3).assert == "abc\n123\n"
|
||||
"abcd123".line_wrap(4).assert == "abcd\n123\n"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
covers 'facets/string/lowercase'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :lowercase do
|
||||
|
||||
test do
|
||||
"Bob".lowercase.assert == "bob"
|
||||
end
|
||||
|
||||
test do
|
||||
"BOB".lowercase.assert == "bOB"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/string/margin'
|
||||
|
||||
test_case String do
|
||||
|
||||
# TODO: Need to expand margin tests
|
||||
method :margin do
|
||||
|
||||
test do
|
||||
a = "xyz".margin(4)
|
||||
a[0..3].assert == ' '
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
covers 'facets/string/methodize'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :methodize do
|
||||
|
||||
test "module name" do
|
||||
a = "FooBase"
|
||||
r = "foo_base"
|
||||
a.methodize.assert == r
|
||||
end
|
||||
|
||||
test "path name" do
|
||||
a = "foo/base"
|
||||
r = "foo__base"
|
||||
a.methodize.assert == r
|
||||
end
|
||||
|
||||
test "name space" do
|
||||
a = "Foo::Base"
|
||||
r = "foo__base"
|
||||
a.methodize.assert == r
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
covers 'facets/string/modulize'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :modulize do
|
||||
|
||||
test "snakecase" do
|
||||
a = "foo_bar"
|
||||
r = "FooBar"
|
||||
a.modulize.assert == r
|
||||
end
|
||||
|
||||
test "path name" do
|
||||
a = "foo/bar"
|
||||
r = "Foo::Bar"
|
||||
a.modulize.assert == r
|
||||
end
|
||||
|
||||
test "on methodized string" do
|
||||
a = "foo__bar"
|
||||
r = "Foo::Bar"
|
||||
a.modulize.assert == r
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
covers 'facets/string/mscan'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :mscan do
|
||||
|
||||
test do
|
||||
r = 'abc,def,gh'.mscan(/[,]/)
|
||||
assert( r.all?{ |md| MatchData === md } )
|
||||
r.to_a.length.assert == 2
|
||||
r[0][0].assert == ','
|
||||
r[1][0].assert == ','
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/string/natcmp'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :natcmp do
|
||||
|
||||
test do
|
||||
"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 == 1
|
||||
"my_prog_v1.0.0".natcmp( "my_prog_v1.0.0").assert == 0
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/string/nchar'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :nchar do
|
||||
|
||||
test do
|
||||
"abcxyz".nchar(3).assert == "abc"
|
||||
"abcxyz".nchar(-3).assert == "xyz"
|
||||
"abcxyz".nchar(3, 'HI').assert == "HIxyz"
|
||||
"abcxyz".nchar(-3, 'HI').assert == "abcHI"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
covers 'facets/string/newlines'
|
||||
|
||||
test_case String do
|
||||
|
||||
# Since Ruby 1.9 defined #lines to keep the newline character,
|
||||
# we have defined #newlines to leave them out.
|
||||
method :newlines do
|
||||
test do
|
||||
"a\nb\nc".newlines.to_a.assert == ["a","b","c"]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
covers 'facets/string/number'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :number? do
|
||||
|
||||
test do
|
||||
x = "1.23\n"
|
||||
x.number?.assert == true
|
||||
end
|
||||
|
||||
test do
|
||||
x = "123_456_789_123_456_789.123_456_000_111\n"
|
||||
x.number?.assert == true
|
||||
end
|
||||
|
||||
test do
|
||||
x = ".123\n"
|
||||
x.number?.assert == false
|
||||
end
|
||||
|
||||
test do
|
||||
x = "123_\n"
|
||||
x.number?.assert == false
|
||||
end
|
||||
|
||||
test do
|
||||
x = "_123\n"
|
||||
x.number?.assert == false
|
||||
end
|
||||
|
||||
test do
|
||||
x = "23_456_789_123_456_.245\n"
|
||||
x.number?.assert == false
|
||||
end
|
||||
|
||||
test do
|
||||
x = "23_456_789_123_456._245\n"
|
||||
x.number?.assert == false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
covers 'facets/string/op_div'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :/ do
|
||||
|
||||
test do
|
||||
('merb' / 'string').assert == "merb/string"
|
||||
('merb' / :symbol).assert == "merb/symbol"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
covers 'facets/string/op_sub'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :"-" do
|
||||
|
||||
test do
|
||||
s = "xxbooxxfoo"
|
||||
(s - "xx").assert == "boofoo"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
covers 'facets/string/pathize'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :pathize do
|
||||
|
||||
test "module name" do
|
||||
a = "Foo::Base"
|
||||
x = "foo/base"
|
||||
a.pathize.assert == x
|
||||
end
|
||||
|
||||
test "path name" do
|
||||
a = "foo/base"
|
||||
x = "foo/base"
|
||||
a.pathize.assert == x
|
||||
end
|
||||
|
||||
test "name space" do
|
||||
a = "foo__base"
|
||||
x = "foo/base"
|
||||
a.pathize.assert == x
|
||||
end
|
||||
|
||||
test 'transforms initial double colon' do
|
||||
'::MyClass'.pathize.assert == '/my_class'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,41 @@
|
||||
covers 'facets/string/quote'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :quote do
|
||||
|
||||
test "counting quotes" do
|
||||
'hi'.quote(1).assert == %{'hi'}
|
||||
'hi'.quote(2).assert == %{"hi"}
|
||||
'hi'.quote(3).assert == %{'"hi"'}
|
||||
'hi'.quote(4).assert == %{""hi""}
|
||||
'hi'.quote(5).assert == %{'""hi""'}
|
||||
end
|
||||
|
||||
test "default double quotes" do
|
||||
'hi'.quote.assert == %{"hi"}
|
||||
end
|
||||
|
||||
test "single quotes" do
|
||||
'hi'.quote(:s).assert == %{'hi'}
|
||||
'hi'.quote(:single).assert == %{'hi'}
|
||||
end
|
||||
|
||||
test "double quotes" do
|
||||
'hi'.quote(:d).assert == %{"hi"}
|
||||
'hi'.quote(:double).assert == %{"hi"}
|
||||
end
|
||||
|
||||
test "back quotes" do
|
||||
'hi'.quote(:b).assert == %{`hi`}
|
||||
'hi'.quote(:back).assert == %{`hi`}
|
||||
end
|
||||
|
||||
test "bracket quotes" do
|
||||
'hi'.quote(:sb).assert == "`hi'"
|
||||
'hi'.quote(:bracket).assert == "`hi'"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
covers 'facets/string/random'
|
||||
|
||||
test_case String do
|
||||
class_method :random do
|
||||
test do
|
||||
r = /^[0-9a-zA-Z]{32}$/
|
||||
a = String.random
|
||||
r.assert =~ a
|
||||
end
|
||||
|
||||
test 'control the length' do
|
||||
r = /^[0-9a-zA-Z]{17}$/
|
||||
a = String.random(17)
|
||||
r.assert =~ a
|
||||
end
|
||||
|
||||
test 'control the character_set' do
|
||||
r = /^[IAMBatman]{12}$/
|
||||
|
||||
a = String.random(12, ['IAMBatman'.chars])
|
||||
r.assert =~ a
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
covers 'facets/string/range'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :range do
|
||||
|
||||
test do
|
||||
"a123a567a9".range(/123/).assert == (1..3)
|
||||
"a123a567a9".range(/a/).assert == (0..0)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :range_all do
|
||||
|
||||
test do
|
||||
"a123a123a9".range_all(/123/).assert == [ (1..3), (5..7) ]
|
||||
"a123a567a9".range_all(/a/).assert == [ (0..0), (4..4), (8..8) ]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :range_of_line do
|
||||
|
||||
test do
|
||||
a = "0123\n456\n78"
|
||||
x = [0..4, 5..8, 9..10]
|
||||
r = a.range_of_line
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
covers 'facets/string/remove'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :remove do
|
||||
|
||||
test "remove string" do
|
||||
s = "xxbooxxfoo"
|
||||
s.remove("xx").assert == "boofoo"
|
||||
end
|
||||
|
||||
test "remove regex" do
|
||||
s = "xbooxxfoox"
|
||||
s.remove(/x{1,2}/).assert == "boofoo"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :remove! do
|
||||
|
||||
test "remove string" do
|
||||
s = "xxbooxxfoo"
|
||||
s.remove!("xx")
|
||||
s.assert == "boofoo"
|
||||
end
|
||||
|
||||
test "remove regex" do
|
||||
s = "xbooxxfoox"
|
||||
s.remove!(/x{1,2}/)
|
||||
s.assert == "boofoo"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :- do
|
||||
|
||||
test "remove string" do
|
||||
s = "xxbooxxfoo"
|
||||
(s - "xx").assert == "boofoo"
|
||||
end
|
||||
|
||||
test "remove regex" do
|
||||
s = "xbooxxfoox"
|
||||
(s - /x{1,2}/).assert == "boofoo"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/string/rewrite'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :rewrite do
|
||||
|
||||
test do
|
||||
s = "HELLO TOMMY!"
|
||||
rules = [[ /TOMMY/, 'MAN' ]]
|
||||
r = s.rewrite(rules)
|
||||
r.assert == "HELLO MAN!"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
covers 'facets/string/rotate'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :rotate do
|
||||
|
||||
test 'rotates left' do
|
||||
'abcdefgh'.rotate(2).assert == 'cdefghab'
|
||||
end
|
||||
test 'rotates right' do
|
||||
'abcdefgh'.rotate(-2).assert == 'ghabcdef'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :rotate! do
|
||||
|
||||
test 'rotates left' do
|
||||
s='abcdefgh'
|
||||
s.rotate!(2)
|
||||
s.assert == 'cdefghab'
|
||||
end
|
||||
test 'rotates right' do
|
||||
s='abcdefgh'
|
||||
s.rotate!(-2)
|
||||
s.assert == 'ghabcdef'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/string/shatter'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :shatter do
|
||||
|
||||
test do
|
||||
s = "<p>This<b>is</b>a test.</p>"
|
||||
r = s.shatter( /<.*?>/ )
|
||||
x = ["<p>", "This", "<b>", "is", "</b>", "a test.", "</p>"]
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/string/similarity'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :similarity do
|
||||
|
||||
test do
|
||||
"Alexsanders".similarity("Aleksander").assert == 0.75
|
||||
end
|
||||
|
||||
test do
|
||||
"Alexander".similarity("Alexander").assert == 1.0
|
||||
end
|
||||
|
||||
test do
|
||||
"Alexander".similarity("").assert == 0.0
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/string/snakecase'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :snakecase do
|
||||
|
||||
test 'dowcases' do
|
||||
'URI'.snakecase.assert == 'uri'
|
||||
end
|
||||
|
||||
test 'converts spaces' do
|
||||
'MyStuff HisStuff'.snakecase.assert == 'my_stuff_his_stuff'
|
||||
end
|
||||
|
||||
test 'does not alter path dividers' do
|
||||
'/my_module/my_class/'.snakecase.assert == '/my_module/my_class/'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
covers 'facets/string/splice'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :splice do
|
||||
|
||||
test do
|
||||
a = "HELLO"
|
||||
a.splice(1).assert == "E"
|
||||
a.assert == "HLLO"
|
||||
end
|
||||
|
||||
test "range" do
|
||||
a = "HELLO"
|
||||
a.splice(1..2).assert == "EL"
|
||||
a.assert == "HLO"
|
||||
end
|
||||
|
||||
test "store" do
|
||||
a = "HELLO"
|
||||
a.splice(1, "X")
|
||||
a.assert == "HXLLO"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# This could be done if class of 2nd arg is checked.
|
||||
#method :splice_length
|
||||
# test do
|
||||
# a = "HELLO"
|
||||
# a.splice(1,2).assert == "EL"
|
||||
# a.assert == "HLO"
|
||||
# end
|
||||
#end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
covers 'facets/string/squish'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :squish do
|
||||
|
||||
test do
|
||||
r = %{ Multi-line
|
||||
string }.squish
|
||||
r.assert == "Multi-line string"
|
||||
end
|
||||
|
||||
test do
|
||||
r = " foo bar \n \t boo".squish
|
||||
r.assert == "foo bar boo"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :squish! do
|
||||
|
||||
test do
|
||||
s = " foo bar \n \t boo"
|
||||
s.squish!
|
||||
s.assert == "foo bar boo"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/string/store'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :store do
|
||||
|
||||
test do
|
||||
a = "HELLO"
|
||||
a.store(1, "X")
|
||||
a.assert == "HXLLO"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/string/subtract'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :- do
|
||||
|
||||
test do
|
||||
("foobar" - "oo").assert == "fbar"
|
||||
("pizza pizza!" - "zz").assert == "pia pia!"
|
||||
("letters" - /[a-z]+/).assert == ""
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/string/titlecase'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :titlecase do
|
||||
|
||||
test do
|
||||
r = "try this out".titlecase
|
||||
x = "Try This Out"
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
covers 'facets/string/to_re'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :to_re do
|
||||
|
||||
test do
|
||||
"abc".to_re.assert == /abc/
|
||||
"a+bc".to_re.assert == /a+bc/
|
||||
"a+bc".to_re(false).assert == /a+bc/
|
||||
"a+bc".to_re(true).assert == /a\+bc/
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :to_rx do
|
||||
|
||||
test do
|
||||
"abc".to_rx.assert == /abc/
|
||||
"a+bc".to_rx.assert == /a\+bc/
|
||||
"a+bc".to_rx(false).assert == /a+bc/
|
||||
"a+bc".to_rx(true).assert == /a\+bc/
|
||||
end
|
||||
|
||||
test "escaped characters" do
|
||||
a = "?"
|
||||
b = /#{a.to_rx}/
|
||||
b.assert =~ "?"
|
||||
b.refute =~ "X"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
covers 'facets/string/trim'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :trim do
|
||||
|
||||
test do
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
end
|
||||
|
||||
test do
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
end
|
||||
|
||||
test do
|
||||
s = %q{|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
end
|
||||
|
||||
test do
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
end
|
||||
|
||||
test do
|
||||
s = %q{|ABC
|
||||
|123
|
||||
|TEST}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
end
|
||||
|
||||
test do
|
||||
s = %q{ |ABC
|
||||
|123
|
||||
|TEST}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
end
|
||||
|
||||
test do
|
||||
s = %q{ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim
|
||||
s.assert == "ABC\n123\nTEST"
|
||||
end
|
||||
|
||||
test "spacing" do
|
||||
s = %q{
|
||||
| ABC
|
||||
| 123
|
||||
| TEST
|
||||
}.trim
|
||||
s.assert == " ABC\n 123\n TEST"
|
||||
end
|
||||
|
||||
test "spacing" do
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim(1)
|
||||
s.assert == " ABC\n 123\n TEST"
|
||||
end
|
||||
|
||||
test "spacing" do
|
||||
s = %q{
|
||||
|ABC
|
||||
|123
|
||||
|TEST
|
||||
}.trim(2)
|
||||
s.assert == " ABC\n 123\n TEST"
|
||||
end
|
||||
|
||||
test "spacing" do
|
||||
s = %q{ ABC
|
||||
- 123
|
||||
- TEST
|
||||
}.trim
|
||||
s.assert == " ABC\n 123\n TEST"
|
||||
end
|
||||
|
||||
test "random placement" do
|
||||
volly = {}
|
||||
100.times{ |n|
|
||||
k = []
|
||||
a = []
|
||||
5.times{ |i|
|
||||
k << ( ( ' ' * Integer(rand*10) ) + '|' + i.to_s )
|
||||
a << ( i.to_s )
|
||||
}
|
||||
volly[k.join("\n")] = a.join("\n")
|
||||
}
|
||||
volly.each{ |k,v|
|
||||
k.trim.assert == v
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
covers 'facets/string/unbracket'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :unbracket do
|
||||
|
||||
test do
|
||||
s = "{unwrap me}"
|
||||
z = s.unbracket('{')
|
||||
z.assert == "unwrap me"
|
||||
end
|
||||
|
||||
test do
|
||||
s = "--unwrap me!"
|
||||
z = s.unbracket('--','!')
|
||||
z.assert == "unwrap me"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :unbracket! do
|
||||
|
||||
test do
|
||||
s = "{unwrap me}"
|
||||
s.unbracket!('{')
|
||||
s.assert == "unwrap me"
|
||||
end
|
||||
|
||||
test do
|
||||
s = "--unwrap me!"
|
||||
s.unbracket!('--','!')
|
||||
s.assert == "unwrap me"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
covers 'facets/string/underscore'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :underscore do
|
||||
|
||||
test "from camelcase" do
|
||||
"SnakeCase".underscore.assert == "snake_case"
|
||||
end
|
||||
|
||||
test "containing a dash" do
|
||||
"Snake-Case".underscore.assert == "snake_case"
|
||||
end
|
||||
|
||||
test "containing spaces" do
|
||||
"Snake Case".underscore.assert == "snake_case"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/string/unfold'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :unfold do
|
||||
|
||||
test do
|
||||
str = "This is a test of\nparagraph folding."
|
||||
res = str.unfold
|
||||
exp = "This is a test of paragraph folding."
|
||||
res.assert == exp
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
covers 'facets/string/unindent'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :unindent do
|
||||
|
||||
Concern "Single-line indentation"
|
||||
|
||||
test "removes space indentation" do
|
||||
"\s\sabc".unindent.assert == "abc"
|
||||
end
|
||||
|
||||
test "removes_tab_indentation" do
|
||||
"\tabc".unindent.assert == "abc"
|
||||
end
|
||||
|
||||
test "removes space and tab indentation" do
|
||||
"\t\sabc".unindent.assert == "abc"
|
||||
end
|
||||
|
||||
Concern "Multi-line indentation"
|
||||
|
||||
test "removes indentation" do
|
||||
"\tabc\n\tabc".unindent.assert == "abc\nabc"
|
||||
end
|
||||
|
||||
test "keeps relative indentation" do
|
||||
"\tabc\n\t\tabc".unindent.assert == "abc\n\tabc"
|
||||
end
|
||||
|
||||
test "ignores blank lines for indent calculation" do
|
||||
"\n\tabc\n\n\t\tabc\n".unindent.assert == "\nabc\n\n\tabc\n"
|
||||
end
|
||||
|
||||
Concern "Complex examples"
|
||||
|
||||
ex1 = <<-EOF
|
||||
I must go down to the seas again
|
||||
The lonely sea and the sky
|
||||
And all I want is a tall ship
|
||||
And a star to steer her by
|
||||
EOF
|
||||
|
||||
ex2 = <<-EOF
|
||||
"Eek!"
|
||||
She cried
|
||||
As the mouse quietly scurried
|
||||
by.
|
||||
EOF
|
||||
|
||||
test "simple examples" do
|
||||
" xyz".unindent(-1).assert == " xyz"
|
||||
" xyz".unindent(0).assert == " xyz"
|
||||
" xyz".unindent(1).assert == " xyz"
|
||||
" xyz".unindent(2).assert == " xyz"
|
||||
" xyz".unindent(3).assert == "xyz"
|
||||
" xyz".unindent(4).assert == "xyz"
|
||||
end
|
||||
|
||||
test "large example unindented one space" do
|
||||
expected = <<-EOF
|
||||
I must go down to the seas again
|
||||
The lonely sea and the sky
|
||||
And all I want is a tall ship
|
||||
And a star to steer her by
|
||||
EOF
|
||||
actual = ex1.unindent(1)
|
||||
actual.assert == expected
|
||||
end
|
||||
|
||||
test "large example unindented four spaces" do
|
||||
expected = <<-EOF
|
||||
I must go down to the seas again
|
||||
The lonely sea and the sky
|
||||
And all I want is a tall ship
|
||||
And a star to steer her by
|
||||
EOF
|
||||
actual = ex1.unindent(4)
|
||||
actual.assert == expected
|
||||
end
|
||||
|
||||
test "unindent larger than current indention" do
|
||||
expected = <<-EOF
|
||||
"Eek!"
|
||||
She cried
|
||||
As the mouse quietly scurried
|
||||
by.
|
||||
EOF
|
||||
actual = ex2.unindent(100)
|
||||
actual.assert == expected
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :unindent! do
|
||||
|
||||
test "modifies string in place" do
|
||||
str = "\s\sabc"
|
||||
str.unindent!
|
||||
str.assert == "abc"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
covers 'facets/string/unquote'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :unquote do
|
||||
|
||||
test do
|
||||
"'here'".unquote.assert == "here"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/string/uppercase'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :uppercase do
|
||||
|
||||
test do
|
||||
"bob".uppercase.assert == "Bob"
|
||||
end
|
||||
|
||||
test do
|
||||
"boB".uppercase.assert == "BoB"
|
||||
end
|
||||
|
||||
test do
|
||||
"BoB".uppercase.assert == "BoB"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
covers 'facets/string/variablize'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :variablize do
|
||||
|
||||
test do
|
||||
s = "a"
|
||||
s.variablize.assert == "@a"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
covers 'facets/string/word_wrap'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :word_wrap do
|
||||
|
||||
test do
|
||||
e = "abcde\n12345\nxyzwu\n"
|
||||
r = "abcde 12345 xyzwu".word_wrap(5)
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
test do
|
||||
e = "abcd\n1234\nxyzw\n"
|
||||
r = "abcd 1234 xyzw".word_wrap(4)
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
test do
|
||||
e = "abc\n123\n"
|
||||
r = "abc 123".word_wrap(4)
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
test do
|
||||
e = "abc \n123\n"
|
||||
r = "abc 123".word_wrap(4)
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
test do
|
||||
e = "abc \n123\n"
|
||||
r = "abc 123".word_wrap(4)
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
# test do
|
||||
# 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
|
||||
|
||||
method :word_wrap! do
|
||||
|
||||
test do
|
||||
w = "abcde 12345 xyzwu"
|
||||
w.word_wrap!(5)
|
||||
w.assert == "abcde\n12345\nxyzwu\n"
|
||||
end
|
||||
|
||||
test do
|
||||
w = "abcd 1234 xyzw"
|
||||
w.word_wrap!(4)
|
||||
w.assert == "abcd\n1234\nxyzw\n"
|
||||
end
|
||||
|
||||
test do
|
||||
w = "abc 123"
|
||||
w.word_wrap!(4)
|
||||
w.assert == "abc\n123\n"
|
||||
end
|
||||
|
||||
test do
|
||||
w = "abc 123"
|
||||
w.word_wrap!(4)
|
||||
w.assert == "abc \n123\n"
|
||||
end
|
||||
|
||||
test do
|
||||
w = "abc 123"
|
||||
w.word_wrap!(4)
|
||||
w.assert == "abc \n123\n"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
covers 'facets/string/words'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :words do
|
||||
|
||||
test do
|
||||
x = "a b c\nd e"
|
||||
x.words.assert == ['a','b','c','d','e']
|
||||
end
|
||||
|
||||
test do
|
||||
x = "ab cd\nef"
|
||||
x.words.assert == ['ab','cd','ef']
|
||||
end
|
||||
|
||||
test do
|
||||
x = "ab cd \n ef-gh"
|
||||
x.words.assert == ['ab','cd','ef-gh']
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/string/xor'
|
||||
|
||||
test_case String do
|
||||
|
||||
method :'^' do
|
||||
|
||||
test do
|
||||
r = "try this out" ^ "puddingsnacks"
|
||||
x = "\004\a\035D\035\006\016\000N\016\026\037"
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user