* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / test / -ext- / string / test_ellipsize.rb
blobd340abd58a5a8bf3f96d6e0ffcc50e3dc2fb5750
1 # frozen_string_literal: false
2 require 'test/unit'
3 require "-test-/string"
5 class Test_StringEllipsize < Test::Unit::TestCase
6   def setup
7     @foobar = Bug::String.new("foobar")
8   end
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
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
25   def test_shorter
26     assert_same(@foobar, @foobar.ellipsize(6))
27     assert_same(@foobar, @foobar.ellipsize(7))
28   end
30   def test_negative_length
31     assert_raise(IndexError) {@foobar.ellipsize(-1)}
32   end
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