|
1 | 1 | package configparser_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "strings" |
| 5 | + |
4 | 6 | "github.com/bigkevmcd/go-configparser"
|
5 | 7 |
|
6 | 8 | gc "gopkg.in/check.v1"
|
@@ -368,3 +370,27 @@ func (s *ConfigParserSuite) TestOptionsWithSectionStripsWhitespaceFromKeys(c *gc
|
368 | 370 | c.Assert(err, gc.IsNil)
|
369 | 371 | c.Assert(result, gc.DeepEquals, []string{"base_dir", "bin_dir", "foo"})
|
370 | 372 | }
|
| 373 | + |
| 374 | +func brokenConv(_ string) (any, error) { return nil, nil } |
| 375 | + |
| 376 | +// Get* methods must fail with a proper error instead of a panic if type assertion has failed. |
| 377 | +func (s *ConfigParserSuite) TestGetWithAssertionError(c *gc.C) { |
| 378 | + parser, err := configparser.ParseReaderWithOptions( |
| 379 | + strings.NewReader("[testing]\nstring=new string\nint=200\nfloat=3.14159265\nbool=TRUE"), |
| 380 | + configparser.Converters(configparser.Converter{ |
| 381 | + configparser.StringConv: brokenConv, |
| 382 | + configparser.IntConv: brokenConv, |
| 383 | + configparser.FloatConv: brokenConv, |
| 384 | + configparser.BoolConv: brokenConv, |
| 385 | + }), |
| 386 | + ) |
| 387 | + c.Assert(err, gc.IsNil) |
| 388 | + _, err = parser.Get("testing", "string") |
| 389 | + c.Assert(err, gc.ErrorMatches, ".*assertion to string failed.*") |
| 390 | + _, err = parser.GetInt64("testing", "int") |
| 391 | + c.Assert(err, gc.ErrorMatches, ".*assertion to int64 failed.*") |
| 392 | + _, err = parser.GetFloat64("testing", "float") |
| 393 | + c.Assert(err, gc.ErrorMatches, ".*assertion to float64 failed.*") |
| 394 | + _, err = parser.GetBool("testing", "bool") |
| 395 | + c.Assert(err, gc.ErrorMatches, ".*assertion to bool failed.*") |
| 396 | +} |
0 commit comments