# parse_control() tests
#
# Copyright (C) 2019  Patrick McDermott
#
# This file is part of the ProteanOS Development Kit.
#
# The ProteanOS Development Kit is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# The ProteanOS Development Kit is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the ProteanOS Development Kit.  If not, see
# <http://www.gnu.org/licenses/>.

set -eu

LF='
'
CONTROL="$(cat <<-'EOF'
	Package: foo
	Version: 1.0
	Description: Frobnicator of Objects
	 This is a frobnicator of objects.  Conveniently, it also bit-bangs all
	 registers.

	Package: bar
	Version: 2.0
	Description: Bit-bang All Registers
	 This is a frobnicator of objects.  Conveniently, it also bit-bangs all
	 registers.
	EOF
	)"

field=0
para=0

field_1()
{
	local name="${1}"
	local value="${2}"
	shift 2

	is 'package field name' "${name}" 'Package'
	is 'package field value' "${value}" 'foo'

	return 0
}

field_2()
{
	local name="${1}"
	local value="${2}"
	shift 2

	is 'version field name' "${name}" 'Version'
	is 'version field value' "${value}" '1.0'

	return 0
}

field_3()
{
	local name="${1}"
	local value="${2}"
	shift 2

	is 'description field name' "${name}" 'Description'
	is_diff 'description field value' "${value}" "$(cat <<-'EOF'
	Frobnicator of Objects
	This is a frobnicator of objects.  Conveniently, it also bit-bangs all
	registers.
	EOF
	)"

	return 0
}

field_4()
{
	local name="${1}"
	local value="${2}"
	shift 2

	is 'package field name' "${name}" 'Package'
	is 'package field value' "${value}" 'bar'

	return 0
}

field_5()
{
	local name="${1}"
	local value="${2}"
	shift 2

	is 'version field name' "${name}" 'Version'
	is 'version field value' "${value}" '2.0'

	return 0
}

field_6()
{
	local name="${1}"
	local value="${2}"
	shift 2

	is 'description field name' "${name}" 'Description'
	is_diff 'description field value' "${value}" "$(cat <<-'EOF'
	Bit-bang All Registers
	This is a frobnicator of objects.  Conveniently, it also bit-bangs all
	registers.
	EOF
	)"

	return 0
}

field_7()
{
	bailout_ 'too many fields'
}

field_cb()
{
	field=$((${field} + 1))
	"field_${field}" "${@}"
}

para_1()
{
	is 'number of parsed fields' "${field}" 3
}

para_2()
{
	is 'number of parsed fields' "${field}" 6
}

para_3()
{
	bailout_ 'too many paragraphs'
}

para_cb()
{
	para=$((${para} + 1))
	"para_${para}" "${@}"
}

main()
{
	plan_ 15

	parse_control - field_cb para_cb 'Package' <<-EOF
		${CONTROL}
		EOF

	is 'number of parsed paragraphs' "${para}" 2

	return 0
}