@@ -11,8 +11,10 @@ import (
1111 "testing"
1212 "time"
1313
14+ "github.com/docker/go-units"
1415 "github.com/go-playground/validator/v10"
1516 "github.com/stretchr/testify/assert"
17+ "github.com/stretchr/testify/require"
1618
1719 "github.com/hedhyw/json-log-viewer/internal/pkg/config"
1820 "github.com/hedhyw/json-log-viewer/internal/pkg/tests"
@@ -142,7 +144,7 @@ func ExampleGetDefaultConfig() {
142144 // "50": "error",
143145 // "60": "fatal"
144146 // },
145- // "maxFileSizeBytes": 1073741824
147+ // "maxFileSizeBytes": 2000000000
146148 // }
147149}
148150
@@ -299,3 +301,76 @@ func TestValidateField(t *testing.T) {
299301 })
300302 }
301303}
304+
305+ func TestByteSize (t * testing.T ) {
306+ t .Parallel ()
307+
308+ testCases := [... ]struct {
309+ Value string
310+ Expected config.ByteSize
311+ }{{
312+ Value : `"1k"` ,
313+ Expected : units .KB ,
314+ }, {
315+ Value : `"1m"` ,
316+ Expected : units .MB ,
317+ }, {
318+ Value : `"1.5m"` ,
319+ Expected : units .MB * 1.5 ,
320+ }, {
321+ Value : `"1g"` ,
322+ Expected : units .GB ,
323+ }, {
324+ Value : `"1t"` ,
325+ Expected : units .TB ,
326+ }, {
327+ Value : `"1p"` ,
328+ Expected : units .PB ,
329+ }, {
330+ Value : "1" ,
331+ Expected : 1 ,
332+ }, {
333+ Value : "12345" ,
334+ Expected : 12345 ,
335+ }}
336+
337+ for _ , testCase := range testCases {
338+ var actual config.ByteSize
339+
340+ err := json .Unmarshal ([]byte (testCase .Value ), & actual )
341+ if assert .NoError (t , err , testCase .Value ) {
342+ assert .Equal (t , testCase .Expected , actual , testCase .Value )
343+ }
344+ }
345+ }
346+
347+ func TestByteSizeParseFailed (t * testing.T ) {
348+ t .Parallel ()
349+
350+ t .Run ("invalid_number" , func (t * testing.T ) {
351+ t .Parallel ()
352+
353+ var value config.ByteSize
354+
355+ err := json .Unmarshal ([]byte (`"123.123.123"` ), & value )
356+ require .Error (t , err )
357+ })
358+
359+ t .Run ("invalid_suffix" , func (t * testing.T ) {
360+ t .Parallel ()
361+
362+ var value config.ByteSize
363+
364+ err := json .Unmarshal ([]byte (`"123X"` ), & value )
365+ require .Error (t , err )
366+ })
367+
368+ t .Run ("empty" , func (t * testing.T ) {
369+ t .Parallel ()
370+
371+ var value config.ByteSize
372+
373+ err := json .Unmarshal ([]byte (`""` ), & value )
374+ require .Error (t , err )
375+ })
376+ }
0 commit comments