# frozen_string_literal: true

require 'test_helper'

class KDL::V1::SpecTest < Minitest::Test
  TEST_CASES_DIR = File.join(__dir__, 'kdl-org/tests/test_cases')
  INPUTS_DIR = File.join(TEST_CASES_DIR, 'input')
  EXPECTED_DIR = File.join(TEST_CASES_DIR, 'expected_kdl')

  EXCLUDE = %w[
    escline_comment_node
  ]

  Dir.glob(File.join(INPUTS_DIR, '*.kdl')).each do |input_path|
    input_name = File.basename(input_path, '.kdl')
    next if EXCLUDE.include?(input_name)
    expected_path = File.join(EXPECTED_DIR, "#{input_name}.kdl")
    if File.exist?(expected_path)
      define_method "test_v1_#{input_name}_matches_expected_output" do
        expected = File.read(expected_path, encoding: Encoding::UTF_8)
        assert_equal expected, ::KDL.load_file(input_path, version: 1).to_s
    end
    else
      define_method "test_v1_#{input_name}_does_not_parse" do
        err = assert_raises { ::KDL.load_file(input_path, version: 1) }
        raise err unless err.is_a?(KDL::Error) || err.is_a?(Racc::ParseError)
      end
    end
  end
end
