require 'test_helper'
require 'archetype'

class SassExtensionsTest < MiniTest::Unit::TestCase
  setup do
    Compass.reset_configuration!
  end

  ## ENVIRONMENT
  # test that archetype-env() is working correctly
  def test_env
    reset
    assert_equal "development", evaluate("archetype-env()")
    Compass.configuration.environment = :production
    assert_equal "production", evaluate("archetype-env()")
    Compass.configuration.environment = :staging
    assert_equal "staging", evaluate("archetype-env()")
    reset
  end

  # list-add
  def test_list_add
    assert_equal "2 3 4", evaluate("list-add(1 2 3, 1)")
    assert_equal "5 6 7", evaluate("list-add(1 2 3, 4)")
    assert_equal "5 7 9", evaluate("list-add(1 2 3, 4 5 6)")
  end

  # list-subtract
  def test_list_subtract
    assert_equal "0 1 2", evaluate("list-subtract(1 2 3, 1)")
    assert_equal "1 2 3", evaluate("list-subtract(5 6 7, 4)")
    assert_equal "1 2 3", evaluate("list-subtract(5 7 9, 4 5 6)")
  end

  # list-multiply
  def test_list_multiply
    assert_equal "2 4 6", evaluate("list-multiply(1 2 3, 2)")
    assert_equal "20 24 28", evaluate("list-multiply(5 6 7, 4)")
    assert_equal "20 35 54", evaluate("list-multiply(5 7 9, 4 5 6)")
  end

  # list-divide
  def test_list_divide
    assert_equal "2/2 4/2 6/2", evaluate("list-divide(2 4 6, 2)")
    assert_equal "20/4 24/4 28/4", evaluate("list-divide(20 24 28, 4)")
    assert_equal "20/4 35/5 54/6", evaluate("list-divide(20 35 54, 4 5 6)")
  end

  # list-mod
  def test_list_mod
    assert_equal "1 0 1", evaluate("list-mod(1 2 3, 2)")
    assert_equal "0 1 2", evaluate("list-mod(4 5 6, 4)")
    assert_equal "3 2 1", evaluate("list-mod(15 17 19, 4 5 6)")
  end

  # index2
  def test_index2
    assert_equal "1", evaluate("index2(a b c, a)")
    assert_equal "4", evaluate("index2(a b c f, d e f)")
    assert_equal "false", evaluate("index2(a b c, d)")
    assert_equal "false", evaluate("index2(a b c, d e f)")
  end

  # nth-cyclic
  def test_nth_cyclic
    assert_equal "a", evaluate("nth-cyclic(a b c, 1)")
    assert_equal "b", evaluate("nth-cyclic(a b c, 2)")
    assert_equal "b", evaluate("nth-cyclic(a b c, 5)")
    assert_equal "c", evaluate("nth-cyclic(a b c, 24)")
  end

  ## LOCALE
  # locale
  def test_locale
    reset
    assert_equal "en_US", evaluate("locale()")
    Compass.configuration.locale = "ja_JP"
    assert_equal "ja_JP", evaluate("locale()")
    reset
  end

  # lang
  def test_lang
    reset
    assert_equal "true", evaluate("lang(en_US)")
    assert_equal "true", evaluate("lang(fr_FR en_US)")
    assert_equal "false", evaluate("lang(fr_FR)")
    Compass.configuration.locale = "ja_JP"
    assert_equal "false", evaluate("lang(en_US)")
    assert_equal "true", evaluate("lang(ja_JP)")
    assert_equal "true", evaluate("lang(CJK)")
    assert_equal "true", evaluate("lang(CJK en_US)")
    reset
  end


  ## NUMBERS
  def test_strip_units
    assert_equal "12", evaluate("strip-units(12px)")
    assert_equal "0.5", evaluate("strip-units(0.5em)")
    assert_equal "20", evaluate("strip-units('20rem')")
    assert_equal "0", evaluate("strip-units(10 somethings)") # this is a failure, so returns 0
  end

  ## UI
  # test generating unique tokens
  def test_unique
    assert_equal ".archetype-uid-RANDOM_UID", evaluate("unique(class)")
    assert_equal "\#archetype-uid-RANDOM_UID", evaluate("unique(id)")
    assert_equal "my-prefix-archetype-uid-RANDOM_UID", evaluate("unique(my-prefix-)")
    assert_equal ".testing-archetype-uid-RANDOM_UID", evaluate("unique('.testing-')")
  end

protected
  def evaluate(value)
    Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s
  end

  def reset
    Compass.reset_configuration!
  end
end
