File: test-color-scheme.rb

package info (click to toggle)
ruby-test-unit 3.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,280 kB
  • sloc: ruby: 15,493; makefile: 9
file content (126 lines) | stat: -rw-r--r-- 3,845 bytes parent folder | download | duplicates (2)
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
class TestUnitColorScheme < Test::Unit::TestCase
  def test_register
    inverted_scheme_spec = {
      "success" => {:name => "red"},
      "failure" => {:name => "green"},
    }
    Test::Unit::ColorScheme["inverted"] = inverted_scheme_spec
    assert_equal({
                   "success" => color("red"),
                   "failure" => color("green"),
                 },
                 Test::Unit::ColorScheme["inverted"].to_hash)
  end

  def test_new_with_colors
    scheme = Test::Unit::ColorScheme.new(:success => color("blue"),
                                         "failure" => color("green",
                                                            :underline => true))
    assert_equal({
                   "success" => color("blue"),
                   "failure" => color("green", :underline => true),
                 },
                 scheme.to_hash)
  end

  def test_new_with_spec
    scheme = Test::Unit::ColorScheme.new(:success => {
                                           :name => "blue",
                                           :bold => true
                                         },
                                         "failure" => {:name => "green"})
    assert_equal({
                   "success" => color("blue", :bold => true),
                   "failure" => color("green"),
                 },
                 scheme.to_hash)
  end

  private
  def color(name, options={})
    Test::Unit::Color.new(name, options)
  end

  module CleanEnvironment
    def setup
      @original_term, ENV["TERM"] = ENV["TERM"], nil
      @original_color_term, ENV["COLORTERM"] = ENV["COLORTERM"], nil
      @original_vte_version, ENV["VTE_VERSION"] = ENV["VTE_VERSION"], nil
      @original_github_actions, ENV["GITHUB_ACTIONS"] = ENV["GITHUB_ACTIONS"], nil
      ENV["TERM"] = "xterm"
    end

    def teardown
      ENV["TERM"] = @original_term
      ENV["COLORTERM"] = @original_color_term
      ENV["VTE_VERSION"] = @original_vte_version
      ENV["GITHUB_ACTIONS"] = @original_github_actions
    end
  end

  class TestFor8Colors < self
    include CleanEnvironment

    def test_default
      expected_schema_keys = [
        "pass",
        "pass-marker",
        "failure",
        "failure-marker",
        "pending",
        "pending-marker",
        "omission",
        "omission-marker",
        "notification",
        "notification-marker",
        "error",
        "error-marker",
        "case",
        "suite",
        "diff-inserted-tag",
        "diff-deleted-tag",
        "diff-difference-tag",
        "diff-inserted",
        "diff-deleted",
      ]
      assert_equal(expected_schema_keys.sort,
                   Test::Unit::ColorScheme.default.to_hash.keys.sort)
    end
  end

  class TestGuessAvailableColors < self
    include CleanEnvironment

    {
      "rxvt"                 => 8,
      "xterm-color"          => 8,
      "alacritty"            => 256,
      "iTerm.app"            => 256,
      "screen-256color"      => 256,
      "screenxterm-256color" => 256,
      "tmux-256color"        => 256,
      "vte-256color"         => 256,
      "vscode-direct"        => 2**24,
      "vte-direct"           => 2**24,
      "xterm-direct"         => 2**24,
    }.each do |term, colors|
      data("%20s => %8d" % [term, colors], {term: term, colors: colors})
    end
    def test_term_env(data)
      ENV["TERM"] = data[:term]
      assert_equal(data[:colors],
                   Test::Unit::ColorScheme.available_colors,
                   "Incorrect available_colors for TERM=%s" % [data[:term]])
    end
  end

  class TestDefaultScheme < self
    include CleanEnvironment

    def test_direct_color
      ENV["TERM"] = "xterm-direct"
      assert_equal(Test::Unit::ColorScheme.default_for_256_colors,
                   Test::Unit::ColorScheme.default)
    end
  end
end