File: test_helper.rb

package info (click to toggle)
ruby-sass 3.7.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,396 kB
  • sloc: ruby: 32,443; sh: 26; makefile: 25
file content (149 lines) | stat: -rw-r--r-- 3,898 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
lib_dir = File.dirname(__FILE__) + '/../lib'

require 'minitest/autorun'
require 'fileutils'
$:.unshift lib_dir unless $:.include?(lib_dir)
require 'sass'
require 'mathn' if ENV['MATHN'] == 'true'

Sass::RAILS_LOADED = true unless defined?(Sass::RAILS_LOADED)

Sass.tests_running = true

if defined?(Encoding)
  $-w, w = false, $-w
  Encoding.default_external = 'UTF-8'
  $-w = w
end

module Sass::Script::Functions
  def option(name)
    Sass::Script::Value::String.new(@options[name.value.to_sym].to_s)
  end
end

class Minitest::Test
  def munge_filename(opts = {})
    opts[:filename] ||= filename_for_test(opts[:syntax] || :sass)
    opts[:sourcemap_filename] ||= sourcemap_filename_for_test
    opts
  end

  def test_name
    caller.
      map {|c| Sass::Util.caller_info(c)[2]}.
      compact.
      map {|c| c.sub(/^(block|rescue) in /, '')}.
      find {|c| c =~ /^test_/}
  end

  def filename_for_test(syntax = :sass)
    "#{test_name}_inline.#{syntax}"
  end

  def sourcemap_filename_for_test(syntax = :sass)
    "#{test_name}_inline.css.map"
  end

  def clean_up_sassc
    path = File.dirname(__FILE__) + "/../.sass-cache"
    Sass::Util.retry_on_windows {FileUtils.rm_r(path) if File.exist?(path)}
  end

  def assert_warning(message)
    the_real_stderr, $stderr = $stderr, StringIO.new
    result = yield

    if message.is_a?(Regexp)
      assert_match message, $stderr.string.strip
    else
      assert_equal message.strip, $stderr.string.strip
    end
    result
  ensure
    $stderr = the_real_stderr
  end

  def assert_no_warning
    the_real_stderr, $stderr = $stderr, StringIO.new
    result = yield
    assert_equal '', $stderr.string
    result
  ensure
    $stderr = the_real_stderr
  end

  def silence_warnings(&block)
    Sass::Util.silence_sass_warnings(&block)
  end

  def assert_raise_message(klass, message)
    yield
  rescue Exception => e
    assert_instance_of(klass, e)
    assert_equal(message, e.message)
  else
    flunk "Expected exception #{klass}, none raised"
  end

  def assert_raise_line(line)
    yield
  rescue Sass::SyntaxError => e
    assert_equal(line, e.sass_line)
  else
    flunk "Expected exception on line #{line}, none raised"
  end

  def assert_sass_to_sass(sass, options: {})
    assert_equal(sass.rstrip, to_sass(sass, options).rstrip,
      "Expected Sass to transform to itself")
  end

  def assert_scss_to_sass(sass, scss, options: {})
    assert_equal(sass.rstrip, to_sass(scss, options.merge(:syntax => :scss)).rstrip,
      "Expected SCSS to transform to Sass")
  end

  def assert_scss_to_scss(scss, source: scss, options: {})
    assert_equal(scss.rstrip, to_scss(source, options.merge(:syntax => :scss)).rstrip,
      "Expected SCSS to transform to #{scss == source ? 'itself' : 'SCSS'}")
  end

  def assert_sass_to_scss(scss, sass, options: {})
    assert_equal(scss.rstrip, to_scss(sass, options).rstrip,
      "Expected Sass to transform to SCSS")
  end

  def assert_converts(sass, scss, options: {})
    assert_sass_to_sass(sass, options: options)
    assert_scss_to_sass(sass, scss, options: options)
    assert_scss_to_scss(scss, options: options)
    assert_sass_to_scss(scss, sass, options: options)
  end

  def to_sass(scss, options = {})
    Sass::Util.silence_sass_warnings do
      Sass::Engine.new(scss, options).to_tree.to_sass(options)
    end
  end

  def to_scss(sass, options = {})
    Sass::Util.silence_sass_warnings do
      Sass::Engine.new(sass, options).to_tree.to_scss(options)
    end
  end
end

module PublicApiLinter
  def lint_api(api_class, duck_type_class)
    define_method :test_lint_instance do
      assert lint_instance.is_a?(duck_type_class)
    end
    api_class.instance_methods.each do |meth|
      define_method :"test_has_#{meth}" do
        assert lint_instance.respond_to?(meth),
          "#{duck_type_class.name} does not implement #{meth}"
      end
    end
  end
end