Skip to content

Commit 27e7572

Browse files
committed
Make KVObject.ToString stringify just the value for backwards compatibility (although you should cast to string instead)
1 parent 7f93d35 commit 27e7572

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

ValveKeyValue/ValveKeyValue.Test/KVObjectApiTestCase.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,31 @@ public void ImplicitFloatToKVObject()
182182

183183
#endregion
184184

185+
#region ToString
186+
187+
[Test]
188+
public void ToStringReturnsValueForStringObject()
189+
{
190+
var obj = new KVObject("key", "hello");
191+
Assert.That(obj.ToString(), Is.EqualTo("hello"));
192+
}
193+
194+
[Test]
195+
public void ToStringReturnsValueForIntObject()
196+
{
197+
var obj = new KVObject("key", 42);
198+
Assert.That(obj.ToString(), Is.EqualTo("42"));
199+
}
200+
201+
[Test]
202+
public void ToStringReturnsCollectionForCollection()
203+
{
204+
var obj = new KVObject("root", [new KVObject("a", "b")]);
205+
Assert.That(obj.ToString(), Is.EqualTo("[Collection]"));
206+
}
207+
208+
#endregion
209+
185210
#region 3. Explicit operators to primitives
186211

187212
[Test]

ValveKeyValue/ValveKeyValue/KVObject.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,18 +532,18 @@ private static KVObject FindInList(List<KVObject> list, string name)
532532
}
533533

534534
/// <inheritdoc/>
535-
public override string ToString() => Name != null ? $"{Name}: {Value}" : Value.ToString(CultureInfo.InvariantCulture);
535+
public override string ToString() => Value.ToString(CultureInfo.InvariantCulture);
536536

537537
private string DebuggerDescription
538538
{
539539
get
540540
{
541541
if (Value.ValueType == KVValueType.String)
542542
{
543-
return $"{Name}: {Value}";
543+
return Name != null ? $"{Name}: {Value}" : (string)Value;
544544
}
545545

546-
return $"{Name}: {Value} ({Value.ValueType})";
546+
return Name != null ? $"{Name}: {Value} ({Value.ValueType})" : $"{Value} ({Value.ValueType})";
547547
}
548548
}
549549

0 commit comments

Comments
 (0)