| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package chunkstream |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "strings" |
| 10 "testing" |
| 11 |
| 12 . "github.com/smartystreets/goconvey/convey" |
| 13 ) |
| 14 |
| 15 type testChunk struct { |
| 16 data []byte |
| 17 released bool |
| 18 } |
| 19 |
| 20 var _ Chunk = (*testChunk)(nil) |
| 21 |
| 22 func tc(d ...byte) *testChunk { |
| 23 return &testChunk{ |
| 24 data: d, |
| 25 } |
| 26 } |
| 27 |
| 28 func (c *testChunk) String() string { |
| 29 pieces := make([]string, len(c.data)) |
| 30 for i, d := range c.data { |
| 31 pieces[i] = fmt.Sprintf("0x%02x", d) |
| 32 } |
| 33 return fmt.Sprintf("{%s}", strings.Join(pieces, ", ")) |
| 34 } |
| 35 |
| 36 func (c *testChunk) Bytes() []byte { |
| 37 return c.data |
| 38 } |
| 39 |
| 40 func (c *testChunk) Len() int { |
| 41 return len(c.data) |
| 42 } |
| 43 |
| 44 func (c *testChunk) Release() { |
| 45 if c.released { |
| 46 panic("double-free") |
| 47 } |
| 48 c.released = true |
| 49 } |
| 50 |
| 51 func TestChunkNode(t *testing.T) { |
| 52 Convey(`A chunkNode wrapping a testing Chunk implementation`, t, func()
{ |
| 53 c := tc(0, 1, 2) |
| 54 n := newChunkNode(c) |
| 55 |
| 56 Convey(`Should call Chunk methods.`, func() { |
| 57 So(n.Bytes(), ShouldResemble, []byte{0, 1, 2}) |
| 58 So(n.Len(), ShouldEqual, 3) |
| 59 }) |
| 60 |
| 61 Convey(`When released, releases the wrapped Chunk.`, func() { |
| 62 n.release() |
| 63 So(c.released, ShouldBeTrue) |
| 64 |
| 65 Convey(`If released again, panics.`, func() { |
| 66 So(func() { n.release() }, ShouldPanic) |
| 67 }) |
| 68 }) |
| 69 }) |
| 70 } |
| OLD | NEW |