| 1 |
# frozen_string_literal: false |
| 2 |
require 'test/unit' |
| 3 |
require "-test-/string" |
| 4 |
|
| 5 |
class Test_StringEllipsize < Test::Unit::TestCase |
| 6 |
def setup |
| 7 |
@foobar = Bug::String.new("foobar") |
| 8 |
end |
| 9 |
|
| 10 |
def assert_equal_with_class(expected, result, *rest) |
| 11 |
assert_equal(expected.encoding, result.encoding, *rest) |
| 12 |
assert_equal(expected, result, result.encoding.name) |
| 13 |
assert_instance_of(String, result, *rest) |
| 14 |
end |
| 15 |
|
| 16 |
def test_longer |
| 17 |
assert_equal_with_class("", @foobar.ellipsize(0)) |
| 18 |
assert_equal_with_class(".", @foobar.ellipsize(1)) |
| 19 |
assert_equal_with_class("..", @foobar.ellipsize(2)) |
| 20 |
assert_equal_with_class("...", @foobar.ellipsize(3)) |
| 21 |
assert_equal_with_class("f...", @foobar.ellipsize(4)) |
| 22 |
assert_equal_with_class("fo...", @foobar.ellipsize(5)) |
| 23 |
end |
| 24 |
|
| 25 |
def test_shorter |
| 26 |
assert_same(@foobar, @foobar.ellipsize(6)) |
| 27 |
assert_same(@foobar, @foobar.ellipsize(7)) |
| 28 |
end |
| 29 |
|
| 30 |
def test_negative_length |
| 31 |
assert_raise(IndexError) {@foobar.ellipsize(-1)} |
| 32 |
end |
| 33 |
|
| 34 |
def test_nonascii |
| 35 |
a = "\u3042" |
| 36 |
Encoding.list.each do |enc| |
| 37 |
next if enc.dummy? |
| 38 |
begin |
| 39 |
s = a.encode(enc) |
| 40 |
e = "...".encode(enc) |
| 41 |
rescue |
| 42 |
else |
| 43 |
assert_equal_with_class(s*12+e, Bug::String.new(s*20).ellipsize(15)) |
| 44 |
end |
| 45 |
end |
| 46 |
end |
| 47 |
end |