File: test_colorize.rb

package info (click to toggle)
ruby-colorize 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: ruby: 581; makefile: 2
file content (214 lines) | stat: -rw-r--r-- 7,273 bytes parent folder | download
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
# frozen_string_literal: true

require 'minitest/autorun'
require 'colorize'

class TestColorize < Minitest::Test
  def test_blue_symbol
    assert_equal "\033[0;34;49mThis is blue\033[0m", 'This is blue'.colorize(:blue)
  end

  def test_incorrect_symbol
    assert_equal "\033[0;39;49mThis is incorrect color\033[0m",  'This is incorrect color'.colorize(:bold)
  end

  def test_incorrect_hash
    assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:color => :bold)

    assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:mode => :green)

    assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:background => :bold)
  end

  def test_blue_hash
    assert_equal "\033[0;34;49mThis is also blue?\033[0m", 'This is also blue?'.colorize(:color => :blue)
  end

  def test_light_blue_symbol
    assert_equal "\033[0;94;49mThis is light blue\033[0m", '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", '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", '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", '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", 'This is red on blue'.colorize(:red).on_blue
  end

  def test_red_with_blue_background_and_underline_symbol_and_methods
    assert_equal "\033[4;31;44mThis is red on blue and underline\033[0m", '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", 'This is blue text on red'.blue.on_red.blink
  end

  def test_uncolorize
    assert_equal 'This is uncolorized', 'This is uncolorized'.blue.on_red.uncolorize
  end

  def test_colorized?
    assert_predicate 'Red'.red, :colorized?
    refute_predicate 'Blue', :colorized?
    refute_predicate 'Green'.blue.green.uncolorize, :colorized?
  end

  def test_concatenated__colorize?
    assert_predicate "none #{'red'.red} none #{'blue'.blue} none", :colorized?
    refute_predicate "none #{'red'.red} none #{'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", "none #{'red'.red} none #{'blue'.blue} none".on_green
  end

  def test_concatenated_strings_uncolorize
    assert_equal 'none red none blue none', "none #{'red'.red} none #{'blue'.blue} none".uncolorize
  end

  def test_new_line
    assert_equal "\033[5;34;41mThis is blue\ntext on red\033[0m", "This is blue\ntext on red".blue.on_red.blink
  end

  def test_disable_colorization_with_=
    String.disable_colorization = true

    assert String.disable_colorization

    String.disable_colorization = false
  end

  def test_disable_colorization_with_method
    String.disable_colorization true

    assert String.disable_colorization

    String.disable_colorization false
  end

  def test_string_disable_colorization_with_=
    String.disable_colorization = true

    assert String.disable_colorization

    assert_equal 'This is blue after disabling', 'This is blue after disabling'.blue

    String.disable_colorization = false

    assert_equal "\033[0;34;49mThis is blue after enabling\033[0m", '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", 'This is blue before disabling'.colorize(:blue)

    String.disable_colorization true

    assert String.disable_colorization

    assert_equal 'This is blue after disabling', 'This is blue after disabling'.blue

    String.disable_colorization false

    assert_equal "\033[0;34;49mThis is blue after enabling\033[0m", 'This is blue after enabling'.colorize(:blue)
  end

  def test_already_colored_string_with_one_value
    assert_equal 'This is red'.red, "\033[31mThis is red\033[0m".red
  end

  def test_color_matrix_method
    assert_raises NoMethodError do
      String.color_matrix
    end
  end

  def test_color_samples_method
    assert_output do
      String.color_samples
    end
  end

  def test_grey_alias
    assert_equal 'This is grey'.grey, 'This is grey'.light_black
  end

  def test_gray_alias
    assert_equal 'This is gray'.gray, 'This is gray'.light_black
  end

  def test_add_color_alias
    String.add_color_alias(:extra_blue, :light_blue)

    assert_equal 'blue'.light_blue, 'blue'.extra_blue
    assert_equal 'blue'.on_light_blue, 'blue'.on_extra_blue
  end

  def test_add_color_alias_errors
    String.add_color_alias(:extra_red, :light_red)

    assert_raises ::Colorize::ColorAlreadyExist, 'Colorize: color :extra_red already exist!' do
      String.add_color_alias(:extra_red, :light_blue)
    end

    assert_raises ::Colorize::ColorDontExist, 'Colorize: color :light_color don\'t exist!' do
      String.add_color_alias(:extra_white, :light_color)
    end
  end

  def test_add_color_alias_with_single_hash
    String.add_color_alias(extra_green: :light_green)

    assert_equal 'example string'.light_green, 'example string'.extra_green
    assert_equal 'example string'.on_light_green, 'example string'.on_extra_green
  end

  def test_add_color_alias_with_single_hash_with_arrow
    String.add_color_alias(:extra_color => :gray)

    assert_equal 'example string'.gray, 'example string'.extra_color
    assert_equal 'example string'.on_gray, 'example string'.on_extra_color
  end

  def test_add_color_alias_with_multi_hash
    String.add_color_alias(extra_color_a: :gray, extra_color_b: :blue)

    assert_equal 'example string'.gray, 'example string'.extra_color_a
    assert_equal 'example string'.blue, 'example string'.extra_color_b
  end

  def test_add_color_alias_with_multi_hash_with_arrow
    String.add_color_alias(:extra_color_c => :gray, :extra_color_d => :blue)

    assert_equal 'example string'.gray, 'example string'.extra_color_c
    assert_equal 'example string'.on_blue, 'example string'.on_extra_color_d
  end

  def test_add_color_alias_with_multi_hash_mixed
    String.add_color_alias(extra_color_e: :gray, :extra_color_f => :blue)

    assert_equal 'example string'.gray, 'example string'.extra_color_e
    assert_equal 'example string'.on_blue, 'example string'.on_extra_color_f
  end

  def test_prevent_colors
    String.prevent_colors true

    assert String.prevent_colors
    assert_equal "#{'blue'.blue}#{'red'.red}#{'green'.green}", "#{'blue'.blue}red#{'green'.green}".red

    String.prevent_colors false
  end

  def test_colorized_string_without_readline
    assert_equal "\e[0;31;49mgreen\e[0m".green, 'green'.green
  end
end