require 'helper'

class TestI0n < Test::Unit::TestCase

  should "be able to find version.yml" do
    assert(File.exists?("#{I0n::CONFIG_PATH}/version.yml"))
  end

  context "Test for I0n.version class instance method" do
    should "return 1.1.1" do
      I0n.version_major = 1
      I0n.version_minor = 1
      I0n.version_patch = 1
      assert_equal "1.1.1", I0n.version
    end
  end

  context "Tests for the I0n#dump_version_yaml class method." do
    should "call File.open and Yaml.dump for version.yml" do
      YAML.stubs(:dump)
      File.stubs(:open)
      File.expects(:open).once
      I0n.dump_version_yaml
    end
  end

  context "Tests for the I0n#bump class method." do

    setup do
      I0n.stubs(:dump_version_yaml)
    end

    should "add 1 to @version_major class instance variable, set @version_minor and @version_patch to 0 and call dump_I0n_version_yaml" do
      I0n.module_eval {@version_major = 1; @version_minor = 1; @version_patch = 1 }
      I0n.expects(:dump_version_yaml).once
      I0n.bump("version_major")
      assert_equal 2, I0n.version_major
      assert_equal 0, I0n.version_minor
      assert_equal 0, I0n.version_patch
    end

    should "add 1 to @version_minor class instance variable, set @version_patch to 0 and call dump_I0n_version_yaml" do
      I0n.module_eval {@version_major = 1; @version_minor = 1; @version_patch = 1 }
      I0n.expects(:dump_version_yaml).once
      I0n.bump("version_minor")
      assert_equal 1, I0n.version_major
      assert_equal 2, I0n.version_minor
      assert_equal 0, I0n.version_patch

    end

    should "add 1 to @version_patch class instance variable and call dump_I0n_version_yaml" do
      I0n.module_eval {@version_major = 1; @version_minor = 1; @version_patch = 1 }
      I0n.expects(:dump_version_yaml).once
      I0n.bump("version_patch")
      assert_equal 1, I0n.version_major
      assert_equal 1, I0n.version_minor
      assert_equal 2, I0n.version_patch

    end

  end

  context "Tests for the I0n#const_missing class method." do

    setup do
      I0n.stubs(:version)
    end

    should "return the value of I0n Version if I0n::VERSION is called" do
      I0n.expects(:version).once
      I0n::VERSION
    end

    should "return to super if an unknown constant is called" do
      assert_raises(NameError) {I0n::VERSIONS}
      assert_raises(NameError) {I0n::AVERSION}
    end

  end

  context "Tests for the I0n#method_missing class method." do

    setup do
      I0n.stubs(:bump).returns(:result)
      I0n.stubs(:dump_version_yaml)
    end

    should "recognise I0n#bump_version_major class instance method calls and forward them to I0n#bump to set @version_major." do
      I0n.expects(:bump).with("version_major").once
      I0n.bump_version_major
    end

    should "recognise I0n#bump_version_minor class instance method calls and forward them to I0n#bump to set @version_minor." do
      I0n.expects(:bump).with("version_minor").once
      I0n.bump_version_minor
    end

    should "recognise I0n#bump_version_patch class instance method calls and forward them to I0n#bump to set @version_patch." do
      I0n.expects(:bump).with("version_patch").once
      I0n.bump_version_patch
    end

    should "return method_missing to super as normal if method name is not recognised." do
      assert_raises(NoMethodError) {I0n.bump_version_blarg}
    end

  end

end