...

Source file src/io/fs/format_test.go

Documentation: io/fs

     1  // Copyright 2023 The Go 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 fs_test
     6  
     7  import (
     8  	. "io/fs"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  // formatTest implements FileInfo to test FormatFileInfo,
    14  // and implements DirEntry to test FormatDirEntry.
    15  type formatTest struct {
    16  	name    string
    17  	size    int64
    18  	mode    FileMode
    19  	modTime time.Time
    20  	isDir   bool
    21  }
    22  
    23  func (fs *formatTest) Name() string {
    24  	return fs.name
    25  }
    26  
    27  func (fs *formatTest) Size() int64 {
    28  	return fs.size
    29  }
    30  
    31  func (fs *formatTest) Mode() FileMode {
    32  	return fs.mode
    33  }
    34  
    35  func (fs *formatTest) ModTime() time.Time {
    36  	return fs.modTime
    37  }
    38  
    39  func (fs *formatTest) IsDir() bool {
    40  	return fs.isDir
    41  }
    42  
    43  func (fs *formatTest) Sys() any {
    44  	return nil
    45  }
    46  
    47  func (fs *formatTest) Type() FileMode {
    48  	return fs.mode.Type()
    49  }
    50  
    51  func (fs *formatTest) Info() (FileInfo, error) {
    52  	return fs, nil
    53  }
    54  
    55  var formatTests = []struct {
    56  	input        formatTest
    57  	wantFileInfo string
    58  	wantDirEntry string
    59  }{
    60  	{
    61  		formatTest{
    62  			name:    "hello.go",
    63  			size:    100,
    64  			mode:    0o644,
    65  			modTime: time.Date(1970, time.January, 1, 12, 0, 0, 0, time.UTC),
    66  			isDir:   false,
    67  		},
    68  		"-rw-r--r-- 100 1970-01-01 12:00:00 hello.go",
    69  		"- hello.go",
    70  	},
    71  	{
    72  		formatTest{
    73  			name:    "home/gopher",
    74  			size:    0,
    75  			mode:    ModeDir | 0o755,
    76  			modTime: time.Date(1970, time.January, 1, 12, 0, 0, 0, time.UTC),
    77  			isDir:   true,
    78  		},
    79  		"drwxr-xr-x 0 1970-01-01 12:00:00 home/gopher/",
    80  		"d home/gopher/",
    81  	},
    82  	{
    83  		formatTest{
    84  			name:    "big",
    85  			size:    0x7fffffffffffffff,
    86  			mode:    ModeIrregular | 0o644,
    87  			modTime: time.Date(1970, time.January, 1, 12, 0, 0, 0, time.UTC),
    88  			isDir:   false,
    89  		},
    90  		"?rw-r--r-- 9223372036854775807 1970-01-01 12:00:00 big",
    91  		"? big",
    92  	},
    93  	{
    94  		formatTest{
    95  			name:    "small",
    96  			size:    -0x8000000000000000,
    97  			mode:    ModeSocket | ModeSetuid | 0o644,
    98  			modTime: time.Date(1970, time.January, 1, 12, 0, 0, 0, time.UTC),
    99  			isDir:   false,
   100  		},
   101  		"Surw-r--r-- -9223372036854775808 1970-01-01 12:00:00 small",
   102  		"S small",
   103  	},
   104  }
   105  
   106  func TestFormatFileInfo(t *testing.T) {
   107  	for i, test := range formatTests {
   108  		got := FormatFileInfo(&test.input)
   109  		if got != test.wantFileInfo {
   110  			t.Errorf("%d: FormatFileInfo(%#v) = %q, want %q", i, test.input, got, test.wantFileInfo)
   111  		}
   112  	}
   113  }
   114  
   115  func TestFormatDirEntry(t *testing.T) {
   116  	for i, test := range formatTests {
   117  		got := FormatDirEntry(&test.input)
   118  		if got != test.wantDirEntry {
   119  			t.Errorf("%d: FormatDirEntry(%#v) = %q, want %q", i, test.input, got, test.wantDirEntry)
   120  		}
   121  	}
   122  
   123  }
   124  

View as plain text