File: test_sassc.rb

package info (click to toggle)
ruby-sprockets 4.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ruby: 13,014; javascript: 157; makefile: 4
file content (133 lines) | stat: -rw-r--r-- 2,957 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
127
128
129
130
131
132
133
# frozen_string_literal: true
require 'sprockets_test'
require 'shared_sass_tests'

silence_warnings do
  require 'sassc'
end

require 'sprockets/sassc_processor'
require 'sprockets/sassc_compressor'

class TestBaseSassc < Sprockets::TestCase
  CACHE_PATH = File.expand_path("../../.sass-cache", __FILE__)

  def sass
    ::SassC
  end

  def sass_functions
    ::SassC::Script::Functions
  end

  def sass_engine
    ::SassC::Engine
  end

  def syntax_error
    SassC::SyntaxError
  end

  def compressor
    Sprockets::SasscCompressor
  end

  def teardown
    refute ::SassC::Script::Functions.instance_methods.include?(:asset_path)
    FileUtils.rm_r(CACHE_PATH) if File.exist?(CACHE_PATH)
    assert !File.exist?(CACHE_PATH)
  end
end

class TestNoSassFunctionSassC < TestBaseSassc
  module ::SassC::Script::Functions
    def javascript_path(path)
      ::SassC::Script::Value::String.new("/js/#{path.value}", :string)
    end

    module Compass
      def stylesheet_path(path)
        ::SassC::Script::Value::String.new("/css/#{path.value}", :string)
      end
    end
    include Compass
  end

  include SharedSassTestNoFunction
end

class TestSprocketsSassc < TestBaseSassc
  def setup
    super

    @env = Sprockets::Environment.new(".") do |env|
      env.cache = {}
      env.append_path(fixture_path('.'))
      env.append_path(fixture_path('compass'))
      env.append_path(fixture_path('octicons'))
      env.register_transformer 'text/sass', 'text/css', Sprockets::SasscProcessor.new
      env.register_transformer 'text/scss', 'text/css', Sprockets::ScsscProcessor.new
    end
  end

  def teardown
    assert !File.exist?(CACHE_PATH)
  end

  def render(path)
    path = fixture_path(path)
    silence_warnings do
      @env.find_asset(path, accept: 'text/css').to_s
    end
  end

  test "raise sass error with line number" do
    begin
      render('sass/error.sass')
      flunk
    rescue SassC::SyntaxError => error
      # this is not exactly consistent with ruby sass
      assert error.message.include?("invalid")
      assert error.message.include?("error.sass")
      assert error.message.include?("line 5")
    end
  end

  test "track sass dependencies metadata" do
    skip "not consistent with ruby sass"

    asset = nil
    silence_warnings do
      asset = @env.find_asset('sass/import_partial.css')
    end
    assert asset
    assert_equal [
      fixture_path('sass/_rounded.scss'),
      fixture_path('sass/import_partial.sass')
    ], asset.metadata[:sass_dependencies].to_a.sort
  end

  include SharedSassTestSprockets
end

class TestSasscCompressor < TestBaseSassc
  include SharedSassTestCompressor
end

class TestSasscFunctions < TestSprocketsSassc
  def setup
    super
    define_asset_path
  end

  def define_asset_path
    @env.context_class.class_eval do
      def asset_path(path, options = {})
        link_asset(path)
        "/#{path}"
      end
    end
  end

  include SharedSassTestFunctions
end