Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var EOFChan = make(chan interface{})
EOFChan can be closed to simulate reading the end of a file. The MockLine value will be returned with simulated end-of-file behavior.
var MockLine = []byte("mock line\n")
MockLine can be used for setting the return value of a call to Read() or ReadLine(). The set value will be returned unless Reader is initialized with a MockReader value that indicates returning an error.
var MsgChan = make(chan []byte)
MsgChan can be used to set a read message value. The call to Read or ReadLine will block until a mock message is send on MsgChan. This way both the value and timing of a read call can be controlled. This can be useful when testing.
The reader only listens on MsgChan if len(MockLine) is 0.
If EOFChan is closed then and a message is written to MsgChan the reader may behave as if it reached an end-of-file or it may return the MsgChan message. If it is desired to return a message with end-of-file behavior then make sure MockLine is set with the desired return value.
Functions ¶
Types ¶
type Reader ¶
type Reader struct {
MockReadMode string
// contains filtered or unexported fields
}
func (*Reader) Close ¶
Example ¶
// showing:
// - Reader.ReadLine() returning an error
r, _ := NewReader("nop://file.txt")
if r == nil {
return
}
r.sts.ByteCnt = 10
err := r.Close()
fmt.Println(err) // <nil>
fmt.Println(r.sts.Size) // 10
Output: <nil> 10
func (*Reader) Read ¶
Read will return n as len(MockLine) or length of MsgChan bytes.
Example ¶
// showing:
// - Reader.Read() happy path
r, _ := NewReader("nop://file.txt")
if r == nil {
return
}
buf := make([]byte, 100)
n, err := r.Read(buf)
fmt.Println(n) // 10
fmt.Println(err) // <nil>
fmt.Println(r.sts.ByteCnt) // 10
Output: 10 <nil> 10
func (*Reader) ReadLine ¶
Example ¶
// showing:
// - Reader.ReadLine() happy path
r, _ := NewReader("nop://file.txt")
if r == nil {
return
}
ln, err := r.ReadLine()
fmt.Print(string(ln)) // mock line
fmt.Println(err) // <nil>
fmt.Println(r.sts.ByteCnt) // 10
fmt.Println(r.sts.LineCnt) // 1
Output: mock line <nil> 10 1
type Writer ¶
type Writer struct {
MockWriteMode string
// contains filtered or unexported fields
}
Writer is a no-operation writer useful for testing.
func NewWriter ¶
Example ¶
// showing:
// - new writer happy path
w, err := NewWriter("nop://file.txt")
if w == nil {
return
}
fmt.Println(err) // <nil>
fmt.Println(w.sts.Path()) // nop://file.txt
Output: <nil> nop://file.txt
func (*Writer) Abort ¶
Example ¶
// showing:
// - writer.Abort happy path
w, _ := NewWriter("nop://file.txt")
if w == nil {
return
}
err := w.Abort()
fmt.Println(err) // <nil>
Output: <nil>
func (*Writer) Close ¶
Example ¶
// showing:
// - writer.Close happy path
w, _ := NewWriter("nop://file.txt")
if w == nil {
return
}
w.sts.ByteCnt = 10 // Size is set from final byte count
err := w.Close()
isCreated := w.sts.Created() != "" // close sets sts.Created
fmt.Println(err) // <nil>
fmt.Println(w.sts.Size) // 10
fmt.Println(isCreated) // true
Output: <nil> 10 true
func (*Writer) Stats ¶
Example ¶
// showing:
// - writer.WriteLine happy path
w, _ := NewWriter("nop://file.txt")
if w == nil {
return
}
sts := w.Stats()
fmt.Println(sts.Path) // nop://file.txt
Output: nop://file.txt
func (*Writer) Write ¶
Example ¶
// showing:
// - writer.Write happy path
w, _ := NewWriter("nop://file.txt")
if w == nil {
return
}
n, err := w.Write([]byte("test line"))
fmt.Println(n) // 9
fmt.Println(err) // <nil>
fmt.Println(w.sts.ByteCnt) // 9
fmt.Println(w.sts.LineCnt) // 0
Output: 9 <nil> 9 0