# encoding: utf-8 ## test/console.rb -- unit tests for ruby Console library ## Author: William Morgan (mailto: wmorgan-ruby-console@masanjin.net) ## Copyright: Copyright 2010 William Morgan ## License: same terms as Ruby itself require 'test/unit' require 'console' class ConsoleTest < ::Test::Unit::TestCase def setup @s = "能吞aê" end def test_slice_of_zero_width_is_empty_string assert_equal "", Console.display_slice(@s, 0, 0) assert_equal "", Console.display_slice(@s, 1, 0) end def test_slice_out_of_bounds_is_nil assert_equal nil, Console.display_slice(@s, 100, 3) assert_equal nil, Console.display_slice(@s, -100, 3) end def test_slice_with_negative_offset assert_equal "ê", Console.display_slice(@s, -1, 1) assert_equal "aê", Console.display_slice(@s, -2, 2) assert_equal "a", Console.display_slice(@s, -2, 1) end def test_slice_width_argument_defaults_to_1 assert_equal "ê", Console.display_slice(@s, -1) assert_equal "a", Console.display_slice(@s, -2) end def test_slice_works_on_chinese_characters assert_equal "能", Console.display_slice(@s, 0, 2); assert_equal "能吞", Console.display_slice(@s, 0, 4); assert_equal "能吞a", Console.display_slice(@s, 0, 5); assert_equal "能吞aê", Console.display_slice(@s, 0, 6); end def test_slice_with_excessive_width_is_still_cool assert_equal "能吞aê", Console.display_slice(@s, 0, 100); assert_equal "吞aê", Console.display_slice(@s, 2, 100); assert_equal "aê", Console.display_slice(@s, 4, 100); assert_equal "ê", Console.display_slice(@s, 5, 100); assert_equal "", Console.display_slice(@s, 6, 100); # yep, we get a non-nil at this value assert_equal nil, Console.display_slice(@s, 7, 100); end def test_slice_with_the_biggest_valid_start_offset_behaves_like_String_slice_does assert_equal "", Console.display_slice(@s, 6, 100); assert_equal "", Console.display_slice(@s, 6, 0); assert_equal nil, Console.display_slice(@s, 6, -1); end def test_slice_misaligned_start_offsets_get_padded s = "能吞aê" assert_equal "", Console.display_slice(@s, 0, 0) assert_equal " ", Console.display_slice(@s, 0, 1) assert_equal "能", Console.display_slice(@s, 0, 2) assert_equal "能 ", Console.display_slice(@s, 0, 3) assert_equal "", Console.display_slice(@s, 1, 0) assert_equal " ", Console.display_slice(@s, 1, 1) assert_equal " ", Console.display_slice(@s, 1, 2) assert_equal " 吞", Console.display_slice(@s, 1, 3) assert_equal "", Console.display_slice(@s, 3, 0); assert_equal " ", Console.display_slice(@s, 3, 1); assert_equal " a", Console.display_slice(@s, 3, 2); assert_equal " aê", Console.display_slice(@s, 3, 3); end def test_slice_misaligned_start_offsets_get_padded_with_specified_character assert_equal "", Console.display_slice(@s, 0, 0, "X") assert_equal "X", Console.display_slice(@s, 0, 1, "X") assert_equal "XX", Console.display_slice(@s, 1, 2, "X") end def test_slice_fails_on_nonstrings assert_raises(TypeError) { Console.display_slice :potato, 1, 1 } assert_raises(TypeError) { Console.display_slice 3, 1, 1 } end def test_display_width_empty_string_is_zero assert_equal 0, Console.display_width("") end def test_display_width_works_on_ASCII_strings assert_equal 1, Console.display_width("a") assert_equal 1, Console.display_width(" ") assert_equal 6, Console.display_width("potato") end def test_display_width_works_on_accented_characters assert_equal 1, Console.display_width("ê") assert_equal 4, Console.display_width("êêêê") end def test_display_width_works_on_chinese_characters assert_equal 2, Console.display_width("能") assert_equal 4, Console.display_width("能吞") end def test_display_width_works_on_mixed_stuff assert_equal 2, Console.display_width("aê") assert_equal 5, Console.display_width("能吞a") assert_equal 6, Console.display_width("能吞aê") assert_equal 6, Console.display_width("aê能吞") assert_equal 6, Console.display_width("a能ê吞") assert_equal 6, Console.display_width("能aê吞") end def test_display_width_fails_on_nonstrings assert_raises(TypeError) { Console.display_width :potato } assert_raises(TypeError) { Console.display_width 3 } end end