# frozen_string_literal: true

require "test_helper"
require "form_template"

class FormTemplateTest < Minitest::Test
	def test_form_one_field
		template = FormTemplate.new(<<~TEMPLATE)
			form!
			title "TITLE"
			instructions "INSTRUCTIONS"
			field(var: "thevar", label: "thelabel")
		TEMPLATE
		form = template.render
		assert_equal :form, form.type
		assert_equal "TITLE", form.title
		assert_equal "INSTRUCTIONS", form.instructions
		assert_equal 1, form.fields.length
		assert_equal "thevar", form.fields[0].var
		assert_equal "thelabel", form.fields[0].label
	end

	def test_form_two_fields
		template = FormTemplate.new(<<~TEMPLATE)
			form!
			field(var: "thevar", label: "thelabel")
			field(var: "thevar2", label: "thelabel2")
		TEMPLATE
		form = template.render
		assert_equal 2, form.fields.length
		assert_equal "thevar", form.fields[0].var
		assert_equal "thelabel", form.fields[0].label
		assert_equal "thevar2", form.fields[1].var
		assert_equal "thelabel2", form.fields[1].label
	end

	def test_result_no_fields
		template = FormTemplate.new(<<~TEMPLATE)
			result!
			title "TITLE"
			instructions "INSTRUCTIONS"
		TEMPLATE
		form = template.render
		assert_equal :result, form.type
		assert_equal "TITLE", form.title
		assert_equal "INSTRUCTIONS", form.instructions
	end

	def test_no_type
		template = FormTemplate.new(<<~TEMPLATE)
			title "TITLE"
			instructions "INSTRUCTIONS"
		TEMPLATE
		assert_raises { template.render }
	end

	def test_custom_xml
		template = FormTemplate.new(<<~TEMPLATE)
			form!
			xml.whoever @arg
		TEMPLATE
		form = template.render(arg: "abc")
		assert_equal "abc", form.at("whoever").content
	end
end
