Skip to content

Commit 77b9e5b

Browse files
committed
Make Remove/Clear on non collections throw
1 parent 0a29586 commit 77b9e5b

2 files changed

Lines changed: 31 additions & 12 deletions

File tree

ValveKeyValue/ValveKeyValue.Test/EdgeCaseTestCase.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ public void AddChildToScalarThrowsInvalidOperationException()
120120
Throws.InstanceOf<InvalidOperationException>());
121121
}
122122

123+
[Test]
124+
public void RemoveFromScalarThrowsInvalidOperationException()
125+
{
126+
var obj = new KVObject("hello");
127+
128+
Assert.That(
129+
() => obj.Remove("child"),
130+
Throws.InstanceOf<InvalidOperationException>());
131+
}
132+
133+
[Test]
134+
public void ClearOnScalarThrowsInvalidOperationException()
135+
{
136+
var obj = new KVObject("hello");
137+
138+
Assert.That(
139+
() => obj.Clear(),
140+
Throws.InstanceOf<InvalidOperationException>());
141+
}
142+
123143
#endregion
124144

125145
#region Add(KVObject) to non-array throws
@@ -601,23 +621,23 @@ public void IndexerSetOnArrayThrowsInvalidOperationException()
601621

602622
#endregion
603623

604-
#region Remove on scalar and array returns false
624+
#region Remove on scalar and array throws
605625

606626
[Test]
607-
public void RemoveOnScalarReturnsFalse()
627+
public void RemoveOnScalarThrows()
608628
{
609629
var obj = new KVObject("hello");
610630

611-
Assert.That(obj.Remove("anything"), Is.False);
631+
Assert.That(() => obj.Remove("anything"), Throws.InstanceOf<InvalidOperationException>());
612632
}
613633

614634
[Test]
615-
public void RemoveOnArrayReturnsFalse()
635+
public void RemoveOnArrayThrows()
616636
{
617637
var arr = KVObject.Array();
618638
arr.Add(1);
619639

620-
Assert.That(arr.Remove("anything"), Is.False);
640+
Assert.That(() => arr.Remove("anything"), Throws.InstanceOf<InvalidOperationException>());
621641
}
622642

623643
#endregion
@@ -659,17 +679,14 @@ public void IndexerSetNullOnMissingKeyInDictCollectionStoresNullValue()
659679

660680
#endregion
661681

662-
#region Clear on scalar is no-op
682+
#region Clear on scalar throws
663683

664684
[Test]
665-
public void ClearOnScalarIsNoOp()
685+
public void ClearOnScalarThrows()
666686
{
667687
var obj = new KVObject(42);
668688

669-
obj.Clear();
670-
671-
Assert.That((int)obj, Is.EqualTo(42));
672-
Assert.That(obj.ValueType, Is.EqualTo(KVValueType.Int32));
689+
Assert.That(() => obj.Clear(), Throws.InstanceOf<InvalidOperationException>());
673690
}
674691

675692
#endregion

ValveKeyValue/ValveKeyValue/KVObject.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public bool Remove(string key)
342342
Dictionary<string, KVObject> dict => dict.Remove(key),
343343
// RemoveAll: removes all entries with this key, not just the first (list-backed collections allow duplicate keys)
344344
List<KeyValuePair<string, KVObject>> list when ValueType == KVValueType.Collection => list.RemoveAll(c => c.Key == key) > 0,
345-
_ => false,
345+
_ => throw new InvalidOperationException($"Cannot remove a named child from a {ValueType} value."),
346346
};
347347
}
348348

@@ -375,6 +375,8 @@ public void Clear()
375375
case List<KVObject> list when ValueType == KVValueType.Array:
376376
list.Clear();
377377
break;
378+
default:
379+
throw new InvalidOperationException($"Cannot clear a {ValueType} value.");
378380
}
379381
}
380382

0 commit comments

Comments
 (0)