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 NewReader ¶
Example ¶
// showing: // - new reader r, err := NewReader("nop://file.txt") if r == nil { return } fmt.Println(err) // output: <nil> fmt.Println(r.sts.Path) // output: nop://file.txt
Output: <nil> nop://file.txt
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) // output: <nil> fmt.Println(r.sts.Size) // output: 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) // output: 10 fmt.Println(err) // output: <nil> fmt.Println(r.sts.ByteCnt) // output: 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)) // output: mock line fmt.Println(err) // output: <nil> fmt.Println(r.sts.ByteCnt) // output: 10 fmt.Println(r.sts.LineCnt) // output: 1
Output: mock line <nil> 10 1
func (*Reader) Stats ¶
Example ¶
// showing: // - Reader.Stats() happy path r, _ := NewReader("nop://file.txt") if r == nil { return } r.sts.LineCnt = 10 r.sts.ByteCnt = 100 r.sts.Created = "created date" r.sts.Size = 200 r.sts.Checksum = "checksum" sts := r.Stats() fmt.Println(sts.Path) // output: nop://file.txt fmt.Println(sts.LineCnt) // output: 10 fmt.Println(sts.ByteCnt) // output: 100 fmt.Println(sts.Created) // output: created date fmt.Println(sts.Size) // output: 200 fmt.Println(sts.Checksum) // output: checksum
Output: nop://file.txt 10 100 created date 200 checksum
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) // output: <nil> fmt.Println(w.sts.Path) // output: 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) // output: <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) // output: <nil> fmt.Println(w.sts.Size) // output: 10 fmt.Println(isCreated) // output: 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) // output: 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) // output: 9 fmt.Println(err) // output: <nil> fmt.Println(w.sts.ByteCnt) // output: 9 fmt.Println(w.sts.LineCnt) // output: 0
Output: 9 <nil> 9 0
func (*Writer) WriteLine ¶
Example ¶
// showing: // - writer.WriteLine happy path w, _ := NewWriter("nop://file.txt") if w == nil { return } err := w.WriteLine([]byte("test line")) fmt.Println(err) // output: <nil> fmt.Println(w.sts.ByteCnt) // output: 10 fmt.Println(w.sts.LineCnt) // output: 1
Output: <nil> 10 1