improve utils tests to 100% coverage

This commit is contained in:
Lucas Clemente
2016-05-28 08:12:04 +02:00
parent a4773eb5ff
commit 576253ba71
5 changed files with 56 additions and 19 deletions

View File

@@ -2,6 +2,7 @@ package utils
import (
"bytes"
"io"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@@ -66,6 +67,11 @@ var _ = Describe("float16", func() {
}
})
It("errors on eof", func() {
_, err := ReadUfloat16(&bytes.Buffer{})
Expect(err).To(MatchError(io.EOF))
})
It("writes", func() {
testcases := []struct {
decoded uint64

View File

@@ -54,4 +54,10 @@ var _ = Describe("Log", func() {
Errorf("err")
Expect(b.Bytes()).To(Equal([]byte("debug\ninfo\nerr\n")))
})
It("says whether debug is enabled", func() {
Expect(Debug()).To(BeFalse())
SetLogLevel(LogLevelDebug)
Expect(Debug()).To(BeTrue())
})
})

View File

@@ -71,6 +71,13 @@ var _ = Describe("Min / Max", func() {
Expect(MinPacketNumber(1, 2)).To(Equal(protocol.PacketNumber(1)))
Expect(MinPacketNumber(2, 1)).To(Equal(protocol.PacketNumber(1)))
})
It("returns the minimum time", func() {
a := time.Now()
b := a.Add(time.Second)
Expect(MinTime(a, b)).To(Equal(a))
Expect(MinTime(b, a)).To(Equal(a))
})
})
It("returns the abs time", func() {

View File

@@ -167,13 +167,7 @@ func WriteUint16(b *bytes.Buffer, i uint16) {
func RandomBit() (bool, error) {
b := make([]byte, 1)
_, err := rand.Read(b)
if err != nil {
return false, err
}
if uint8(b[0])%2 == 0 {
return false, nil
}
return true, nil
return uint8(b[0])%2 == 0, err
}
// Uint32Slice attaches the methods of sort.Interface to []uint32, sorting in increasing order.

View File

@@ -2,6 +2,8 @@ package utils
import (
"bytes"
"io"
"sort"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@@ -17,9 +19,11 @@ var _ = Describe("Utils", func() {
})
It("throws an error if less than 2 bytes are passed", func() {
b := []byte{0x13}
_, err := ReadUint16(bytes.NewReader(b))
Expect(err).To(HaveOccurred())
b := []byte{0x13, 0xEF}
for i := 0; i < len(b); i++ {
_, err := ReadUint16(bytes.NewReader(b[:i]))
Expect(err).To(MatchError(io.EOF))
}
})
})
@@ -32,9 +36,11 @@ var _ = Describe("Utils", func() {
})
It("throws an error if less than 4 bytes are passed", func() {
b := []byte{0x13, 0x34, 0xEA}
_, err := ReadUint32(bytes.NewReader(b))
Expect(err).To(HaveOccurred())
b := []byte{0x12, 0x35, 0xAB, 0xFF}
for i := 0; i < len(b); i++ {
_, err := ReadUint32(bytes.NewReader(b[:i]))
Expect(err).To(MatchError(io.EOF))
}
})
})
@@ -47,9 +53,11 @@ var _ = Describe("Utils", func() {
})
It("throws an error if less than 8 bytes are passed", func() {
b := []byte{0x13, 0x34, 0xEA, 0x00, 0x14, 0xAA}
_, err := ReadUint64(bytes.NewReader(b))
Expect(err).To(HaveOccurred())
b := []byte{0x12, 0x35, 0xAB, 0xFF, 0xEF, 0xBE, 0xAD, 0xDE}
for i := 0; i < len(b); i++ {
_, err := ReadUint64(bytes.NewReader(b[:i]))
Expect(err).To(MatchError(io.EOF))
}
})
})
@@ -168,9 +176,19 @@ var _ = Describe("Utils", func() {
Context("Rand", func() {
It("returns either true or false", func() {
val, err := RandomBit()
Expect(err).ToNot(HaveOccurred())
Expect(val).To(SatisfyAny(Equal(true), Equal(false)))
countTrue := 0
countFalse := 0
for i := 0; i < 100; i++ {
val, err := RandomBit()
Expect(err).NotTo(HaveOccurred())
if val {
countTrue++
} else {
countFalse++
}
}
Expect(countTrue).ToNot(BeZero())
Expect(countFalse).ToNot(BeZero())
})
})
@@ -194,4 +212,10 @@ var _ = Describe("Utils", func() {
Expect(err).To(HaveOccurred())
})
})
It("sorts uint32 slices", func() {
s := Uint32Slice{1, 5, 2, 4, 3}
sort.Sort(s)
Expect(s).To(Equal(Uint32Slice{1, 2, 3, 4, 5}))
})
})