...

Source file src/github.com/google/go-containerregistry/internal/and/and_closer_test.go

Documentation: github.com/google/go-containerregistry/internal/and

     1  // Copyright 2020 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package and
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"testing"
    21  )
    22  
    23  func TestRead(t *testing.T) {
    24  	want := "asdf"
    25  	r := bytes.NewBufferString(want)
    26  	called := false
    27  
    28  	rac := &ReadCloser{
    29  		Reader: r,
    30  		CloseFunc: func() error {
    31  			called = true
    32  			return nil
    33  		},
    34  	}
    35  
    36  	data, err := io.ReadAll(rac)
    37  	if err != nil {
    38  		t.Errorf("ReadAll(rac) = %v", err)
    39  	}
    40  	if got := string(data); got != want {
    41  		t.Errorf("ReadAll(rac); got %q, want %q", got, want)
    42  	}
    43  
    44  	if called {
    45  		t.Error("called before Close(); got true, wanted false")
    46  	}
    47  	if err := rac.Close(); err != nil {
    48  		t.Errorf("Close() = %v", err)
    49  	}
    50  	if !called {
    51  		t.Error("called after Close(); got false, wanted true")
    52  	}
    53  }
    54  
    55  func TestWrite(t *testing.T) {
    56  	w := bytes.NewBuffer([]byte{})
    57  	called := false
    58  
    59  	wac := &WriteCloser{
    60  		Writer: w,
    61  		CloseFunc: func() error {
    62  			called = true
    63  			return nil
    64  		},
    65  	}
    66  
    67  	want := "asdf"
    68  	if _, err := wac.Write([]byte(want)); err != nil {
    69  		t.Errorf("Write(%q); = %v", want, err)
    70  	}
    71  
    72  	if called {
    73  		t.Error("called before Close(); got true, wanted false")
    74  	}
    75  	if err := wac.Close(); err != nil {
    76  		t.Errorf("Close() = %v", err)
    77  	}
    78  	if !called {
    79  		t.Error("called after Close(); got false, wanted true")
    80  	}
    81  
    82  	if got := w.String(); got != want {
    83  		t.Errorf("w.String(); got %q, want %q", got, want)
    84  	}
    85  }
    86  

View as plain text