<commit_before>package ext_test

import (
	"fmt"
	"testing"

	"github.com/goph/stdlib/ext"
	"github.com/goph/stdlib/internal/mocks"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type testCloser struct {
	called bool
	err    error
}

func (c *testCloser) Close() error {
	c.called = true

	return c.err
}

func TestCloserFunc_CallsUnderlyingFunc(t *testing.T) {
	var called bool

	closer := ext.CloserFunc(func() {
		called = true
	})

	var err error

	if got, want := closer.Close(), err; got != want {
		t.Errorf("wrapped functions are expected to return nil, error received: %v", got)
	}

	if called != true {
		t.Error("the wrapped function is expected to be called")
	}
}

func TestCloserFunc_RecoversErrorPanic(t *testing.T) {
	err := fmt.Errorf("internal error")

	closer := ext.CloserFunc(func() {
		panic(err)
	})

	assert.EqualError(t, closer.Close(), "internal error")
}

func TestClosers(t *testing.T) {
	closer1 := &mocks.Closer{}
	closer1.On("Close").Return(nil)

	closer2 := &mocks.Closer{}
	closer2.On("Close").Return(nil)

	closer := ext.Closers{closer1, closer2}

	assert.NoError(t, closer.Close())
	closer1.AssertExpectations(t)
	closer2.AssertExpectations(t)
}

func TestClosers_Empty(t *testing.T) {
	closer := ext.Closers{}

	assert.NoError(t, closer.Close())
}

func TestClosers_Error(t *testing.T) {
	closer1 := &mocks.Closer{}
	closer1.On("Close").Return(nil)

	err := fmt.Errorf("error")
	closer2 := &mocks.Closer{}
	closer2.On("Close").Return(err)

	closer := ext.Closers{closer1, closer2}

	merr := closer.Close()

	type errorCollection interface {
		Errors() []error
	}

	require.Error(t, merr)
	require.Implements(t, (*errorCollection)(nil), merr)
	assert.Contains(t, merr.(errorCollection).Errors(), err)

	closer1.AssertExpectations(t)
	closer2.AssertExpectations(t)
}

func TestClose(t *testing.T) {
	err := fmt.Errorf("error")
	closer := ext.CloserFunc(func() { panic(err) })

	assert.EqualError(t, closer.Close(), "error")
}
<commit_msg>Remove unused closer<commit_after>package ext_test

import (
	"fmt"
	"testing"

	"github.com/goph/stdlib/ext"
	"github.com/goph/stdlib/internal/mocks"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestCloserFunc_CallsUnderlyingFunc(t *testing.T) {
	var called bool

	closer := ext.CloserFunc(func() {
		called = true
	})

	var err error

	if got, want := closer.Close(), err; got != want {
		t.Errorf("wrapped functions are expected to return nil, error received: %v", got)
	}

	if called != true {
		t.Error("the wrapped function is expected to be called")
	}
}

func TestCloserFunc_RecoversErrorPanic(t *testing.T) {
	err := fmt.Errorf("internal error")

	closer := ext.CloserFunc(func() {
		panic(err)
	})

	assert.EqualError(t, closer.Close(), "internal error")
}

func TestClosers(t *testing.T) {
	closer1 := &mocks.Closer{}
	closer1.On("Close").Return(nil)

	closer2 := &mocks.Closer{}
	closer2.On("Close").Return(nil)

	closer := ext.Closers{closer1, closer2}

	assert.NoError(t, closer.Close())
	closer1.AssertExpectations(t)
	closer2.AssertExpectations(t)
}

func TestClosers_Empty(t *testing.T) {
	closer := ext.Closers{}

	assert.NoError(t, closer.Close())
}

func TestClosers_Error(t *testing.T) {
	closer1 := &mocks.Closer{}
	closer1.On("Close").Return(nil)

	err := fmt.Errorf("error")
	closer2 := &mocks.Closer{}
	closer2.On("Close").Return(err)

	closer := ext.Closers{closer1, closer2}

	merr := closer.Close()

	type errorCollection interface {
		Errors() []error
	}

	require.Error(t, merr)
	require.Implements(t, (*errorCollection)(nil), merr)
	assert.Contains(t, merr.(errorCollection).Errors(), err)

	closer1.AssertExpectations(t)
	closer2.AssertExpectations(t)
}

func TestClose(t *testing.T) {
	err := fmt.Errorf("error")
	closer := ext.CloserFunc(func() { panic(err) })

	assert.EqualError(t, closer.Close(), "error")
}

{'fraction_non_alphanumeric': 0.161439588688946, 'fraction_numerical': 0.008226221079691516, 'mean_word_length': 4.790178571428571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1256028', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1480, 'n_words': 276}