File: attribute_test.rb

package info (click to toggle)
ruby-term-ansicolor 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,104 kB
  • sloc: ruby: 1,269; makefile: 10
file content (73 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download | duplicates (3)
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
require 'test_helper'

class AttributeTest < Test::Unit::TestCase
  include Term::ANSIColor

  def test_cast
    color = Attribute.get(:color123)
    on_color = Attribute.get(:on_color123)
    assert_equal color, Attribute[color]
    assert_equal color, Attribute[:color123]
    assert_equal color, Attribute[123]
    assert_equal color, Attribute['123']
    assert_equal color, Attribute['#87ffff']
    assert_equal color, Attribute[ [ 0x87, 0xff, 0xff ] ]
    assert_equal on_color, Attribute['on_123']
    assert_equal on_color, Attribute['on_#87ffff']
  end

  def test_gray
    a1 = Attribute[ [ 0, 0, 0 ] ]
    assert_equal false, a1.gray?
    a2 = Attribute[ [ 255, 255, 255 ] ]
    assert_equal false, a2.gray?
    a3 = Attribute[ [ 00, 0x7f, 0xff ] ]
    assert_equal false, a3.gray?
    a4 = Attribute[ [ 0x7f, 0x7f, 0x7f ] ]
    assert_equal true, a4.gray?
  end

  def test_distance_to
    color = Attribute.nearest_rgb_color('#0f0')
    assert_in_delta 250.954, Attribute.get(:color0).distance_to(color), 1e-3
    color = Attribute.nearest_rgb_color('#0f0')
    assert_in_delta 255, Attribute.get(:color0).distance_to(color,
      :metric => RGBColorMetrics::Euclidean), 1e-3
    assert_equal 1 / 0.0, Attribute.get(:color0).distance_to(nil)
  end

  def test_nearest_rgb_color
    assert_equal Attribute.get(:color0).rgb, Attribute.nearest_rgb_color('#000').rgb
    assert_equal Attribute.get(:color15).rgb, Attribute.nearest_rgb_color('#ffffff').rgb
    assert_equal :color247, Attribute.nearest_rgb_color('#aaa').name
    assert_equal :color109, Attribute.nearest_rgb_color('#aaa', :gray => false).name
  end

  def test_nearest_rgb_on_color
    assert_equal Attribute.get(:on_color0).rgb, Attribute.nearest_rgb_on_color('#000').rgb
    assert_equal Attribute.get(:on_color15).rgb, Attribute.nearest_rgb_on_color('#ffffff').rgb
    assert_equal :on_color247, Attribute.nearest_rgb_on_color('#aaa').name
    assert_equal :on_color109, Attribute.nearest_rgb_on_color('#aaa', :gray => false).name
  end

  def test_apply
    assert_equal "\e[5m", Attribute[:blink].apply
    assert_equal "\e[5mfoo\e[0m", Attribute[:blink].apply('foo')
    assert_equal "\e[5mfoo\e[0m", Attribute[:blink].apply { 'foo' }
  end

  def test_gradient
    g0 = Attribute[:blink].gradient_to Attribute['#30ffaa']
    assert_equal [], g0
    g1 = Attribute['#30ffaa'].gradient_to(Attribute['#ff507f'], :steps => 9)
    assert_equal [ :color49, :color49, :color43, :color79, :color108,
      :color247, :color138, :color168, :color204 ], g1.map(&:name)
    g2 = Attribute['#30ffaa'].gradient_to(
      Attribute['#ff507f'],
      :steps  => 9,
      :metric => RGBColorMetrics::Euclidean
    )
    assert_equal [ :color49, :color43, :color79, :color73, :color108,
      :color247, :color138, :color168, :color204 ], g2.map(&:name)
  end
end