package tw_test

import (
	"encoding/xml"
	"testing"

	"gitea.dwysokinski.me/twhelp/corev3/internal/tw"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestKnightNewItems_UnmarshalXML(t *testing.T) {
	t.Parallel()

	tests := []struct {
		data        string
		expected    tw.KnightNewItems
		expectedErr error
	}{
		{
			data:     "<knight_new_items/>",
			expected: 0,
		},
		{
			data:     "<knight_new_items>Off</knight_new_items>",
			expected: 0,
		},
		{
			data:     "<knight_new_items>0</knight_new_items>",
			expected: 0,
		},
		{
			data:     "<knight_new_items>On</knight_new_items>",
			expected: 1,
		},
		{
			data:     "<knight_new_items>ON</knight_new_items>",
			expected: 1,
		},
		{
			data:     "<knight_new_items>on</knight_new_items>",
			expected: 1,
		},
		{
			data:     "<knight_new_items>1</knight_new_items>",
			expected: 1,
		},
		{
			data:        "<knight_new_items>invalid value</knight_new_items>",
			expectedErr: xml.UnmarshalError(`KnightNewItems: parsing "invalid value": invalid syntax`),
		},
	}

	for _, tt := range tests {
		tt := tt

		t.Run(tt.data, func(t *testing.T) {
			t.Parallel()

			var res tw.KnightNewItems
			require.ErrorIs(t, tt.expectedErr, xml.Unmarshal([]byte(tt.data), &res))
			assert.Equal(t, tt.expected, res)
		})
	}
}
