Skip to content

Commit 48b90b4

Browse files
IgorFedchenkoAaronontheweb
authored andcommitted
Wrap all Config/HoconRoot getters with HoconValueException (#148)
* Added getters with default values, fixed existing getters and added test * Fixed outdated tests
1 parent e832d51 commit 48b90b4

9 files changed

+357
-127
lines changed

src/Hocon.Configuration/Config.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Hocon
1717
/// the internal representation of a HOCON (Human-Optimized Config Object Notation)
1818
/// configuration string.
1919
/// </summary>
20-
public class Config:HoconRoot
20+
public class Config: HoconRoot
2121
{
2222
/// <summary>
2323
/// The configuration used as a secondary source.
@@ -65,7 +65,7 @@ protected Config Copy()
6565
};
6666
}
6767

68-
protected override HoconValue GetNode(HoconPath path)
68+
protected override HoconValue GetNode(HoconPath path, bool throwIfNotFound = false)
6969
{
7070
HoconValue result;
7171
try
@@ -74,6 +74,9 @@ protected override HoconValue GetNode(HoconPath path)
7474
}
7575
catch
7676
{
77+
if (throwIfNotFound)
78+
throw;
79+
7780
result = Fallback?.GetNode(path);
7881
}
7982

src/Hocon.Tests/HoconTests.cs

+62-10
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,11 @@ public void GettingStringFromArrayReturnsNull()
109109
Assert.Null(Parser.Parse(hocon).GetString("array"));
110110
}
111111

112-
// TODO: This behavior is incorrect, anything to array should throw instead.
113-
// see https://github.com/lightbend/config/blob/master/HOCON.md#automatic-type-conversions
114-
// see https://github.com/lightbend/config/blob/v1.3.3/config/src/main/java/com/typesafe/config/Config.java#L936
115-
// OLD BEHAVIOR:(not sure if this is the expected behavior but it is what we have established in Akka.NET)
116112
[Fact]
117113
public void GettingArrayFromLiteralsReturnsNull()
118114
{
119115
var hocon = " literal : a b c";
120-
var res = Parser.Parse(hocon).GetStringList("literal");
121-
122-
Assert.Null(res);
116+
Parser.Parse(hocon).Invoking(c => c.GetStringList("literal")).Should().Throw<HoconException>("Anything converted to array should throw instead");
123117
}
124118

