# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

require 'spec_helper'

describe TwitterCldr::Formatters::Rbnf::RbnfFormatter do
  failures = {}

  TEST_FILE_DIR = File.join(File.dirname(__FILE__), "locales")

  module RbnfTestHelpers
    class << self
      def test_file_for(locale)
        File.join(TEST_FILE_DIR, locale, "rbnf_test.yml")
      end

      def allowed_failures
        @allowed_failures ||= begin
          YAML.load_file(
            File.join(File.dirname(__FILE__), "allowed_failures.yml")
          )
        end
      end

      def allowed_failure?(options = {})
        trail = [:locale, :group, :rule_set]
        dest = trail.inject(allowed_failures) do |ret, leg|
          if ret && ret[options[leg]]
            ret[options[leg]]
          else
            ret
          end
        end

        if dest
          dest.include?(options[:number])
        else
          false
        end
      end
    end
  end

  TwitterCldr.supported_locales.each do |locale|
    next unless described_class.supported_locale?(locale)

    formatter = described_class.new(locale)
    test_data = YAML.load_file(RbnfTestHelpers.test_file_for(locale.to_s))

    describe locale.localize.as_language_code do
      formatter.group_names.each do |group_name|

        describe group_name do
          formatter.rule_set_names_for_group(group_name).each do |rule_set_name|

            describe rule_set_name.gsub("-", " ") do

              # running basic test suite only runs spellout-numbering tests (for speed)
              it "formats correctly", slow: rule_set_name != "spellout-numbering" do
                if locale == :id && rule_set_name == "spellout-ordinal"
                  pending(
                    "Skipping Indonesian spellout-ordinal test cases, see: " \
                    "https://unicode-org.atlassian.net/browse/CLDR-17730"
                  )
                end

                test_data[group_name][rule_set_name].each_pair do |number, expected|
                  got = formatter.format(number, {
                    rule_group: group_name,
                    rule_set: rule_set_name
                  })

                  if got != expected
                    opts = {
                      locale: locale, group: group_name,
                      rule_set: rule_set_name, number: number
                    }

                    unless RbnfTestHelpers.allowed_failure?(opts)
                      failures[locale] ||= {}
                      failures[locale][group_name] ||= {}
                      failures[locale][group_name][rule_set_name] ||= []
                      failures[locale][group_name][rule_set_name] << number
                      expect(got).to eq(expected)
                    end
                  else
                    expect(got).to eq(expected)
                  end

                end
              end

            end
          end
        end
      end
    end
  end

  after(:all) do
    if failures.size > 0
      puts "\nFailure hash:"
      puts failures.inspect
    end
  end

end
