From 4a3ebd28eac95d39a299836b5339204cf1cf6513 Mon Sep 17 00:00:00 2001 From: Gabriel Vasile Date: Mon, 15 Nov 2021 16:29:49 +0200 Subject: [PATCH] Add test for dropLastLine --- internal/magic/magic_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/internal/magic/magic_test.go b/internal/magic/magic_test.go index f89aff84..8f59860d 100644 --- a/internal/magic/magic_test.go +++ b/internal/magic/magic_test.go @@ -18,3 +18,31 @@ func TestMagic(t *testing.T) { } } } + +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) { + for i, tt := range dropTests { + gotR := dropLastLine([]byte(tt.raw), tt.cutAt) + got, _ := io.ReadAll(gotR) + if got := string(got); got != tt.res { + t.Errorf("dropLastLine %d error: expected %q; got %q", i, tt.res, got) + } + } +}