From 4592ce7ffb176632d52d41b9a7ae6a2814708a5f Mon Sep 17 00:00:00 2001 From: Gabriel Vasile Date: Mon, 15 Nov 2021 19:19:19 +0200 Subject: [PATCH] Use named subtests for magic tests --- internal/magic/magic_test.go | 76 +++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/internal/magic/magic_test.go b/internal/magic/magic_test.go index fa477ccd..6dd5c0bc 100644 --- a/internal/magic/magic_test.go +++ b/internal/magic/magic_test.go @@ -5,42 +5,56 @@ import ( "testing" ) -var magicTests = []struct { - raw string - limit uint32 - res bool - detector Detector -}{ - {`["an incomplete JSON array`, 0, false, JSON}, -} - func TestMagic(t *testing.T) { - for i, tt := range magicTests { - if got := tt.detector([]byte(tt.raw), tt.limit); got != tt.res { - t.Errorf("Detector %d error: expected: %t; got: %t", i, tt.res, got) - } + tCases := []struct { + name string + detector Detector + raw string + limit uint32 + res bool + }{ + { + name: "incomplete JSON, limit 0", + detector: JSON, + raw: `["an incomplete JSON array`, + limit: 0, + res: false, + }, + { + name: "incomplete JSON, limit 10", + detector: JSON, + raw: `["an incomplete JSON array`, + limit: 10, + res: true, + }, + } + for _, tt := range tCases { + t.Run(tt.name, func(t *testing.T) { + if got := tt.detector([]byte(tt.raw), tt.limit); got != tt.res { + t.Errorf("expected: %t; got: %t", tt.res, got) + } + }) } -} - -var dropTests = []struct { - raw string - cutAt uint32 - res string -}{ - {"", 0, ""}, - {"", 1, ""}, - {"å", 2, "å"}, - {"\n", 0, "\n"}, - {"\n", 1, "\n"}, - {"\n\n", 1, "\n"}, - {"\n\n", 3, "\n\n"}, - {"a\n\n", 3, "a\n"}, - {"\na\n", 3, "\na"}, - {"å\n\n", 5, "å\n\n"}, - {"\nå\n", 5, "\nå\n"}, } func TestDropLastLine(t *testing.T) { + dropTests := []struct { + raw string + cutAt uint32 + res string + }{ + {"", 0, ""}, + {"", 1, ""}, + {"å", 2, "å"}, + {"\n", 0, "\n"}, + {"\n", 1, "\n"}, + {"\n\n", 1, "\n"}, + {"\n\n", 3, "\n\n"}, + {"a\n\n", 3, "a\n"}, + {"\na\n", 3, "\na"}, + {"å\n\n", 5, "å\n\n"}, + {"\nå\n", 5, "\nå\n"}, + } for i, tt := range dropTests { gotR := dropLastLine([]byte(tt.raw), tt.cutAt) got, _ := io.ReadAll(gotR)