1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
# frozen_string_literal: true
require 'minitest/autorun'
require "#{File.dirname(__FILE__)}/../lib/colorized_string"
class TestColorizedString < Minitest::Test
def test_colorize_not_required
refute_respond_to '', :colorize, 'Colorize should not be required. Only colorized_string'
end
def test_blue_symbol
assert_equal "\033[0;34;49mThis is blue\033[0m", ColorizedString['This is blue'].colorize(:blue)
end
def test_incorrect_symbol
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", ColorizedString['This is incorrect color'].colorize(:bold)
end
def test_incorrect_hash
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", ColorizedString['This is incorrect color'].colorize(:color => :bold)
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", ColorizedString['This is incorrect color'].colorize(:mode => :green)
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", ColorizedString['This is incorrect color'].colorize(:background => :bold)
end
def test_blue_hash
assert_equal "\033[0;34;49mThis is also blue\033[0m", ColorizedString['This is also blue'].colorize(:color => :blue)
end
def test_light_blue_symbol
assert_equal "\033[0;94;49mThis is light blue\033[0m", ColorizedString['This is light blue'].colorize(:light_blue)
end
def test_light_blue_with_red_background_hash
assert_equal "\033[0;94;41mThis is light blue with red background\033[0m", ColorizedString['This is light blue with red background'].colorize(:color => :light_blue, :background => :red)
end
def test_light_blue_with_red_background_symbol_and_hash
assert_equal "\033[0;94;41mThis is light blue with red background\033[0m", ColorizedString['This is light blue with red background'].colorize(:light_blue).colorize(:background => :red)
end
def test_blue_with_red_background_method
assert_equal "\033[0;34;41mThis is blue text on red\033[0m", ColorizedString['This is blue text on red'].blue.on_red
end
def test_red_with_blue_background_symbol_and_method
assert_equal "\033[0;31;44mThis is red on blue\033[0m", ColorizedString['This is red on blue'].colorize(:red).on_blue
end
def test_red_with_blue_background_and_underline_sumbol_and_methods
assert_equal "\033[4;31;44mThis is red on blue and underline\033[0m", ColorizedString['This is red on blue and underline'].colorize(:red).on_blue.underline
end
def test_blue_with_red_background_and_blink_methods
assert_equal "\033[5;34;41mThis is blue text on red\033[0m", ColorizedString['This is blue text on red'].blue.on_red.blink
end
def test_uncolorize
assert_equal 'This is uncolorized', ColorizedString['This is uncolorized'].blue.on_red.uncolorize
end
def test_colorized?
assert_predicate ColorizedString['Red'].red, :colorized?
refute_predicate ColorizedString['Blue'], :colorized?
refute_predicate ColorizedString['Green'].blue.green.uncolorize, :colorized?
end
def test_concatenated__colorize?
assert_predicate ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"], :colorized?
refute_predicate ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].uncolorize, :colorized?
end
def test_concatenated_strings_on_green
assert_equal "\033[0;39;42mnone \033[0m\033[0;31;42mred\033[0m\033[0;39;42m none \033[0m\033[0;34;42mblue\033[0m\033[0;39;42m none\033[0m", ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].on_green
end
def test_concatenated_strings_uncolorize
assert_equal 'none red none blue none', ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].uncolorize
end
def test_frozen_strings
assert_equal "\033[5;34;41mThis is blue text on red\033[0m", ColorizedString['This is blue text on red'].freeze.blue.on_red.blink
end
def test_new_line
assert_equal "\033[5;34;41mThis is blue\ntext on red\033[0m", ColorizedString["This is blue\ntext on red"].freeze.blue.on_red.blink
end
def test_disable_colorization_with_=
ColorizedString.disable_colorization = true
assert ColorizedString.disable_colorization
ColorizedString.disable_colorization = false
end
def test_disable_colorization_with_method
ColorizedString.disable_colorization true
assert ColorizedString.disable_colorization
ColorizedString.disable_colorization false
end
def test_string_disable_colorization_with_=
ColorizedString.disable_colorization = true
assert ColorizedString.disable_colorization
assert_equal 'This is blue after disabling', ColorizedString['This is blue after disabling'].blue
ColorizedString.disable_colorization = false
assert_equal "\033[0;34;49mThis is blue after enabling\033[0m", ColorizedString['This is blue after enabling'].colorize(:blue)
end
def test_string_disable_colorization_with_method
assert_equal "\033[0;34;49mThis is blue before disabling\033[0m", ColorizedString['This is blue before disabling'].colorize(:blue)
ColorizedString.disable_colorization true
assert ColorizedString.disable_colorization
assert_equal 'This is blue after disabling', ColorizedString['This is blue after disabling'].blue
ColorizedString.disable_colorization false
assert_equal "\033[0;34;49mThis is blue after enabling\033[0m", ColorizedString['This is blue after enabling'].colorize(:blue)
end
def test_already_colored_string_with_one_value
assert_equal ColorizedString['This is red'].red, ColorizedString["\033[31mThis is red\033[0m"].red
end
def test_color_matrix_method
assert_raises(NoMethodError) { ColorizedString.color_matrix }
end
def test_color_samples_method
assert_output do
ColorizedString.color_samples
end
end
def test_grey_alias
assert_equal ColorizedString['This is grey'].grey, ColorizedString['This is grey'].light_black
end
def test_gray_alias
assert_equal ColorizedString['This is gray'].gray, ColorizedString['This is gray'].light_black
end
def test_add_color_alias
ColorizedString.add_color_alias(:extra_blue, :light_blue)
assert_equal ColorizedString['blue'].light_blue, ColorizedString['blue'].extra_blue
assert_equal ColorizedString['blue'].on_light_blue, ColorizedString['blue'].on_extra_blue
assert_raises ::Colorize::ColorAlreadyExist, 'Colorize: color :extra_blue already exist!' do
ColorizedString.add_color_alias(:extra_blue, :light_color)
end
assert_raises ::Colorize::ColorDontExist, 'Colorize: color :light_color don\'t exist!' do
ColorizedString.add_color_alias(:extra_white, :light_color)
end
end
def test_add_color_alias_with_single_hash
ColorizedString.add_color_alias(extra_green: :light_green)
assert_equal ColorizedString['example string'].light_green, ColorizedString['example string'].extra_green
assert_equal ColorizedString['example string'].on_light_green, ColorizedString['example string'].on_extra_green
end
def test_add_color_alias_with_single_hash_with_arrow
ColorizedString.add_color_alias(:extra_color => :gray)
assert_equal ColorizedString['example string'].gray, ColorizedString['example string'].extra_color
assert_equal ColorizedString['example string'].on_gray, ColorizedString['example string'].on_extra_color
end
def test_add_color_alias_with_multi_hash
ColorizedString.add_color_alias(extra_color_a: :gray, extra_color_b: :blue)
assert_equal ColorizedString['example string'].gray, ColorizedString['example string'].extra_color_a
assert_equal ColorizedString['example string'].blue, ColorizedString['example string'].extra_color_b
end
def test_add_color_alias_with_multi_hash_with_arrow
ColorizedString.add_color_alias(:extra_color_c => :gray, :extra_color_d => :blue)
assert_equal ColorizedString['example string'].gray, ColorizedString['example string'].extra_color_c
assert_equal ColorizedString['example string'].on_blue, ColorizedString['example string'].on_extra_color_d
end
def test_add_color_alias_with_multi_hash_mixed
ColorizedString.add_color_alias(extra_color_e: :gray, :extra_color_f => :blue)
assert_equal ColorizedString['example string'].gray, ColorizedString['example string'].extra_color_e
assert_equal ColorizedString['example string'].on_blue, ColorizedString['example string'].on_extra_color_f
end
def test_prevent_colors
ColorizedString.prevent_colors true
assert ColorizedString.prevent_colors
assert_equal "#{ColorizedString['blue'].blue}#{ColorizedString['red'].red}#{ColorizedString['green'].green}", ColorizedString["#{ColorizedString['blue'].blue}red#{ColorizedString['green'].green}"].red
ColorizedString.prevent_colors false
end
def test_colorized_string_without_readline
assert_equal ColorizedString["\e[0;31;49mgreen\e[0m"].green, ColorizedString['green'].green
end
end
|