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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
# frozen_string_literal: true
require_relative 'helper'
class Reline::Face::Test < Reline::TestCase
RESET_SGR = "\e[0m"
def setup
@colorterm_backup = ENV['COLORTERM']
ENV['COLORTERM'] = 'truecolor'
end
def teardown
Reline::Face.reset_to_initial_configs
ENV['COLORTERM'] = @colorterm_backup
end
class WithInsufficientSetupTest < self
def setup
super
Reline::Face.config(:my_insufficient_config) do |face|
end
@face = Reline::Face[:my_insufficient_config]
end
def test_my_insufficient_config_line
assert_equal RESET_SGR, @face[:default]
assert_equal RESET_SGR, @face[:enhanced]
assert_equal RESET_SGR, @face[:scrollbar]
end
def test_my_insufficient_configs
my_configs = Reline::Face.configs[:my_insufficient_config]
assert_equal(
{
default: { style: :reset, escape_sequence: RESET_SGR },
enhanced: { style: :reset, escape_sequence: RESET_SGR },
scrollbar: { style: :reset, escape_sequence: RESET_SGR }
},
my_configs
)
end
end
class WithSetupTest < self
def setup
super
Reline::Face.config(:my_config) do |face|
face.define :default, foreground: :blue
face.define :enhanced, foreground: "#FF1020", background: :black, style: [:bold, :underlined]
end
Reline::Face.config(:another_config) do |face|
face.define :another_label, foreground: :red
end
@face = Reline::Face[:my_config]
end
def test_now_there_are_four_configs
assert_equal %i(default completion_dialog my_config another_config), Reline::Face.configs.keys
end
def test_resetting_config_discards_user_defined_configs
Reline::Face.reset_to_initial_configs
assert_equal %i(default completion_dialog), Reline::Face.configs.keys
end
def test_my_configs
my_configs = Reline::Face.configs[:my_config]
assert_equal(
{
default: {
escape_sequence: "#{RESET_SGR}\e[34m", foreground: :blue
},
enhanced: {
background: :black,
foreground: "#FF1020",
style: [:bold, :underlined],
escape_sequence: "\e[0m\e[38;2;255;16;32;40;1;4m"
},
scrollbar: {
style: :reset,
escape_sequence: "\e[0m"
}
},
my_configs
)
end
def test_my_config_line
assert_equal "#{RESET_SGR}\e[34m", @face[:default]
end
def test_my_config_enhanced
assert_equal "#{RESET_SGR}\e[38;2;255;16;32;40;1;4m", @face[:enhanced]
end
def test_not_respond_to_another_label
assert_equal false, @face.respond_to?(:another_label)
end
end
class WithoutSetupTest < self
def test_my_config_default
Reline::Face.config(:my_config) do |face|
# do nothing
end
face = Reline::Face[:my_config]
assert_equal RESET_SGR, face[:default]
end
def test_style_does_not_exist
face = Reline::Face[:default]
assert_raise ArgumentError do
face[:style_does_not_exist]
end
end
def test_invalid_keyword
assert_raise ArgumentError do
Reline::Face.config(:invalid_config) do |face|
face.define :default, invalid_keyword: :red
end
end
end
def test_invalid_foreground_name
assert_raise ArgumentError do
Reline::Face.config(:invalid_config) do |face|
face.define :default, foreground: :invalid_name
end
end
end
def test_invalid_background_name
assert_raise ArgumentError do
Reline::Face.config(:invalid_config) do |face|
face.define :default, background: :invalid_name
end
end
end
def test_invalid_style_name
assert_raise ArgumentError do
Reline::Face.config(:invalid_config) do |face|
face.define :default, style: :invalid_name
end
end
end
def test_private_constants
[:SGR_PARAMETER, :Config, :CONFIGS].each do |name|
assert_equal false, Reline::Face.constants.include?(name)
end
end
end
class ConfigTest < self
def setup
super
@config = Reline::Face.const_get(:Config).new(:my_config) { }
end
def teardown
super
Reline::Face.instance_variable_set(:@force_truecolor, nil)
end
def test_rgb?
assert_equal true, @config.send(:rgb_expression?, "#FFFFFF")
end
def test_invalid_rgb?
assert_equal false, @config.send(:rgb_expression?, "FFFFFF")
assert_equal false, @config.send(:rgb_expression?, "#FFFFF")
end
def test_format_to_sgr_preserves_order
assert_equal(
"#{RESET_SGR}\e[37;41;1;3m",
@config.send(:format_to_sgr, foreground: :white, background: :red, style: [:bold, :italicized])
)
assert_equal(
"#{RESET_SGR}\e[37;1;3;41m",
@config.send(:format_to_sgr, foreground: :white, style: [:bold, :italicized], background: :red)
)
end
def test_format_to_sgr_with_reset
assert_equal(
RESET_SGR,
@config.send(:format_to_sgr, style: :reset)
)
assert_equal(
"#{RESET_SGR}\e[37;0;41m",
@config.send(:format_to_sgr, foreground: :white, style: :reset, background: :red)
)
end
def test_format_to_sgr_with_single_style
assert_equal(
"#{RESET_SGR}\e[37;41;1m",
@config.send(:format_to_sgr, foreground: :white, background: :red, style: :bold)
)
end
def test_truecolor
ENV['COLORTERM'] = 'truecolor'
assert_equal true, Reline::Face.truecolor?
ENV['COLORTERM'] = '24bit'
assert_equal true, Reline::Face.truecolor?
ENV['COLORTERM'] = nil
assert_equal false, Reline::Face.truecolor?
Reline::Face.force_truecolor
assert_equal true, Reline::Face.truecolor?
end
def test_sgr_rgb_truecolor
ENV['COLORTERM'] = 'truecolor'
assert_equal "38;2;255;255;255", @config.send(:sgr_rgb, :foreground, "#ffffff")
assert_equal "48;2;18;52;86", @config.send(:sgr_rgb, :background, "#123456")
end
def test_sgr_rgb_256color
ENV['COLORTERM'] = nil
assert_equal '38;5;231', @config.send(:sgr_rgb, :foreground, '#ffffff')
assert_equal '48;5;16', @config.send(:sgr_rgb, :background, '#000000')
# Color steps are [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
assert_equal '38;5;24', @config.send(:sgr_rgb, :foreground, '#005f87')
assert_equal '38;5;67', @config.send(:sgr_rgb, :foreground, '#5f87af')
assert_equal '48;5;110', @config.send(:sgr_rgb, :background, '#87afd7')
assert_equal '48;5;153', @config.send(:sgr_rgb, :background, '#afd7ff')
# Boundary values are [0x30, 0x73, 0x9b, 0xc3, 0xeb]
assert_equal '38;5;24', @config.send(:sgr_rgb, :foreground, '#2f729a')
assert_equal '38;5;67', @config.send(:sgr_rgb, :foreground, '#30739b')
assert_equal '48;5;110', @config.send(:sgr_rgb, :background, '#9ac2ea')
assert_equal '48;5;153', @config.send(:sgr_rgb, :background, '#9bc3eb')
end
def test_force_truecolor_reconfigure
ENV['COLORTERM'] = nil
Reline::Face.config(:my_config) do |face|
face.define :default, foreground: '#005f87'
face.define :enhanced, background: '#afd7ff'
end
assert_equal "\e[0m\e[38;5;24m", Reline::Face[:my_config][:default]
assert_equal "\e[0m\e[48;5;153m", Reline::Face[:my_config][:enhanced]
Reline::Face.force_truecolor
assert_equal "\e[0m\e[38;2;0;95;135m", Reline::Face[:my_config][:default]
assert_equal "\e[0m\e[48;2;175;215;255m", Reline::Face[:my_config][:enhanced]
end
end
end
|