@@ -11,6 +11,7 @@ import (
1111 "os"
1212 "reflect"
1313 "regexp"
14+ "strconv"
1415 "strings"
1516 "testing"
1617 "time"
@@ -262,7 +263,7 @@ var parsedDurationTests = []struct {
262263
263264// ----------------------------------------------------------------------------
264265
265- var floatTests = []struct {
266+ var float64Tests = []struct {
266267 input , key string
267268 def , value float64
268269}{
@@ -274,6 +275,8 @@ var floatTests = []struct {
274275 {"key = 0" , "key" , 999 , 0 },
275276 {"key = -1" , "key" , 999 , - 1 },
276277 {"key = 0123" , "key" , 999 , 123 },
278+ {"key = " + strconv .FormatFloat (math .SmallestNonzeroFloat64 , 'f' , - 1 , 64 ), "key" , 999 , math .SmallestNonzeroFloat64 },
279+ {"key = " + strconv .FormatFloat (math .MaxFloat64 , 'f' , - 1 , 64 ), "key" , 999 , math .MaxFloat64 },
277280
278281 // invalid values
279282 {"key = 0xff" , "key" , 999 , 999 },
@@ -285,6 +288,32 @@ var floatTests = []struct {
285288
286289// ----------------------------------------------------------------------------
287290
291+ var float32Tests = []struct {
292+ input , key string
293+ def , value float32
294+ }{
295+ // valid values
296+ {"key = 1.0" , "key" , 999 , 1.0 },
297+ {"key = 0.0" , "key" , 999 , 0.0 },
298+ {"key = -1.0" , "key" , 999 , - 1.0 },
299+ {"key = 1" , "key" , 999 , 1 },
300+ {"key = 0" , "key" , 999 , 0 },
301+ {"key = -1" , "key" , 999 , - 1 },
302+ {"key = 0123" , "key" , 999 , 123 },
303+ {"key = " + strconv .FormatFloat (math .SmallestNonzeroFloat32 , 'f' , - 1 , 32 ), "key" , 999 , math .SmallestNonzeroFloat32 },
304+ {"key = " + strconv .FormatFloat (math .MaxFloat32 , 'f' , - 1 , 32 ), "key" , 999 , math .MaxFloat32 },
305+
306+ // invalid values
307+ {"key = 0xff" , "key" , 999 , 999 },
308+ {"key = a" , "key" , 999 , 999 },
309+ {"key = " + strconv .FormatFloat (math .MaxFloat32 * 10 , 'f' , - 1 , 64 ), "key" , 999 , 999 },
310+
311+ // non existent key
312+ {"key = 1" , "key2" , 999 , 999 },
313+ }
314+
315+ // ----------------------------------------------------------------------------
316+
288317var int64Tests = []struct {
289318 input , key string
290319 def , value int64
@@ -294,6 +323,8 @@ var int64Tests = []struct {
294323 {"key = 0" , "key" , 999 , 0 },
295324 {"key = -1" , "key" , 999 , - 1 },
296325 {"key = 0123" , "key" , 999 , 123 },
326+ {"key = " + strconv .FormatInt (math .MinInt64 , 10 ), "key" , 999 , math .MinInt64 },
327+ {"key = " + strconv .FormatInt (math .MaxInt64 , 10 ), "key" , 999 , math .MaxInt64 },
297328
298329 // invalid values
299330 {"key = 0xff" , "key" , 999 , 999 },
@@ -306,6 +337,31 @@ var int64Tests = []struct {
306337
307338// ----------------------------------------------------------------------------
308339
340+ var int32Tests = []struct {
341+ input , key string
342+ def , value int32
343+ }{
344+ // valid values
345+ {"key = 1" , "key" , 999 , 1 },
346+ {"key = 0" , "key" , 999 , 0 },
347+ {"key = -1" , "key" , 999 , - 1 },
348+ {"key = 0123" , "key" , 999 , 123 },
349+ {"key = " + strconv .FormatInt (math .MinInt32 , 10 ), "key" , 999 , math .MinInt32 },
350+ {"key = " + strconv .FormatInt (math .MaxInt32 , 10 ), "key" , 999 , math .MaxInt32 },
351+
352+ // invalid values
353+ {"key = 0xff" , "key" , 999 , 999 },
354+ {"key = 1.0" , "key" , 999 , 999 },
355+ {"key = a" , "key" , 999 , 999 },
356+ {"key = " + strconv .FormatInt (math .MinInt32 - 1 , 10 ), "key" , 999 , 999 },
357+ {"key = " + strconv .FormatInt (math .MaxInt32 + 1 , 10 ), "key" , 999 , 999 },
358+
359+ // non existent key
360+ {"key = 1" , "key2" , 999 , 999 },
361+ }
362+
363+ // ----------------------------------------------------------------------------
364+
309365var uint64Tests = []struct {
310366 input , key string
311367 def , value uint64
@@ -314,12 +370,36 @@ var uint64Tests = []struct {
314370 {"key = 1" , "key" , 999 , 1 },
315371 {"key = 0" , "key" , 999 , 0 },
316372 {"key = 0123" , "key" , 999 , 123 },
373+ {"key = " + strconv .FormatUint (math .MaxUint64 , 10 ), "key" , 999 , math .MaxUint64 },
374+
375+ // invalid values
376+ {"key = -1" , "key" , 999 , 999 },
377+ {"key = 0xff" , "key" , 999 , 999 },
378+ {"key = 1.0" , "key" , 999 , 999 },
379+ {"key = a" , "key" , 999 , 999 },
380+
381+ // non existent key
382+ {"key = 1" , "key2" , 999 , 999 },
383+ }
384+
385+ // ----------------------------------------------------------------------------
386+
387+ var uint32Tests = []struct {
388+ input , key string
389+ def , value uint32
390+ }{
391+ // valid values
392+ {"key = 1" , "key" , 999 , 1 },
393+ {"key = 0" , "key" , 999 , 0 },
394+ {"key = 0123" , "key" , 999 , 123 },
395+ {"key = " + strconv .FormatUint (math .MaxUint32 , 10 ), "key" , 999 , math .MaxUint32 },
317396
318397 // invalid values
319398 {"key = -1" , "key" , 999 , 999 },
320399 {"key = 0xff" , "key" , 999 , 999 },
321400 {"key = 1.0" , "key" , 999 , 999 },
322401 {"key = a" , "key" , 999 , 999 },
402+ {"key = " + strconv .FormatUint (math .MaxUint32 + 1 , 10 ), "key" , 999 , 999 },
323403
324404 // non existent key
325405 {"key = 1" , "key2" , 999 , 999 },
@@ -555,7 +635,7 @@ func TestGetParsedDuration(t *testing.T) {
555635}
556636
557637func TestGetFloat64 (t * testing.T ) {
558- for _ , test := range floatTests {
638+ for _ , test := range float64Tests {
559639 p := mustParse (t , test .input )
560640 assert .Equal (t , p .Len (), 1 )
561641 assert .Equal (t , p .GetFloat64 (test .key , test .def ), test .value )
@@ -570,6 +650,22 @@ func TestMustGetFloat64(t *testing.T) {
570650 assert .Panic (t , func () { p .MustGetFloat64 ("invalid" ) }, "unknown property: invalid" )
571651}
572652
653+ func TestGetFloat32 (t * testing.T ) {
654+ for _ , test := range float32Tests {
655+ p := mustParse (t , test .input )
656+ assert .Equal (t , p .Len (), 1 )
657+ assert .Equal (t , p .GetFloat32 (test .key , test .def ), test .value )
658+ }
659+ }
660+
661+ func TestMustGetFloat32 (t * testing.T ) {
662+ input := "key = 123\n key2 = ghi"
663+ p := mustParse (t , input )
664+ assert .Equal (t , p .MustGetFloat32 ("key" ), float32 (123 ))
665+ assert .Panic (t , func () { p .MustGetFloat32 ("key2" ) }, "strconv.ParseFloat: parsing.*" )
666+ assert .Panic (t , func () { p .MustGetFloat32 ("invalid" ) }, "unknown property: invalid" )
667+ }
668+
573669func TestGetInt (t * testing.T ) {
574670 for _ , test := range int64Tests {
575671 p := mustParse (t , test .input )
@@ -602,6 +698,22 @@ func TestMustGetInt64(t *testing.T) {
602698 assert .Panic (t , func () { p .MustGetInt64 ("invalid" ) }, "unknown property: invalid" )
603699}
604700
701+ func TestGetInt32 (t * testing.T ) {
702+ for _ , test := range int32Tests {
703+ p := mustParse (t , test .input )
704+ assert .Equal (t , p .Len (), 1 )
705+ assert .Equal (t , p .GetInt32 (test .key , test .def ), test .value )
706+ }
707+ }
708+
709+ func TestMustGetInt32 (t * testing.T ) {
710+ input := "key = 123\n key2 = ghi"
711+ p := mustParse (t , input )
712+ assert .Equal (t , p .MustGetInt32 ("key" ), int32 (123 ))
713+ assert .Panic (t , func () { p .MustGetInt32 ("key2" ) }, "strconv.ParseInt: parsing.*" )
714+ assert .Panic (t , func () { p .MustGetInt32 ("invalid" ) }, "unknown property: invalid" )
715+ }
716+
605717func TestGetUint (t * testing.T ) {
606718 for _ , test := range uint64Tests {
607719 p := mustParse (t , test .input )
@@ -634,6 +746,22 @@ func TestMustGetUint64(t *testing.T) {
634746 assert .Panic (t , func () { p .MustGetUint64 ("invalid" ) }, "unknown property: invalid" )
635747}
636748
749+ func TestGetUint32 (t * testing.T ) {
750+ for _ , test := range uint32Tests {
751+ p := mustParse (t , test .input )
752+ assert .Equal (t , p .Len (), 1 )
753+ assert .Equal (t , p .GetUint32 (test .key , test .def ), test .value )
754+ }
755+ }
756+
757+ func TestMustGetUint32 (t * testing.T ) {
758+ input := "key = 123\n key2 = ghi"
759+ p := mustParse (t , input )
760+ assert .Equal (t , p .MustGetUint32 ("key" ), uint32 (123 ))
761+ assert .Panic (t , func () { p .MustGetUint32 ("key2" ) }, "strconv.ParseUint: parsing.*" )
762+ assert .Panic (t , func () { p .MustGetUint32 ("invalid" ) }, "unknown property: invalid" )
763+ }
764+
637765func TestGetString (t * testing.T ) {
638766 for _ , test := range stringTests {
639767 p := mustParse (t , test .input )
0 commit comments