github.com/gogf/gf@v1.16.9/os/gfile/gfile_z_readline_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gfile_test 8 9 import ( 10 "github.com/gogf/gf/debug/gdebug" 11 "github.com/gogf/gf/errors/gerror" 12 "testing" 13 14 "github.com/gogf/gf/os/gfile" 15 "github.com/gogf/gf/test/gtest" 16 ) 17 18 func Test_NotFound(t *testing.T) { 19 gtest.C(t, func(t *gtest.T) { 20 teatFile := gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/error.log" 21 callback := func(line string) error { 22 return nil 23 } 24 err := gfile.ReadLines(teatFile, callback) 25 t.AssertNE(err, nil) 26 }) 27 } 28 29 func Test_ReadLines(t *testing.T) { 30 gtest.C(t, func(t *gtest.T) { 31 var ( 32 expectList = []string{"a", "b", "c", "d", "e"} 33 getList = make([]string, 0) 34 callback = func(line string) error { 35 getList = append(getList, line) 36 return nil 37 } 38 teatFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" 39 ) 40 err := gfile.ReadLines(teatFile, callback) 41 t.AssertEQ(getList, expectList) 42 t.AssertEQ(err, nil) 43 }) 44 } 45 46 func Test_ReadLines_Error(t *testing.T) { 47 gtest.C(t, func(t *gtest.T) { 48 var ( 49 callback = func(line string) error { 50 return gerror.New("custom error") 51 } 52 teatFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" 53 ) 54 err := gfile.ReadLines(teatFile, callback) 55 t.AssertEQ(err.Error(), "custom error") 56 }) 57 } 58 59 func Test_ReadLinesBytes(t *testing.T) { 60 gtest.C(t, func(t *gtest.T) { 61 var ( 62 expectList = [][]byte{[]byte("a"), []byte("b"), []byte("c"), []byte("d"), []byte("e")} 63 getList = make([][]byte, 0) 64 callback = func(line []byte) error { 65 getList = append(getList, line) 66 return nil 67 } 68 teatFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" 69 ) 70 err := gfile.ReadLinesBytes(teatFile, callback) 71 t.AssertEQ(getList, expectList) 72 t.AssertEQ(err, nil) 73 }) 74 } 75 76 func Test_ReadLinesBytes_Error(t *testing.T) { 77 gtest.C(t, func(t *gtest.T) { 78 var ( 79 callback = func(line []byte) error { 80 return gerror.New("custom error") 81 } 82 teatFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log" 83 ) 84 err := gfile.ReadLinesBytes(teatFile, callback) 85 t.AssertEQ(err.Error(), "custom error") 86 }) 87 }