// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package ierror

import (
	"errors"
	"fmt"
	"io"
	"testing"
)

func TestIggyError_Error(t *testing.T) {
	cases := []struct {
		name     string
		err      error
		expected string
	}{
		{name: "with field", err: ErrInvalidTopicId, expected: "invalid topic id"},
		{name: "without field", err: TopicIdNotFound{1, 1}, expected: "topic with id: 1 for stream with id: 1 was not found."},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if got := c.err.Error(); got != c.expected {
				t.Errorf("Error() = %v, want %v", got, c.expected)
			}
		})
	}
}

func TestIggyError_Is(t *testing.T) {
	cases := []struct {
		name     string
		err      error
		target   error
		expected bool
	}{
		{
			name:     "different code",
			err:      InvalidCredentials{},
			target:   ErrInvalidStreamId,
			expected: false,
		}, {
			name:     "same type, different field",
			err:      ResourceNotFound{"key1"},
			target:   ResourceNotFound{"key2"},
			expected: true,
		},
		{
			name:     "wrapped error with same type",
			err:      fmt.Errorf("wrap: %w", ErrInvalidCredentials),
			target:   ErrInvalidCredentials,
			expected: true,
		},
		{
			name:     "compare with nil",
			err:      ErrInvalidCredentials,
			target:   nil,
			expected: false,
		},
		{
			name:     "compare with different type",
			err:      ErrInvalidCredentials,
			target:   io.EOF,
			expected: false,
		},
	}

	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if got := errors.Is(c.err, c.target); got != c.expected {
				t.Errorf("errors.Is(%v, %v) = %v, want %v", c.err, c.target, got, c.expected)
			}
		})
	}
}
