ADDED src/0dev.org/ioutil/ioutil.go Index: src/0dev.org/ioutil/ioutil.go ================================================================== --- /dev/null +++ src/0dev.org/ioutil/ioutil.go @@ -0,0 +1,18 @@ +// Package ioutil contains various constructs for io operations +package ioutil + +// An function alias type that implements io.Writer +type WriterFunc func([]byte) (int, error) + +// Delegates the call to the WriterFunc while implementing io.Writer +func (w WriterFunc) Write(b []byte) (int, error) { + return w(b) +} + +// An function alias type that implements io.Reader +type ReaderFunc func([]byte) (int, error) + +// Delegates the call to the WriterFunc while implementing io.Reader +func (r ReaderFunc) Read(b []byte) (int, error) { + return r(b) +} ADDED src/0dev.org/ioutil/ioutil_test.go Index: src/0dev.org/ioutil/ioutil_test.go ================================================================== --- /dev/null +++ src/0dev.org/ioutil/ioutil_test.go @@ -0,0 +1,47 @@ +package ioutil + +import ( + diff "0dev.org/diff" + "bytes" + "testing" +) + +func TestWriter(t *testing.T) { + var ( + input []byte = []byte{0, 1, 2, 3, 4, 5, 6, 7} + output []byte + + reader *bytes.Reader = bytes.NewReader(input) + buffer bytes.Buffer + ) + + reader.WriteTo(WriterFunc(buffer.Write)) + output = buffer.Bytes() + + // Diff the result against the initial input + delta := diff.Diff(diff.D{len(input), len(output), + func(i, j int) bool { return input[i] == output[j] }}) + if len(delta.Added) > 0 || len(delta.Removed) > 0 { + t.Error("Differences detected ", delta) + } +} + +func TestReader(t *testing.T) { + var ( + input []byte = []byte{0, 1, 2, 3, 4, 5, 6, 7} + output []byte + + reader *bytes.Reader = bytes.NewReader(input) + buffer bytes.Buffer + ) + + buffer.ReadFrom(ReaderFunc(reader.Read)) + output = buffer.Bytes() + + // Diff the result against the initial input + delta := diff.Diff(diff.D{len(input), len(output), + func(i, j int) bool { return input[i] == output[j] }}) + if len(delta.Added) > 0 || len(delta.Removed) > 0 { + t.Error("Differences detected ", delta) + } +}