Skip to content

Commit e78ebff

Browse files
committed
Add IsCollection, byte[] operator, return "null" in tostring
1 parent a9431ac commit e78ebff

File tree

7 files changed

+88
-1
lines changed

7 files changed

+88
-1
lines changed

ValveKeyValue/ValveKeyValue.Test/ConversionCoverageTestCase.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,25 @@ public void KVObjectBlobFactory()
232232

233233
#endregion
234234

235+
#region Explicit byte[] operator
236+
237+
[Test]
238+
public void ExplicitByteArrayFromKVObject()
239+
{
240+
var obj = KVObject.Blob(new byte[] { 1, 2, 3 });
241+
byte[] result = (byte[])obj;
242+
Assert.That(result, Is.EqualTo(new byte[] { 1, 2, 3 }));
243+
}
244+
245+
[Test]
246+
public void ExplicitByteArrayFromNonBlobThrows()
247+
{
248+
var obj = new KVObject(42);
249+
Assert.That(() => (byte[])obj, Throws.InstanceOf<InvalidOperationException>());
250+
}
251+
252+
#endregion
253+
235254
#region KVObject IConvertible (Convert.ChangeType works directly)
236255

237256
[Test]

ValveKeyValue/ValveKeyValue.Test/KVObjectApiTestCase.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ public void ToStringReturnsCollectionForCollection()
205205
Assert.That(obj.ToString(CultureInfo.InvariantCulture), Is.EqualTo("[Collection]"));
206206
}
207207

208+
[Test]
209+
public void ToStringReturnsNullStringForNullValue()
210+
{
211+
var obj = KVObject.Null();
212+
Assert.That(obj.ToString(CultureInfo.InvariantCulture), Is.EqualTo("null"));
213+
#pragma warning disable CA1305
214+
Assert.That(obj.ToString(), Is.EqualTo("null"));
215+
#pragma warning restore CA1305
216+
}
217+
208218
#endregion
209219

210220
#region Explicit operators to primitives
@@ -281,6 +291,34 @@ public void NullKVObjectIsNull()
281291
Assert.That(value.IsNull, Is.True);
282292
}
283293

294+
[Test]
295+
public void IsCollectionReturnsTrueForDictCollection()
296+
{
297+
var obj = KVObject.Collection();
298+
Assert.That(obj.IsCollection, Is.True);
299+
}
300+
301+
[Test]
302+
public void IsCollectionReturnsTrueForListCollection()
303+
{
304+
var obj = KVObject.ListCollection();
305+
Assert.That(obj.IsCollection, Is.True);
306+
}
307+
308+
[Test]
309+
public void IsCollectionReturnsFalseForScalar()
310+
{
311+
var obj = new KVObject(42);
312+
Assert.That(obj.IsCollection, Is.False);
313+
}
314+
315+
[Test]
316+
public void IsCollectionReturnsFalseForArray()
317+
{
318+
var obj = KVObject.Array();
319+
Assert.That(obj.IsCollection, Is.False);
320+
}
321+
284322
[Test]
285323
public void NullKVObjectValueTypeIsNull()
286324
{

ValveKeyValue/ValveKeyValue.Test/KVSerializerNullInputsTestCase.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ public void DeserializeWithNullStream(KVSerializationFormat format)
1313
.With.Property(nameof(ArgumentNullException.ParamName)).EqualTo("stream"));
1414
}
1515

16+
[TestCaseSource(nameof(Formats))]
17+
public void SerializeWithNullStream(KVSerializationFormat format)
18+
{
19+
Assert.That(
20+
() => KVSerializer.Create(format).Serialize(stream: null!, data: new KVObject()),
21+
Throws.Exception.TypeOf<ArgumentNullException>()
22+
.With.Property(nameof(ArgumentNullException.ParamName)).EqualTo("stream"));
23+
}
24+
25+
[TestCaseSource(nameof(Formats))]
26+
public void SerializeDocumentWithNullStream(KVSerializationFormat format)
27+
{
28+
var doc = new KVDocument(new KVHeader(), "test", new KVObject());
29+
Assert.That(
30+
() => KVSerializer.Create(format).Serialize(stream: null!, data: doc),
31+
Throws.Exception.TypeOf<ArgumentNullException>()
32+
.With.Property(nameof(ArgumentNullException.ParamName)).EqualTo("stream"));
33+
}
34+
1635
public static IEnumerable Formats => Enum.GetValues<KVSerializationFormat>();
1736
}
1837
}

