From 7709ee9b7f550797e5ca5da8adc5a849aea99ee0 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sun, 3 Dec 2017 23:11:29 +0100 Subject: [PATCH] more interface test --- common/buf/reader.go | 5 ----- common/buf/reader_test.go | 12 ++++++++++++ common/buf/writer.go | 19 +++---------------- common/buf/writer_test.go | 13 +++++++++++++ transport/internet/kcp/connection_test.go | 1 + 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/common/buf/reader.go b/common/buf/reader.go index f7b425e85..e2018e649 100644 --- a/common/buf/reader.go +++ b/common/buf/reader.go @@ -6,11 +6,6 @@ import ( "v2ray.com/core/common/errors" ) -var ( - _ Reader = (*BytesToBufferReader)(nil) - _ io.Reader = (*BytesToBufferReader)(nil) -) - // BytesToBufferReader is a Reader that adjusts its reading speed automatically. type BytesToBufferReader struct { io.Reader diff --git a/common/buf/reader_test.go b/common/buf/reader_test.go index 35c144323..66d70b4d0 100644 --- a/common/buf/reader_test.go +++ b/common/buf/reader_test.go @@ -70,3 +70,15 @@ func TestBytesReaderMultiBuffer(t *testing.T) { assert(mb[0].String(), Equals, "abc") assert(mb[1].String(), Equals, "efg") } + +func TestReaderInterface(t *testing.T) { + assert := With(t) + + assert((*BytesToBufferReader)(nil), Implements, (*io.Reader)(nil)) + assert((*BytesToBufferReader)(nil), Implements, (*Reader)(nil)) + + assert((*BufferedReader)(nil), Implements, (*Reader)(nil)) + assert((*BufferedReader)(nil), Implements, (*io.Reader)(nil)) + assert((*BufferedReader)(nil), Implements, (*io.ByteReader)(nil)) + assert((*BufferedReader)(nil), Implements, (*io.WriterTo)(nil)) +} diff --git a/common/buf/writer.go b/common/buf/writer.go index 6dbc886e3..56bcd8802 100644 --- a/common/buf/writer.go +++ b/common/buf/writer.go @@ -6,12 +6,6 @@ import ( "v2ray.com/core/common/errors" ) -var ( - _ io.ReaderFrom = (*BufferToBytesWriter)(nil) - _ io.Writer = (*BufferToBytesWriter)(nil) - _ Writer = (*BufferToBytesWriter)(nil) -) - // BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer. type BufferToBytesWriter struct { io.Writer @@ -39,13 +33,6 @@ func (w *BufferToBytesWriter) ReadFrom(reader io.Reader) (int64, error) { return sc.Size, err } -var ( - _ io.ReaderFrom = (*BufferedWriter)(nil) - _ io.Writer = (*BufferedWriter)(nil) - _ Writer = (*BufferedWriter)(nil) - _ io.ByteWriter = (*BufferedWriter)(nil) -) - // BufferedWriter is a Writer with internal buffer. type BufferedWriter struct { writer Writer @@ -173,7 +160,7 @@ func (w *seqWriter) WriteMultiBuffer(mb MultiBuffer) error { return nil } -type noOpWriter struct{} +type noOpWriter byte func (noOpWriter) WriteMultiBuffer(b MultiBuffer) error { b.Release() @@ -203,8 +190,8 @@ func (noOpWriter) ReadFrom(reader io.Reader) (int64, error) { var ( // Discard is a Writer that swallows all contents written in. - Discard Writer = noOpWriter{} + Discard Writer = noOpWriter(0) // DiscardBytes is an io.Writer that swallows all contents written in. - DiscardBytes io.Writer = noOpWriter{} + DiscardBytes io.Writer = noOpWriter(0) ) diff --git a/common/buf/writer_test.go b/common/buf/writer_test.go index 0e6b15db8..1dbe2f224 100644 --- a/common/buf/writer_test.go +++ b/common/buf/writer_test.go @@ -73,3 +73,16 @@ func TestDiscardBytesMultiBuffer(t *testing.T) { assert(nBytes, Equals, int64(size)) assert(err, IsNil) } + +func TestWriterInterface(t *testing.T) { + assert := With(t) + + assert((*BufferToBytesWriter)(nil), Implements, (*Writer)(nil)) + assert((*BufferToBytesWriter)(nil), Implements, (*io.Writer)(nil)) + assert((*BufferToBytesWriter)(nil), Implements, (*io.ReaderFrom)(nil)) + + assert((*BufferedWriter)(nil), Implements, (*Writer)(nil)) + assert((*BufferedWriter)(nil), Implements, (*io.Writer)(nil)) + assert((*BufferedWriter)(nil), Implements, (*io.ReaderFrom)(nil)) + assert((*BufferedWriter)(nil), Implements, (*io.ByteWriter)(nil)) +} diff --git a/transport/internet/kcp/connection_test.go b/transport/internet/kcp/connection_test.go index 85fc6bc29..89a224ca0 100644 --- a/transport/internet/kcp/connection_test.go +++ b/transport/internet/kcp/connection_test.go @@ -38,4 +38,5 @@ func TestConnectionInterface(t *testing.T) { assert((*Connection)(nil), Implements, (*io.Writer)(nil)) assert((*Connection)(nil), Implements, (*io.Reader)(nil)) assert((*Connection)(nil), Implements, (*buf.Reader)(nil)) + assert((*Connection)(nil), Implements, (*buf.Writer)(nil)) }