github.com/KEINOS/go-countline@v1.1.1-0.20221217083629-60710df7606b/cl/cl_test.go (about)

     1  package cl
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/KEINOS/go-countline/cl/spec"
     7  	"github.com/pkg/errors"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // ============================================================================
    12  //  Tests
    13  // ============================================================================
    14  
    15  func TestCountLines_golden(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	spec.RunSpecTest(t, "CountLines", CountLines)
    19  }
    20  
    21  func TestCountLines_nil_input(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	numLines, err := CountLines(nil)
    25  
    26  	require.Error(t, err)
    27  	require.Equal(t, 0, numLines, "returned number of lines should be 0 on error")
    28  	require.Contains(t, err.Error(), "given reader is nil")
    29  }
    30  
    31  type DummyReader struct{}
    32  
    33  func (r *DummyReader) Read(p []byte) (int, error) {
    34  	return 0, errors.New("forced error")
    35  }
    36  
    37  func TestCountLines_io_read_fail(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	dummyReader := &DummyReader{}
    41  
    42  	numLines, err := CountLines(dummyReader)
    43  
    44  	require.Error(t, err)
    45  	require.Equal(t, 0, numLines, "returned number of lines should be 0 on error")
    46  	require.Contains(t, err.Error(), "failed to read from reader")
    47  	require.Contains(t, err.Error(), "forced error", "the error should contain the reason of the error")
    48  }