summaryrefslogtreecommitdiff
path: root/test/test_color_scheme.rb
blob: 0716327777d1f37ac1f186f5955ab3117b4c4d57 (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
#!/usr/bin/env ruby
# coding: utf-8

# tc_color_scheme.rb
#
#  Created by Jeremy Hinegardner on 2007-01-24.
#  Copyright 2007 Jeremy Hinegardner. All rights reserved.
#
#  This is Free Software.  See LICENSE and COPYING for details.

require "test_helper"

require "highline"
require "stringio"

class TestColorScheme < Minitest::Test
  def setup
    HighLine.reset
    @input    = StringIO.new
    @output   = StringIO.new
    @terminal = HighLine.new(@input, @output)
  end

  def test_using_color_scheme
    refute(HighLine.using_color_scheme?)

    HighLine.color_scheme = HighLine::ColorScheme.new
    assert(true, HighLine.using_color_scheme?)
  end

  def test_scheme
    HighLine.color_scheme = HighLine::SampleColorScheme.new

    @terminal.say("This should be <%= color('warning yellow', :warning) %>.")
    assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",
                 @output.string)
    @output.rewind

    @terminal.say("This should be <%= color('warning yellow', 'warning') %>.")
    assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",
                 @output.string)
    @output.rewind

    @terminal.say("This should be <%= color('warning yellow', 'WarNing') %>.")
    assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",
                 @output.string)
    @output.rewind

    # Check that keys are available, and as expected
    assert_equal %w[critical error warning notice
                    info debug row_even row_odd].sort,
                 HighLine.color_scheme.keys.sort

    # Color scheme doesn't care if we use symbols or strings.
    # And it isn't case-sensitive
    warning1 = HighLine.color_scheme[:warning]
    warning2 = HighLine.color_scheme["warning"]
    warning3 = HighLine.color_scheme[:wArning]
    warning4 = HighLine.color_scheme["warniNg"]
    assert_instance_of HighLine::Style, warning1
    assert_instance_of HighLine::Style, warning2
    assert_instance_of HighLine::Style, warning3
    assert_instance_of HighLine::Style, warning4
    assert_equal warning1, warning2
    assert_equal warning1, warning3
    assert_equal warning1, warning4

    # Nonexistent keys return nil
    assert_nil HighLine.color_scheme[:nonexistent]

    # Same as above, for definitions
    defn1 = HighLine.color_scheme.definition(:warning)
    defn2 = HighLine.color_scheme.definition("warning")
    defn3 = HighLine.color_scheme.definition(:wArning)
    defn4 = HighLine.color_scheme.definition("warniNg")
    assert_instance_of Array, defn1
    assert_instance_of Array, defn2
    assert_instance_of Array, defn3
    assert_instance_of Array, defn4
    assert_equal [:bold, :yellow], defn1
    assert_equal [:bold, :yellow], defn2
    assert_equal [:bold, :yellow], defn3
    assert_equal [:bold, :yellow], defn4
    assert_nil HighLine.color_scheme.definition(:nonexistent)

    color_scheme_hash = HighLine.color_scheme.to_hash
    assert_instance_of Hash, color_scheme_hash
    assert_equal %w[critical error warning notice
                    info debug row_even row_odd].sort,
                 color_scheme_hash.keys.sort
    assert_instance_of Array, HighLine.color_scheme.definition(:warning)
    assert_equal [:bold, :yellow], HighLine.color_scheme.definition(:warning)

    # turn it back off, should raise an exception
    HighLine.color_scheme = nil
    assert_raises(NameError) do
      @terminal.say("This should be <%= color('nothing at all', :error) %>.")
    end
  end
end