Skip to content

Commit

Permalink
bytesize: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyfrosch committed May 14, 2020
1 parent 6cbae39 commit ef15663
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions convert/bytesize_test.go
@@ -0,0 +1,52 @@
package convert

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func ExampleParseBytes() {
b := ParseBytes("100MB")

fmt.Println(b)
fmt.Println(b.ToHumanReadable())
fmt.Println(b.ToKilobyte())
fmt.Println(b.ToGigabyte())
// Output: 100 MB
// 100 MB
// 100000
// 0.1
}

func TestParseBytes(t *testing.T) {
var b *Bytesize

b = ParseBytes(1)
assert.Equal(t, 1, b.Data)
assert.Equal(t, "B", b.Unit)

b = ParseBytes("1")
assert.Equal(t, 1, b.Data)
assert.Equal(t, "B", b.Unit)

b = ParseBytes("1mb")
assert.Equal(t, 1, b.Data)
assert.Equal(t, "MB", b.Unit)

t.Skip("missing error handling")
b = ParseBytes("foobar")
assert.Equal(t, 1, b.Data)
assert.Equal(t, "MB", b.Unit)
}

func TestBytesize_String(t *testing.T) {
assert.Equal(t, "100 MB", ParseBytes("100mb").String())
assert.Equal(t, "100 MB", fmt.Sprint(ParseBytes("100mb")))
}

func TestBytesize_ToHumanReadable(t *testing.T) {
assert.Equal(t, "900 MB", ParseBytes("900MB").ToHumanReadable())
assert.Equal(t, "1 GB", ParseBytes("1000MB").ToHumanReadable())
assert.Equal(t, "1.2 GB", ParseBytes("1200MB").ToHumanReadable())
}

0 comments on commit ef15663

Please sign in to comment.