github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/streams/utils_test.go (about)

     1  package streams
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/lmorg/murex/test/count"
     8  )
     9  
    10  func TestAppendBytes(t *testing.T) {
    11  	tests := []struct {
    12  		Slice    string
    13  		Data     string
    14  		Expected string
    15  	}{
    16  		{
    17  			Slice:    "",
    18  			Data:     "",
    19  			Expected: "",
    20  		},
    21  		{
    22  			Slice:    "",
    23  			Data:     "bar",
    24  			Expected: "bar",
    25  		},
    26  		{
    27  			Slice:    "foo",
    28  			Data:     "",
    29  			Expected: "foo",
    30  		},
    31  		{
    32  			Slice:    "foo",
    33  			Data:     "bar",
    34  			Expected: "foobar",
    35  		},
    36  		{
    37  			Slice:    "foo",
    38  			Data:     "barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar",
    39  			Expected: "foobarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar",
    40  		},
    41  	}
    42  
    43  	count.Tests(t, len(tests))
    44  
    45  	for i, test := range tests {
    46  		b := appendBytes([]byte(test.Slice), []byte(test.Data)...)
    47  		actual := string(b)
    48  		if actual != test.Expected {
    49  			t.Errorf("Actual does not match expected in test %d", i)
    50  			t.Logf("  Slice:    '%s'", test.Slice)
    51  			t.Logf("  Data:     '%s'", test.Data)
    52  			t.Logf("  Expected: '%s'", test.Expected)
    53  			t.Logf("  Actual:   '%s'", actual)
    54  		}
    55  	}
    56  }
    57  
    58  func TestSetDataType(t *testing.T) {
    59  	count.Tests(t, 2)
    60  
    61  	stream := NewStdin()
    62  
    63  	stream.SetDataType("foo")
    64  	dt := stream.GetDataType()
    65  	if dt != "foo" {
    66  		t.Errorf("Data type unset: '%s'", dt)
    67  	}
    68  
    69  	stream.SetDataType("bar")
    70  	dt = stream.GetDataType()
    71  	if dt != "foo" {
    72  		t.Errorf("Data type unset or overwritten: '%s'", dt)
    73  	}
    74  }
    75  
    76  func TestSetNewStream(t *testing.T) {
    77  	count.Tests(t, 3)
    78  
    79  	stream, err := newStream("")
    80  	if err != nil {
    81  		t.Errorf(err.Error())
    82  	}
    83  
    84  	stream.SetDataType("foo")
    85  	dt := stream.GetDataType()
    86  	if dt != "foo" {
    87  		t.Errorf("Data type unset: '%s'", dt)
    88  	}
    89  
    90  	stream.SetDataType("bar")
    91  	dt = stream.GetDataType()
    92  	if dt != "foo" {
    93  		t.Errorf("Data type unset or overwritten: '%s'", dt)
    94  	}
    95  }
    96  
    97  func TestSetOpenClose(t *testing.T) {
    98  	count.Tests(t, 2)
    99  
   100  	stream, err := newStream("")
   101  	if err != nil {
   102  		t.Errorf(err.Error())
   103  	}
   104  
   105  	stream.Open()
   106  	stream.Close()
   107  }
   108  
   109  func TestSetCloseError1(t *testing.T) {
   110  	count.Tests(t, 1)
   111  
   112  	stream, err := newStream("")
   113  	if err != nil {
   114  		t.Errorf(err.Error())
   115  	}
   116  
   117  	defer func() {
   118  		if r := recover(); r == nil {
   119  			t.Error("expecting a panic due to more closed dependant than open")
   120  		}
   121  	}()
   122  
   123  	stream.Close()
   124  }
   125  
   126  func TestSetOpenCloseError(t *testing.T) {
   127  	count.Tests(t, 3)
   128  
   129  	stream, err := newStream("")
   130  	if err != nil {
   131  		t.Errorf(err.Error())
   132  	}
   133  
   134  	stream.Open()
   135  	stream.Close()
   136  
   137  	defer func() {
   138  		if r := recover(); r == nil {
   139  			t.Error("expecting a panic due to more closed dependant than open")
   140  		}
   141  	}()
   142  
   143  	stream.Close()
   144  }
   145  
   146  func TestSetForceClose(t *testing.T) {
   147  	count.Tests(t, 3)
   148  
   149  	stream, err := newStream("")
   150  	if err != nil {
   151  		t.Errorf(err.Error())
   152  	}
   153  
   154  	stream.ForceClose()
   155  }
   156  
   157  func TestSetOpenForceClose(t *testing.T) {
   158  	count.Tests(t, 3)
   159  
   160  	stream, err := newStream("")
   161  	if err != nil {
   162  		t.Errorf(err.Error())
   163  	}
   164  
   165  	stream.Open()
   166  	stream.ForceClose()
   167  }
   168  
   169  func TestSetOpenForceCloseWithContext(t *testing.T) {
   170  	count.Tests(t, 3)
   171  
   172  	ctx := context.Background()
   173  	v := "not ran"
   174  	cancelFunc := func() {
   175  		v = "have run"
   176  	}
   177  
   178  	stream := NewStdinWithContext(ctx, cancelFunc)
   179  
   180  	stream.Open()
   181  	stream.ForceClose()
   182  
   183  	if v != "have run" {
   184  		t.Error("cancelFunc not invoked when stream was force closed")
   185  	}
   186  }