125119
//Added tests to conform to the HOCON spec https://github.com/typesafehub/config/blob/master/HOCON.md
@@ -206,6 +200,27 @@ public void CanMergeObject()
206200
Assert.Equal("2", config.GetString("a.b.c.y"));
207201
Assert.Equal("3", config.GetString("a.b.c.z"));
208202
}
203+
204+
[Fact]
205+
public void Getter_failures_Should_include_bad_path()
206+
{
207+
var badConfig = Parser.Parse("{a.c: abc}");
208+
var badPath = "a.c";
209+
210+
badConfig.Invoking(c => c.GetInt(badPath, 0)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
211+
badConfig.Invoking(c => c.GetDouble(badPath, 0)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
212+
badConfig.Invoking(c => c.GetBooleanList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
213+
badConfig.Invoking(c => c.GetByteList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
214+
badConfig.Invoking(c => c.GetDecimalList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
215+
badConfig.Invoking(c => c.GetDoubleList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
216+
badConfig.Invoking(c => c.GetFloatList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
217+
badConfig.Invoking(c => c.GetIntList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
218+
badConfig.Invoking(c => c.GetLongList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
219+
badConfig.Invoking(c => c.GetObjectList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
220+
badConfig.Invoking(c => c.GetStringList(badPath)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
221+
badConfig.Invoking(c => c.GetInt(badPath, 0)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
222+
badConfig.Invoking(c => c.GetInt(badPath, 0)).Should().Throw<HoconValueException>().Which.FailPath.Should().Be(badPath);
223+
}
209224

210225
[Fact]
211226
public void CanOverrideObject()
@@ -394,6 +409,43 @@ public void CanAssignDoubleToField()
394409
Assert.Equal(1.1, Parser.Parse(hocon).GetDouble("a"));
395410
}
396411

412+
[Fact]
413+
public void CanSetDefaultValuesWhenGettingData()
414+
{
415+
var emptyConfig = Parser.Parse("{}");
416+
var missingKey = "a";
417+
418+
emptyConfig.GetInt(missingKey, 0).Should().Be(0);
419+
emptyConfig.GetDouble(missingKey, 0).Should().Be(0);
420+
421+
emptyConfig.GetBooleanList(missingKey, new List<bool>()).Should().Equal(new List<bool>());
422+
emptyConfig.Invoking(c => c.GetBooleanList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();
423+
424+
emptyConfig.GetByteList(missingKey, new List<byte>()).Should().Equal(new List<byte>());
425+
emptyConfig.Invoking(c => c.GetByteList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();;
426+
427+
emptyConfig.GetDecimalList(missingKey, new List<decimal>()).Should().Equal(new List<decimal>());
428+
emptyConfig.Invoking(c => c.GetDecimalList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();;
429+
430+
emptyConfig.GetDoubleList(missingKey, new List<double>()).Should().Equal(new List<double>());
431+
emptyConfig.Invoking(c => c.GetDoubleList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();;
432+
433+
emptyConfig.GetFloatList(missingKey, new List<float>()).Should().Equal(new List<float>());
434+
emptyConfig.Invoking(c => c.GetFloatList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();;
435+
436+
emptyConfig.GetIntList(missingKey, new List<int>()).Should().Equal(new List<int>());
437+
emptyConfig.Invoking(c => c.GetIntList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();;
438+
439+
emptyConfig.GetLongList(missingKey, new List<long>()).Should().Equal(new List<long>());
440+
emptyConfig.Invoking(c => c.GetLongList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();;
441+
442+
emptyConfig.GetObjectList(missingKey, new List<HoconObject>()).Should().Equal(new List<HoconObject>());
443+
emptyConfig.Invoking(c => c.GetObjectList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();;
444+
445+
emptyConfig.GetStringList(missingKey, new List<string>()).Should().Equal(new List<string>());
446+
emptyConfig.Invoking(c => c.GetStringList(missingKey)).Should().Throw<HoconValueException>().Which.InnerException.Should().BeOfType<HoconParserException>();;
447+
}
448+
397449
[Fact]
398450
public void CanAssignNumbersToField()
399451
{
@@ -421,9 +473,9 @@ public void CanAssignNumbersToField()
421473
Assert.Equal(float.NaN, config.GetFloat("e"));
422474

423475
Assert.Equal(1000.05m, config.GetDecimal("a"));
424-
Assert.Throws<HoconException>(() => config.GetDecimal("b"));
425-
Assert.Throws<HoconException>(() => config.GetDecimal("c"));
426-
Assert.Throws<HoconException>(() => config.GetDecimal("d"));
476+
Assert.Throws<HoconValueException>(() => config.GetDecimal("b")).GetBaseException().Should().BeOfType<HoconException>();
477+
Assert.Throws<HoconValueException>(() => config.GetDecimal("c")).GetBaseException().Should().BeOfType<HoconException>();
478+
Assert.Throws<HoconValueException>(() => config.GetDecimal("d")).GetBaseException().Should().BeOfType<HoconException>();
427479

428480
Assert.Equal(255, config.GetLong("f"));
429481
Assert.Equal(255, config.GetLong("g"));

src/Hocon.Tests/NumericallyIndexedObjectToArray.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using FluentAssertions;
34
using Xunit;
45

56
namespace Hocon.Tests
@@ -158,15 +159,15 @@ public void WithMixedFieldValuesShouldThrow()
158159
}";
159160
var config = Parser.Parse(hocon);
160161

161-
Assert.Throws<HoconException>(() =>
162+
Assert.Throws<HoconValueException>(() =>
162163
{
163164
config.GetObjectList("a");
164165
});
165-
Assert.Throws<HoconException>(() =>
166+
Assert.Throws<HoconValueException>(() =>
166167
{
167168
config.GetStringList("a");
168169
});
169-
Assert.Throws<HoconException>(() =>
170+
Assert.Throws<HoconValueException>(() =>
170171
{
171172
config.GetIntList("a");
172173
});
@@ -182,10 +183,12 @@ public void WithMixedFieldValuesShouldThrow()
182183
public void WithEmptyOrNonPositiveIndicesShouldThrow(string hocon)
183184
{
184185
var config = Parser.Parse(hocon);
185-
Assert.Throws<HoconException>(() =>
186+
var ex = Assert.Throws<HoconValueException>(() =>
186187
{
187188
config.GetObjectList("a");
188189
});
190+
ex.GetBaseException().Should().BeOfType<HoconException>();
191+
ex.FailPath.Should().Be("a");
189192
}
190193

191194
/*

0 commit comments

Comments
 (0)