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

# tc_string_extension.rb
#
#  Created by Richard LeBer 2011-06-27
#
#  This is Free Software.  See LICENSE and COPYING for details.

require "test_helper"

require "highline"
require "stringio"
require "string_methods"

# FakeString is here just to avoid
# using HighLine.colorize_strings
# on tests

class FakeString < String
  include HighLine::StringExtensions
end

class TestStringExtension < Minitest::Test
  def setup
    HighLine.reset
    @string = FakeString.new "string"
  end

  def teardown
    HighLine.reset
  end

  include StringMethods

  def test_Highline_String_is_yaml_serializable
    require "yaml"
    return if Gem::Version.new(YAML::VERSION) < Gem::Version.new("2.0.2")

    highline_string =
      HighLine::String.new("Yaml didn't messed with HighLine::String")
    yaml_highline_string = highline_string.to_yaml
    yaml_loaded_string = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0') # Ruby 2.6+
                           YAML.safe_load(yaml_highline_string, permitted_classes: [HighLine::String])
                         else
                           YAML.safe_load(yaml_highline_string, [HighLine::String])
                         end
    assert_equal "Yaml didn't messed with HighLine::String",
                 yaml_loaded_string
    assert_equal highline_string, yaml_loaded_string
    assert_instance_of HighLine::String, yaml_loaded_string
  end

  def test_highline_string_respond_to_color
    assert HighLine::String.new("highline string").respond_to? :color
  end

  def test_normal_string_doesnt_respond_to_color
    refute "normal_string".respond_to? :color
  end

  def test_highline_string_still_raises_for_non_available_messages
    assert_raises(NoMethodError) do
      @string.unknown_message
    end
  end

  def test_String_includes_StringExtension_when_receives_colorize_strings
    @include_received = 0
    caller = proc { @include_received += 1 }
    ::String.stub :include, caller do
      HighLine.colorize_strings
    end
    assert_equal 1, @include_received
  end

  def test_respond_to_dynamic_style_methods
    string = HighLine::String.new("pirarucu")
    assert_respond_to(string, :on_rgb_123456)
    assert_respond_to(string, :rgb_abcdef)
  end
end