ValveKeyValue/ValveKeyValue.Test/Test Data/apisurface.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class ValveKeyValue.KVDocument
4848
public ValveKeyValue.KVFlag get_Flag();
4949
public ValveKeyValue.KVHeader get_Header();
5050
public bool get_IsArray();
51+
public bool get_IsCollection();
5152
public bool get_IsNull();
5253
public ValveKeyValue.KVObject get_Item(int index);
5354
public ValveKeyValue.KVObject get_Item(string key);
@@ -182,6 +183,7 @@ public class ValveKeyValue.KVObject
182183
public int get_Count();
183184
public ValveKeyValue.KVFlag get_Flag();
184185
public bool get_IsArray();
186+
public bool get_IsCollection();
185187
public bool get_IsNull();
186188
public ValveKeyValue.KVObject get_Item(int index);
187189
public ValveKeyValue.KVObject get_Item(string key);
@@ -211,6 +213,7 @@ public class ValveKeyValue.KVObject
211213
public static sbyte op_Explicit(ValveKeyValue.KVObject obj);
212214
public static decimal op_Explicit(ValveKeyValue.KVObject obj);
213215
public static IntPtr op_Explicit(ValveKeyValue.KVObject obj);
216+
public static byte[] op_Explicit(ValveKeyValue.KVObject obj);
214217
public static ValveKeyValue.KVObject op_Implicit(bool value);
215218
public static ValveKeyValue.KVObject op_Implicit(byte value);
216219
public static ValveKeyValue.KVObject op_Implicit(byte[] value);

ValveKeyValue/ValveKeyValue/KVObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public partial class KVObject : IReadOnlyDictionary<string, KVObject>
4343
/// </summary>
4444
public bool IsArray => ValueType == KVValueType.Array;
4545

46+
/// <summary>
47+
/// Gets a value indicating whether this value is a collection.
48+
/// </summary>
49+
public bool IsCollection => ValueType == KVValueType.Collection;
50+
4651
/// <summary>
4752
/// Gets the number of children in this object's collection or array value.
4853
/// Returns 0 if the value is neither a collection nor an array.

ValveKeyValue/ValveKeyValue/KVObject_IConvertible.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ KVValueType.Collection or KVValueType.Array or KVValueType.BinaryBlob
192192
{
193193
KVValueType.String => (string)_ref!,
194194
KVValueType.Boolean => _scalar != 0 ? "1" : "0",
195-
KVValueType.Null => throw new NotSupportedException("Cannot convert null to String."),
195+
KVValueType.Null => "null",
196196
KVValueType.FloatingPoint => BitConverter.Int32BitsToSingle((int)_scalar).ToString(provider),
197197
KVValueType.FloatingPoint64 => BitConverter.Int64BitsToDouble(_scalar).ToString(provider),
198198
KVValueType.Int32 or KVValueType.Pointer => ((int)_scalar).ToString(provider),

ValveKeyValue/ValveKeyValue/KVObject_operators.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public partial class KVObject
9595
/// <summary>Explicit cast from <see cref="KVObject"/> to <see cref="IntPtr"/>.</summary>
9696
public static explicit operator IntPtr(KVObject obj) => new(obj.ToInt32(null));
9797

98+
/// <summary>Explicit cast from <see cref="KVObject"/> to <see langword="byte[]"/>.</summary>
99+
public static explicit operator byte[](KVObject obj) => obj.AsBlob();
100+
98101
#endregion
99102
}
100103
}

0 commit comments

Comments
 (0)