github.com/rgonomic/rgo@v0.2.2-0.20220708095818-4747f0905d6e/internal/vfs/txtar/txtar_test.go (about)

     1  // Copyright ©2019 The rgonomic Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package txtar
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  var fileSystemTests = []struct {
    13  	data map[string][]string
    14  	want string
    15  }{
    16  	{
    17  		data: map[string][]string{
    18  			"a": {"A"},
    19  		},
    20  		want: `-- a --
    21  A
    22  `,
    23  	},
    24  	{
    25  		data: map[string][]string{
    26  			"b": {"B"},
    27  			"a": {"A"},
    28  		},
    29  		want: `-- a --
    30  A
    31  -- b --
    32  B
    33  `,
    34  	},
    35  	{
    36  		data: map[string][]string{
    37  			"b": {"B1", "B2"},
    38  			"a": {"A1", "A2"},
    39  		},
    40  		want: `-- a --
    41  A1A2
    42  -- b --
    43  B1B2
    44  `,
    45  	},
    46  	{
    47  		data: map[string][]string{
    48  			"path/to/b": {"B1", "B2"},
    49  			"path/to/a": {"A1", "A2"},
    50  		},
    51  		want: `-- path/to/a --
    52  A1A2
    53  -- path/to/b --
    54  B1B2
    55  `,
    56  	},
    57  }
    58  
    59  func TestFileSystem(t *testing.T) {
    60  	for _, test := range fileSystemTests {
    61  		var buf bytes.Buffer
    62  		fs := &FileSystem{Output: &buf}
    63  		for path, writes := range test.data {
    64  			w, err := fs.Open(path)
    65  			for _, d := range writes {
    66  				n, err := w.Write([]byte(d))
    67  				if err != nil {
    68  					t.Errorf("unexpected write error: %v", err)
    69  				}
    70  				if n != len(d) {
    71  					t.Errorf("unexpected number of bytes written for %q: got: %d want: %d", d, n, len(d))
    72  				}
    73  			}
    74  			err = w.Close()
    75  			if err != nil {
    76  				t.Errorf("unexpected close error: %v", err)
    77  			}
    78  		}
    79  		err := fs.Flush()
    80  		if err != nil {
    81  			t.Errorf("unexpected flush error: %v", err)
    82  		}
    83  		got := buf.String()
    84  		if got != test.want {
    85  			t.Errorf("unexpected result:\ngot:\n%s\nwant:\n%s", got, test.want)
    86  		}
    87  	}
    88  }