initial commits
parent
ed553874c4
commit
f8e5f3fd28
|
@ -0,0 +1,36 @@
|
|||
package raft
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"sourcecode.social/reiver/go-erorr"
|
||||
)
|
||||
|
||||
// writeHeader writes the header of a RAFT file.
|
||||
//
|
||||
// It writes:
|
||||
//
|
||||
// "RAFT/1\n\n"
|
||||
//
|
||||
// Technically, the 2nd "\n" isn't necessary.
|
||||
// Technically, this is all that is necessary is "RAFT/1\n"
|
||||
//
|
||||
// The 2nd "\n" is added to make the resulting RAFT file easier to read (if a person was to look at it and try to read it).
|
||||
func writeHeader(writer io.Writer) error {
|
||||
|
||||
if nil == writer {
|
||||
return errNilWriter
|
||||
}
|
||||
|
||||
{
|
||||
var header string = "RAFT/1" + eoleol
|
||||
|
||||
err := writeString(writer, header)
|
||||
if nil != err {
|
||||
return erorr.Errorf("raft: problem writing first 2 lines of raft file — %q: %w", header, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package raft
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"strings"
|
||||
)
|
||||
|
||||
func TestWriteHeader(t *testing.T) {
|
||||
|
||||
var expected string = "RAFT/1\n\n"
|
||||
|
||||
var actual string
|
||||
{
|
||||
var buffer strings.Builder
|
||||
|
||||
|
||||
err := writeHeader(&buffer)
|
||||
if nil != err {
|
||||
t.Errorf("Did not expect an error but actually got one.")
|
||||
t.Logf("ERROR: %q", err)
|
||||
return
|
||||
}
|
||||
|
||||
actual = buffer.String()
|
||||
}
|
||||
|
||||
{
|
||||
if expected != actual {
|
||||
t.Errorf("The actual written value is not what was expected.")
|
||||
t.Logf("EXPECTED: %q", expected)
|
||||
t.Logf("ACTUAL: %q", actual)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue