From 3498329782a4f2e38aad805f99abc67b882c2656 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 2 Feb 2022 00:38:08 -0500 Subject: internal/feed: add TestParseMetaSize, newMeta passTests, and NewMeta failTests --- internal/feed/meta_test.go | 112 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 10 deletions(-) (limited to 'internal/feed/meta_test.go') diff --git a/internal/feed/meta_test.go b/internal/feed/meta_test.go index 04ad22b..640926c 100644 --- a/internal/feed/meta_test.go +++ b/internal/feed/meta_test.go @@ -3,9 +3,56 @@ package feed import ( "bytes" "encoding/json" + "fmt" "testing" ) +func TestParseMetaSize(t *testing.T) { + passTests := []struct { + val string + exp uint64 + } { + { "0", 0 }, + { "1024", 1024 }, + { "18446744073709551615", 18446744073709551615 }, + } + + for _, test := range(passTests) { + t.Run(test.val, func(t *testing.T) { + got, err := parseMetaSize("foo", test.val) + if err != nil { + t.Error(err) + return + } + + if got != test.exp { + t.Errorf("got %d, exp %d", got, test.exp) + return + } + }) + } + + failTests := []struct { + val string + exp string + } { + { "-1", "invalid foo: \"-1\"" }, + { "a", "invalid foo: \"a\"" }, + { "18446744073709551616", "invalid foo: \"18446744073709551616\"" }, + } + + for _, test := range(failTests) { + t.Run(test.val, func(t *testing.T) { + got, err := parseMetaSize("foo", test.val) + if err == nil { + t.Errorf("got %d, exp error", got) + } else if err.Error() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp) + } + }) + } +} + // test data const testMeta = `lastModifiedDate:2022-01-29T03:01:16-05:00 size:73202582 @@ -16,20 +63,65 @@ sha256:B86258D5D9861507A1894A7B92011764803D7267787B1487539E240EA2405440 // Test meta parser func TestNewMeta(t *testing.T) { - t.Run("NewMeta", func(t *testing.T) { - // create buffer - buf := bytes.NewBufferString(testMeta) + passTests := []string { + `lastModifiedDate:2022-01-29T03:01:16-05:00 +size:73202582 +zipSize:3753799 +gzSize:3753663 +sha256:B86258D5D9861507A1894A7B92011764803D7267787B1487539E240EA2405440 +`, + } - // decode meta, check for error - _, err := NewMeta(buf) - if err != nil { - t.Error(err) - } - }) + for i, val := range(passTests) { + // build test name + name := fmt.Sprintf("passTests[%d]", i) + + t.Run(name, func(t *testing.T) { + // create buffer + buf := bytes.NewBufferString(val) + + // decode meta, check for error + _, err := NewMeta(buf) + if err != nil { + t.Error(err) + } + }) + } + + failTests := []struct { + val string + exp string + } { + { "asdf", "bad meta line: \"asdf\"" }, + { "size:a", "invalid size: \"a\"" }, + { "zipSize:a", "invalid zipSize: \"a\"" }, + { "gzSize:a", "invalid gzSize: \"a\"" }, + { "sha256:a", "invalid sha256 hash length: 1" }, + { + val: "sha256:0z00000000000000000000000000000000000000000000000000000000000000", + exp: "invalid sha256 hash: encoding/hex: invalid byte: U+007A 'z'", + }, + { "foo:bar", "unknown key: \"foo\"" }, + } + + for _, test := range(failTests) { + t.Run(test.val, func(t *testing.T) { + // create buffer + buf := bytes.NewBufferString(test.val) + + // decode meta, check for error + got, err := NewMeta(buf) + if err == nil { + t.Errorf("got %v, exp error", got) + } else if err.Error() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp) + } + }) + } t.Run("JsonEncode", func(t *testing.T) { // create buffer - buf := bytes.NewBufferString(testMeta) + buf := bytes.NewBufferString(passTests[0]) // decode meta, check for error meta, err := NewMeta(buf) -- cgit v1.2.3