// Copyright 2020 Douyu
//
// Licensed 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 defers

import (
	"testing"

	"github.com/douyu/jupiter/pkg/util/xdefer"
	"github.com/stretchr/testify/assert"
)

func TestRegister(t *testing.T) {
	var str string
	type args struct {
		fns []func() error
	}
	tests := []struct {
		name string
		args args
	}{
		// TODO: Add test cases.
		{
			name: "register",
			args: args{
				fns: []func() error{
					func() error { str += "1,"; return nil },
					func() error { str += "2,"; return nil },
					func() error { str += "3,"; return nil },
					func() error { str += "4,"; return nil },
					nil,
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			Register(tt.args.fns...)
		})
	}
}

func TestClean(t *testing.T) {
	var str string
	globalDefers = xdefer.NewStack()
	globalDefers.Push(
		func() error { str += "1,"; return nil },
		func() error { str += "2,"; return nil },
		func() error { str += "3,"; return nil },
		func() error { str += "4,"; return nil },
	)

	tests := []struct {
		name string
	}{
		{
			"testing",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			Clean()
			assert.Equal(t, str, "4,3,2,1,")
		})
	}
}
