Skip to content

Commit

Permalink
Merge 4592ce7 into bfdc13a
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Nov 15, 2021
2 parents bfdc13a + 4592ce7 commit fdb39fb
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions internal/magic/magic_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit fdb39fb

Please sign in to comment.