require 'spec_helper'

class HelpersSpec < Less::Rails::Spec
  
  before { dummy_config.less.compress = false }
  
  let(:helpers) { dummy_asset('helpers') }
  
  it 'parse asset paths' do
    _digest = Rails.application.assets['rails.png'].digest
    line_for_helper('asset-path').must_equal  "asset-path: \"/assets/rails-#{_digest}.png\";"
    line_for_helper('asset-url').must_equal   "asset-url: url(/assets/rails-#{_digest}.png);"
    line_for_helper('image-path').must_equal  "image-path: \"/assets/rails-#{_digest}.png\";"
    line_for_helper('image-url').must_equal   "image-url: url(/assets/rails-#{_digest}.png);"
    line_for_helper('video-path').must_equal  'video-path: "/videos/rails.mp4";'
    line_for_helper('video-url').must_equal   "video-url: url(/videos/rails.mp4);"
    line_for_helper('audio-path').must_equal  'audio-path: "/audios/rails.mp3";'
    line_for_helper('audio-url').must_equal   "audio-url: url(/audios/rails.mp3);"
    if Rails::VERSION::MAJOR < 4
      line_for_helper('javascript-path').must_equal  'javascript-path: "/assets/rails.js";'
      line_for_helper('javascript-url').must_equal   "javascript-url: url(/assets/rails.js);"
      line_for_helper('stylesheet-path').must_equal  'stylesheet-path: "/assets/rails.css";'
      line_for_helper('stylesheet-url').must_equal   "stylesheet-url: url(/assets/rails.css);"
      line_for_helper('font-path').must_equal        'font-path: "/assets/rails.ttf";'
      line_for_helper('font-url').must_equal         "font-url: url(/assets/rails.ttf);"
    else
      line_for_helper('javascript-path').must_equal  'javascript-path: "/javascripts/rails.js";'
      line_for_helper('javascript-url').must_equal   "javascript-url: url(/javascripts/rails.js);"
      line_for_helper('stylesheet-path').must_equal  'stylesheet-path: "/stylesheets/rails.css";'
      line_for_helper('stylesheet-url').must_equal   "stylesheet-url: url(/stylesheets/rails.css);"
      line_for_helper('font-path').must_equal        'font-path: "/fonts/rails.ttf";'
      line_for_helper('font-url').must_equal         "font-url: url(/fonts/rails.ttf);"
    end
  end
  
  it 'parses data urls ' do
    line = line_for_helper('asset-data-url')
    asset_data_url_regexp = %r{asset-data-url: url\((.*?)\)}
    line.must_match asset_data_url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A5RTMzQTlDQTZFODJBQTFCIiBzdFJlZjpkb2N1bWVudElEPSJ4bXAuZGlkOkI3Njk0MTVBRDY2QzExRTA5RTMzQTlDQTZFODJBQTFCIi8%2BIDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY%2BIDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8%2B0HhJ9AAAABBJREFUeNpi%2BP%2F%2FPwNAgAEACPwC%2FtuiTRYAAAAASUVORK5CYII%3D"
    asset_data_url_match.must_equal asset_data_url_expected
  end


  private
  
  def line_for_helper(name)
    helpers.each_line.detect{ |line| line.include? name }.strip
  end

end
