aboutsummaryrefslogtreecommitdiffstats
path: root/test/reline/test_face.rb
blob: 14da4f6d656749feb78e492a33bcc2d9b767b6a6 (plain)
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
# frozen_string_literal: true

require_relative 'helper'

class Reline::Face::Test < Reline::TestCase
  RESET_SGR = "\e[0m"

  def teardown
    Reline::Face.reset_to_initial_configs
  end

  class WithInsufficientSetupTest < self
    def setup
      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
      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
      @config = Reline::Face.const_get(:Config).new(:my_config) { }
    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_sgr_rgb
      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
  end
end