1 package magic
2
3 import (
4 "io"
5 "testing"
6 )
7
8 func TestMagic(t *testing.T) {
9 tCases := []struct {
10 name string
11 detector Detector
12 raw string
13 limit uint32
14 res bool
15 }{
16 {
17 name: "incomplete JSON, limit 0",
18 detector: JSON,
19 raw: `["an incomplete JSON array`,
20 limit: 0,
21 res: false,
22 },
23 {
24 name: "incomplete JSON, limit 10",
25 detector: JSON,
26 raw: `["an incomplete JSON array`,
27 limit: 10,
28 res: true,
29 },
30 {
31 name: "basic JSON data type null",
32 detector: JSON,
33 raw: `null`,
34 limit: 10,
35 res: false,
36 },
37 {
38 name: "basic JSON data type string",
39 detector: JSON,
40 raw: `"abc"`,
41 limit: 10,
42 res: false,
43 },
44 {
45 name: "basic JSON data type integer",
46 detector: JSON,
47 raw: `120`,
48 limit: 10,
49 res: false,
50 },
51 {
52 name: "basic JSON data type float",
53 detector: JSON,
54 raw: `.120`,
55 limit: 10,
56 res: false,
57 },
58 {
59 name: "NdJSON with basic data types",
60 detector: NdJSON,
61 raw: "1\nnull\n\"foo\"\n0.1",
62 limit: 10,
63 res: false,
64 },
65 {
66 name: "NdJSON with basic data types and empty object",
67 detector: NdJSON,
68 raw: "1\n2\n3\n{}",
69 limit: 10,
70 res: true,
71 },
72 {
73 name: "NdJSON with empty objects types",
74 detector: NdJSON,
75 raw: "{}\n{}\n{}",
76 limit: 10,
77 res: true,
78 },
79 }
80 for _, tt := range tCases {
81 t.Run(tt.name, func(t *testing.T) {
82 if got := tt.detector([]byte(tt.raw), tt.limit); got != tt.res {
83 t.Errorf("expected: %t; got: %t", tt.res, got)
84 }
85 })
86 }
87 }
88
89 func TestDropLastLine(t *testing.T) {
90 dropTests := []struct {
91 raw string
92 cutAt uint32
93 res string
94 }{
95 {"", 0, ""},
96 {"", 1, ""},
97 {"å", 2, "å"},
98 {"\n", 0, "\n"},
99 {"\n", 1, "\n"},
100 {"\n\n", 1, "\n"},
101 {"\n\n", 3, "\n\n"},
102 {"a\n\n", 3, "a\n"},
103 {"\na\n", 3, "\na"},
104 {"å\n\n", 5, "å\n\n"},
105 {"\nå\n", 5, "\nå\n"},
106 }
107 for i, tt := range dropTests {
108 gotR := dropLastLine([]byte(tt.raw), tt.cutAt)
109 got, _ := io.ReadAll(gotR)
110 if got := string(got); got != tt.res {
111 t.Errorf("dropLastLine %d error: expected %q; got %q", i, tt.res, got)
112 }
113 }
114 }
115
116 func BenchmarkSrt(b *testing.B) {
117 const subtitle = `1
118 00:02:16,612 --> 00:02:19,376
119 Senator, we're making
120 our final approach into Coruscant.
121
122 `
123 for i := 0; i < b.N; i++ {
124 Srt([]byte(subtitle), 0)
125 }
126 }
127
View as plain text