summaryrefslogtreecommitdiff
path: root/spec/helpers/colors_helper_spec.rb
blob: ca5cafb7ebe088e8a831956c801d99e511434255 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ColorsHelper do
  using RSpec::Parameterized::TableSyntax

  describe '#hex_color_to_rgb_array' do
    context 'valid hex color' do
      where(:hex_color, :rgb_array) do
        '#000000' | [0, 0, 0]
        '#aaaaaa' | [170, 170, 170]
        '#cCcCcC' | [204, 204, 204]
        '#FFFFFF' | [255, 255, 255]
        '#000abc' | [0, 10, 188]
        '#123456' | [18, 52, 86]
        '#a1b2c3' | [161, 178, 195]
        '#000'    | [0, 0, 0]
        '#abc'    | [170, 187, 204]
        '#321'    | [51, 34, 17]
        '#7E2'    | [119, 238, 34]
        '#fFf'    | [255, 255, 255]
      end

      with_them do
        it 'returns correct RGB array' do
          expect(helper.hex_color_to_rgb_array(hex_color)).to eq(rgb_array)
        end
      end
    end

    context 'invalid hex color' do
      where(:hex_color) { ['', '0', '#00', '#ffff', '#1234567', 'invalid', [], 1, nil] }

      with_them do
        it 'raise ArgumentError' do
          expect { helper.hex_color_to_rgb_array(hex_color) }.to raise_error(ArgumentError)
        end
      end
    end
  end

  describe '#rgb_array_to_hex_color' do
    context 'valid RGB array' do
      where(:rgb_array, :hex_color) do
        [0, 0, 0]       | '#000000'
        [0, 0, 255]     | '#0000ff'
        [0, 255, 0]     | '#00ff00'
        [255, 0, 0]     | '#ff0000'
        [12, 34, 56]    | '#0c2238'
        [222, 111, 88]  | '#de6f58'
        [255, 255, 255] | '#ffffff'
      end

      with_them do
        it 'returns correct hex color' do
          expect(helper.rgb_array_to_hex_color(rgb_array)).to eq(hex_color)
        end
      end
    end

    context 'invalid RGB array' do
      where(:rgb_array) do
        [
          '',
          '#000000',
          0,
          nil,
          [],
          [0],
          [0, 0],
          [0, 0, 0, 0],
          [-1, 0, 0],
          [0, -1, 0],
          [0, 0, -1],
          [256, 0, 0],
          [0, 256, 0],
          [0, 0, 256]
        ]
      end

      with_them do
        it 'raise ArgumentError' do
          expect { helper.rgb_array_to_hex_color(rgb_array) }.to raise_error(ArgumentError)
        end
      end
    end
  end
end