gobot.io/x/gobot@v1.16.0/sysfs/fs_mock_test.go (about) 1 package sysfs 2 3 import ( 4 "testing" 5 6 "gobot.io/x/gobot/gobottest" 7 ) 8 9 func TestMockFilesystemOpen(t *testing.T) { 10 fs := NewMockFilesystem([]string{"foo"}) 11 f1 := fs.Files["foo"] 12 13 gobottest.Assert(t, f1.Opened, false) 14 f2, err := fs.OpenFile("foo", 0, 0666) 15 gobottest.Assert(t, f1, f2) 16 gobottest.Assert(t, err, nil) 17 18 err = f2.Sync() 19 gobottest.Assert(t, err, nil) 20 21 _, err = fs.OpenFile("bar", 0, 0666) 22 gobottest.Refute(t, err, nil) 23 24 fs.Add("bar") 25 f4, _ := fs.OpenFile("bar", 0, 0666) 26 gobottest.Refute(t, f4.Fd(), f1.Fd()) 27 } 28 29 func TestMockFilesystemStat(t *testing.T) { 30 fs := NewMockFilesystem([]string{"foo", "bar/baz"}) 31 32 fileStat, err := fs.Stat("foo") 33 gobottest.Assert(t, err, nil) 34 gobottest.Assert(t, fileStat.IsDir(), false) 35 36 dirStat, err := fs.Stat("bar") 37 gobottest.Assert(t, err, nil) 38 gobottest.Assert(t, dirStat.IsDir(), true) 39 40 _, err = fs.Stat("plonk") 41 gobottest.Refute(t, err, nil) 42 } 43 44 func TestMockFilesystemWrite(t *testing.T) { 45 fs := NewMockFilesystem([]string{"bar"}) 46 f1 := fs.Files["bar"] 47 48 f2, err := fs.OpenFile("bar", 0, 0666) 49 gobottest.Assert(t, err, nil) 50 // Never been read or written. 51 gobottest.Assert(t, f1.Seq <= 0, true) 52 53 f2.WriteString("testing") 54 // Was written. 55 gobottest.Assert(t, f1.Seq > 0, true) 56 gobottest.Assert(t, f1.Contents, "testing") 57 } 58 59 func TestMockFilesystemRead(t *testing.T) { 60 fs := NewMockFilesystem([]string{"bar"}) 61 f1 := fs.Files["bar"] 62 f1.Contents = "Yip" 63 64 f2, err := fs.OpenFile("bar", 0, 0666) 65 gobottest.Assert(t, err, nil) 66 // Never been read or written. 67 gobottest.Assert(t, f1.Seq <= 0, true) 68 69 buffer := make([]byte, 20) 70 n, _ := f2.Read(buffer) 71 72 // Was read. 73 gobottest.Assert(t, f1.Seq > 0, true) 74 gobottest.Assert(t, n, 3) 75 gobottest.Assert(t, string(buffer[:3]), "Yip") 76 77 n, _ = f2.ReadAt(buffer, 10) 78 gobottest.Assert(t, n, 3) 79 }