|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +def color_decorator_test_case_name(test_case) |
| 4 | + "when translated style is #{test_case[:input][0].inspect} "\ |
| 5 | + "and content is #{test_case[:input][1].inspect}" |
| 6 | +end |
| 7 | + |
| 8 | +RSpec.describe RubyJard::Decorators::ColorDecorator do |
| 9 | + subject(:color_decorator) { described_class.new(color_scheme) } |
| 10 | + |
| 11 | + let(:color_scheme) do |
| 12 | + Class.new(RubyJard::ColorScheme) do |
| 13 | + const_set( |
| 14 | + :STYLES, |
| 15 | + { |
| 16 | + color0: [], |
| 17 | + color1: ['234'], |
| 18 | + color2: ['#aaa'], # 170, 170, 170 |
| 19 | + color3: ['#80b57b'], # 128, 181, 123 |
| 20 | + color4: %w[234 245], |
| 21 | + color5: ['#bbb', '#aaa'], # 187, 187, 187 and 170, 170, 170 |
| 22 | + color6: ['#80b57b', '#78b5ff'], # 128, 181, 123 and 120, 181, 255 |
| 23 | + color_not_supported_1: ['rgb(0, 255, 255)'], |
| 24 | + color_not_supported_2: ['rgb(0, 255, 255)', 'rgb(0, 255, 255)'], |
| 25 | + color_invalid: 'invalid' |
| 26 | + }.freeze |
| 27 | + ) |
| 28 | + end.new |
| 29 | + end |
| 30 | + |
| 31 | + [ |
| 32 | + # Input style is a symbol |
| 33 | + { input: [:color0, nil], output: "\e[0m" }, |
| 34 | + { input: [:color0, 'hello'], output: "hello\e[0m" }, |
| 35 | + { input: [:color1, nil], output: "\e[38;5;234m\e[0m" }, |
| 36 | + { input: [:color1, 'hello'], output: "\e[38;5;234mhello\e[0m" }, |
| 37 | + { input: [:color2, nil], output: "\e[38;2;170;170;170m\e[0m" }, |
| 38 | + { input: [:color2, 'hello'], output: "\e[38;2;170;170;170mhello\e[0m" }, |
| 39 | + { input: [:color3, nil], output: "\e[38;2;128;181;123m\e[0m" }, |
| 40 | + { input: [:color3, 'hello'], output: "\e[38;2;128;181;123mhello\e[0m" }, |
| 41 | + { input: [:color4, nil], output: "\e[38;5;234m\e[48;5;245m\e[0m" }, |
| 42 | + { input: [:color4, 'hello'], output: "\e[38;5;234m\e[48;5;245mhello\e[0m" }, |
| 43 | + { input: [:color5, nil], output: "\e[38;2;187;187;187m\e[48;2;170;170;170m\e[0m" }, |
| 44 | + { input: [:color5, 'hello'], output: "\e[38;2;187;187;187m\e[48;2;170;170;170mhello\e[0m" }, |
| 45 | + { input: [:color6, nil], output: "\e[38;2;128;181;123m\e[48;2;120;181;255m\e[0m" }, |
| 46 | + { input: [:color6, 'hello'], output: "\e[38;2;128;181;123m\e[48;2;120;181;255mhello\e[0m" }, |
| 47 | + { input: [:color_not_supported_1, nil], output: "\e[0m" }, |
| 48 | + { input: [:color_not_supported_1, 'hello'], output: "hello\e[0m" }, |
| 49 | + { input: [:color_not_supported_2, nil], output: "\e[0m" }, |
| 50 | + { input: [:color_not_supported_2, 'hello'], output: "hello\e[0m" }, |
| 51 | + { input: [:color_invalid, nil], error: /must be an array/ }, |
| 52 | + { input: [:color_not_found, nil], output: "\e[0m" }, |
| 53 | + # Input style is an array with single symbol |
| 54 | + { input: [[:color0], nil], output: "\e[0m" }, |
| 55 | + { input: [[:color0], 'hello'], output: "hello\e[0m" }, |
| 56 | + { input: [[:color1], nil], output: "\e[38;5;234m\e[0m" }, |
| 57 | + { input: [[:color1], 'hello'], output: "\e[38;5;234mhello\e[0m" }, |
| 58 | + { input: [[:color2], nil], output: "\e[38;2;170;170;170m\e[0m" }, |
| 59 | + { input: [[:color2], 'hello'], output: "\e[38;2;170;170;170mhello\e[0m" }, |
| 60 | + { input: [[:color3], nil], output: "\e[38;2;128;181;123m\e[0m" }, |
| 61 | + { input: [[:color3], 'hello'], output: "\e[38;2;128;181;123mhello\e[0m" }, |
| 62 | + { input: [[:color4], nil], output: "\e[38;5;234m\e[48;5;245m\e[0m" }, |
| 63 | + { input: [[:color4], 'hello'], output: "\e[38;5;234m\e[48;5;245mhello\e[0m" }, |
| 64 | + { input: [[:color5], nil], output: "\e[38;2;187;187;187m\e[48;2;170;170;170m\e[0m" }, |
| 65 | + { input: [[:color5], 'hello'], output: "\e[38;2;187;187;187m\e[48;2;170;170;170mhello\e[0m" }, |
| 66 | + { input: [[:color6], nil], output: "\e[38;2;128;181;123m\e[48;2;120;181;255m\e[0m" }, |
| 67 | + { input: [[:color6], 'hello'], output: "\e[38;2;128;181;123m\e[48;2;120;181;255mhello\e[0m" }, |
| 68 | + { input: [[:color_not_supported_1], nil], output: "\e[0m" }, |
| 69 | + { input: [[:color_not_supported_1], 'hello'], output: "hello\e[0m" }, |
| 70 | + { input: [[:color_not_supported_2], nil], output: "\e[0m" }, |
| 71 | + { input: [[:color_not_supported_2], 'hello'], output: "hello\e[0m" }, |
| 72 | + { input: [[:color_invalid], nil], error: /must be an array/ }, |
| 73 | + { input: [[:color_not_found], nil], output: "\e[0m" }, |
| 74 | + # Input style includes text attributes |
| 75 | + { input: [[:color0, :underline], nil], output: "\e[4m\e[0m" }, |
| 76 | + { input: [[:color0, :underline], 'hello'], output: "\e[4mhello\e[0m" }, |
| 77 | + { input: [[:color1, :underline], nil], output: "\e[38;5;234m\e[4m\e[0m" }, |
| 78 | + { input: [[:color1, :underline], 'hello'], output: "\e[38;5;234m\e[4mhello\e[0m" }, |
| 79 | + { input: [[:color2, :underline], nil], output: "\e[38;2;170;170;170m\e[4m\e[0m" }, |
| 80 | + { input: [[:color2, :underline], 'hello'], output: "\e[38;2;170;170;170m\e[4mhello\e[0m" }, |
| 81 | + { input: [[:color3, :underline], nil], output: "\e[38;2;128;181;123m\e[4m\e[0m" }, |
| 82 | + { input: [[:color3, :underline], 'hello'], output: "\e[38;2;128;181;123m\e[4mhello\e[0m" }, |
| 83 | + { input: [[:color4, :underline], nil], output: "\e[38;5;234m\e[48;5;245m\e[4m\e[0m" }, |
| 84 | + { input: [[:color4, :underline], 'hello'], output: "\e[38;5;234m\e[48;5;245m\e[4mhello\e[0m" }, |
| 85 | + { input: [[:color5, :underline], nil], output: "\e[38;2;187;187;187m\e[48;2;170;170;170m\e[4m\e[0m" }, |
| 86 | + { input: [[:color5, :underline], 'hello'], output: "\e[38;2;187;187;187m\e[48;2;170;170;170m\e[4mhello\e[0m" }, |
| 87 | + { input: [[:color6, :underline], nil], output: "\e[38;2;128;181;123m\e[48;2;120;181;255m\e[4m\e[0m" }, |
| 88 | + { input: [[:color6, :underline], 'hello'], output: "\e[38;2;128;181;123m\e[48;2;120;181;255m\e[4mhello\e[0m" }, |
| 89 | + { input: [[:color_not_supported_1, :underline], nil], output: "\e[4m\e[0m" }, |
| 90 | + { input: [[:color_not_supported_1, :underline], 'hello'], output: "\e[4mhello\e[0m" }, |
| 91 | + { input: [[:color_not_supported_2, :underline], nil], output: "\e[4m\e[0m" }, |
| 92 | + { input: [[:color_not_supported_2, :underline], 'hello'], output: "\e[4mhello\e[0m" }, |
| 93 | + { input: [[:color_invalid, :underline], nil], error: /must be an array/ }, |
| 94 | + { input: [[:color_not_found, :underline], nil], output: "\e[4m\e[0m" }, |
| 95 | + # Completed example |
| 96 | + { |
| 97 | + input: [[:color4, :underline, :bold, :italic], '#<Thread:0x000055fcf5f64c38@Super thread test1.rb:78 sleep>'], |
| 98 | + output: "\e[38;5;234m\e[48;5;245m\e[4m\e[1m\e[3m#<Thread:0x000055fcf5f64c38@Super thread test1.rb:78 sleep>\e[0m" |
| 99 | + } |
| 100 | + ].each do |test_case| |
| 101 | + context color_decorator_test_case_name(test_case) do |
| 102 | + it do |
| 103 | + if test_case[:error] |
| 104 | + expect do |
| 105 | + color_decorator.decorate(*test_case[:input]) |
| 106 | + end.to raise_error(RubyJard::Error, test_case[:error]) |
| 107 | + else |
| 108 | + expect(color_decorator.decorate(*test_case[:input])).to eql(test_case[:output]) |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | +end |
0 commit comments