Skip to content

Commit 2335204

Browse files
authored
Merge pull request #122 from greymistcube/refactor/dictionary
🧹 Remove `GetValue<T>()` from `Dictionary`
2 parents 7422d73 + 4c6d55d commit 2335204

File tree

3 files changed

+14
-196
lines changed

3 files changed

+14
-196
lines changed

Bencodex.Tests/Types/DictionaryTest.cs

Lines changed: 10 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -234,64 +234,17 @@ public void SetItem()
234234
.SetItem((Text)"boolean", (Bencodex.Types.Boolean)true)
235235
.SetItem((Text)"list", new List(new IValue[] { (Text)"bar", (Integer)1337 }));
236236

237-
Assert.Equal("foo", (Text)dictionary["text"]);
238-
Assert.Equal("foo", dictionary.GetValue<Text>("text"));
239-
Assert.Throws<InvalidCastException>(
240-
() => dictionary.GetValue<Integer>("text"));
241-
Assert.Throws<InvalidCastException>(
242-
() => dictionary.GetValue<Binary>("text"));
243-
Assert.Throws<InvalidCastException>(
244-
() => dictionary.GetValue<Bencodex.Types.Boolean>("text"));
245-
237+
Assert.Equal((Text)"foo", (Text)dictionary["text"]);
246238
Assert.Equal((Integer)1337, (Integer)dictionary["integer"]);
247239
Assert.Equal(
248-
(Integer)1337,
249-
dictionary.GetValue<Integer>("integer"));
250-
Assert.Throws<InvalidCastException>(
251-
() => dictionary.GetValue<Binary>("integer"));
252-
Assert.Throws<InvalidCastException>(
253-
() => dictionary.GetValue<Text>("integer"));
254-
Assert.Throws<InvalidCastException>(
255-
() => dictionary.GetValue<Bencodex.Types.Boolean>("integer"));
256-
257-
Assert.Equal(
258-
new byte[] { 0x01, 0x02, 0x03, 0x04 },
240+
(Binary)new byte[] { 0x01, 0x02, 0x03, 0x04 },
259241
(Binary)dictionary["binary"]);
260-
Assert.Equal(
261-
new byte[] { 0x01, 0x02, 0x03, 0x04 },
262-
dictionary.GetValue<Binary>("binary"));
263-
Assert.Throws<InvalidCastException>(
264-
() => dictionary.GetValue<Integer>("binary"));
265-
Assert.Throws<InvalidCastException>(
266-
() => dictionary.GetValue<Text>("binary"));
267-
Assert.Throws<InvalidCastException>(
268-
() => dictionary.GetValue<Bencodex.Types.Boolean>("binary"));
269-
270242
Assert.Equal(
271243
(Bencodex.Types.Boolean)true,
272244
(Bencodex.Types.Boolean)dictionary["boolean"]);
273245
Assert.Equal(
274-
(Bencodex.Types.Boolean)true,
275-
dictionary.GetValue<Bencodex.Types.Boolean>("boolean"));
276-
Assert.Throws<InvalidCastException>(
277-
() => dictionary.GetValue<Integer>("boolean"));
278-
Assert.Throws<InvalidCastException>(
279-
() => dictionary.GetValue<Binary>("boolean"));
280-
Assert.Throws<InvalidCastException>(
281-
() => dictionary.GetValue<Text>("boolean"));
282-
283-
Assert.Equal(
284-
new IValue[] { (Text)"bar", (Integer)1337 },
285-
(Bencodex.Types.List)dictionary["list"]);
286-
Assert.Equal(
287-
new IValue[] { (Text)"bar", (Integer)1337 },
288-
dictionary.GetValue<Bencodex.Types.List>("list"));
289-
Assert.Throws<InvalidCastException>(
290-
() => dictionary.GetValue<Integer>("list"));
291-
Assert.Throws<InvalidCastException>(
292-
() => dictionary.GetValue<Binary>("list"));
293-
Assert.Throws<InvalidCastException>(
294-
() => dictionary.GetValue<Text>("list"));
246+
new List(new IValue[] { (Text)"bar", (Integer)1337 }),
247+
(List)dictionary["list"]);
295248
}
296249

297250
[Fact]
@@ -380,146 +333,24 @@ public void Add()
380333
.Add((Binary)bListKey, bList);
381334

382335
// String keys
383-
Assert.Equal(sText, (Text)dictionary[sTextKey]);
384-
Assert.Equal(sText, dictionary.GetValue<Text>(sTextKey));
385-
Assert.Throws<InvalidCastException>(
386-
() => dictionary.GetValue<Integer>(sTextKey));
387-
Assert.Throws<InvalidCastException>(
388-
() => dictionary.GetValue<Binary>(sTextKey));
389-
Assert.Throws<InvalidCastException>(
390-
() => dictionary.GetValue<Bencodex.Types.Boolean>(sTextKey));
391-
336+
Assert.Equal((Text)sText, (Text)dictionary[sTextKey]);
392337
Assert.Equal((Integer)sShort, (Integer)dictionary[sShortKey]);
393-
Assert.Equal(
394-
(Integer)sShort,
395-
dictionary.GetValue<Integer>(sShortKey));
396-
Assert.Throws<InvalidCastException>(
397-
() => dictionary.GetValue<Binary>(sShortKey));
398-
Assert.Throws<InvalidCastException>(
399-
() => dictionary.GetValue<Text>(sShortKey));
400-
Assert.Throws<InvalidCastException>(
401-
() => dictionary.GetValue<Bencodex.Types.Boolean>(sShortKey));
402-
403338
Assert.Equal((Integer)sInt, (Integer)dictionary[sIntKey]);
404-
Assert.Equal(
405-
(Integer)sInt,
406-
dictionary.GetValue<Integer>(sIntKey));
407-
Assert.Throws<InvalidCastException>(
408-
() => dictionary.GetValue<Binary>(sIntKey));
409-
Assert.Throws<InvalidCastException>(
410-
() => dictionary.GetValue<Text>(sIntKey));
411-
Assert.Throws<InvalidCastException>(
412-
() => dictionary.GetValue<Bencodex.Types.Boolean>(sIntKey));
413-
414-
Assert.Equal(
415-
sBinary,
416-
(Binary)dictionary[sBinaryKey]);
417-
Assert.Equal(
418-
sBinary,
419-
dictionary.GetValue<Binary>(sBinaryKey));
420-
Assert.Throws<InvalidCastException>(
421-
() => dictionary.GetValue<Integer>(sBinaryKey));
422-
Assert.Throws<InvalidCastException>(
423-
() => dictionary.GetValue<Text>(sBinaryKey));
424-
Assert.Throws<InvalidCastException>(
425-
() => dictionary.GetValue<Bencodex.Types.Boolean>(sBinaryKey));
426-
339+
Assert.Equal((Binary)sBinary, (Binary)dictionary[sBinaryKey]);
427340
Assert.Equal(
428341
(Bencodex.Types.Boolean)sBoolean,
429342
(Bencodex.Types.Boolean)dictionary[sBooleanKey]);
430-
Assert.Equal(
431-
(Bencodex.Types.Boolean)sBoolean,
432-
dictionary.GetValue<Bencodex.Types.Boolean>(sBooleanKey));
433-
Assert.Throws<InvalidCastException>(
434-
() => dictionary.GetValue<Integer>(sBooleanKey));
435-
Assert.Throws<InvalidCastException>(
436-
() => dictionary.GetValue<Binary>(sBooleanKey));
437-
Assert.Throws<InvalidCastException>(
438-
() => dictionary.GetValue<Text>(sBooleanKey));
439-
440-
Assert.Equal(
441-
sList,
442-
(Bencodex.Types.List)dictionary[sListKey]);
443-
Assert.Equal(
444-
sList,
445-
dictionary.GetValue<Bencodex.Types.List>(sListKey));
446-
Assert.Throws<InvalidCastException>(
447-
() => dictionary.GetValue<Integer>(sListKey));
448-
Assert.Throws<InvalidCastException>(
449-
() => dictionary.GetValue<Binary>(sListKey));
450-
Assert.Throws<InvalidCastException>(
451-
() => dictionary.GetValue<Text>(sListKey));
343+
Assert.Equal(sList, (List)dictionary[sListKey]);
452344

453345
// Byte array keys
454-
Assert.Equal(bText, (Text)dictionary[bTextKey]);
455-
Assert.Equal(bText, dictionary.GetValue<Text>(bTextKey));
456-
Assert.Throws<InvalidCastException>(
457-
() => dictionary.GetValue<Integer>(bTextKey));
458-
Assert.Throws<InvalidCastException>(
459-
() => dictionary.GetValue<Binary>(bTextKey));
460-
Assert.Throws<InvalidCastException>(
461-
() => dictionary.GetValue<Bencodex.Types.Boolean>(bTextKey));
462-
346+
Assert.Equal((Text)bText, (Text)dictionary[bTextKey]);
463347
Assert.Equal((Integer)bShort, (Integer)dictionary[bShortKey]);
464-
Assert.Equal(
465-
(Integer)bShort,
466-
dictionary.GetValue<Integer>(bShortKey));
467-
Assert.Throws<InvalidCastException>(
468-
() => dictionary.GetValue<Binary>(bShortKey));
469-
Assert.Throws<InvalidCastException>(
470-
() => dictionary.GetValue<Text>(bShortKey));
471-
Assert.Throws<InvalidCastException>(
472-
() => dictionary.GetValue<Bencodex.Types.Boolean>(bShortKey));
473-
474348
Assert.Equal((Integer)bInt, (Integer)dictionary[bIntKey]);
475-
Assert.Equal(
476-
(Integer)bInt,
477-
dictionary.GetValue<Integer>(bIntKey));
478-
Assert.Throws<InvalidCastException>(
479-
() => dictionary.GetValue<Binary>(bIntKey));
480-
Assert.Throws<InvalidCastException>(
481-
() => dictionary.GetValue<Text>(bIntKey));
482-
Assert.Throws<InvalidCastException>(
483-
() => dictionary.GetValue<Bencodex.Types.Boolean>(bIntKey));
484-
485-
Assert.Equal(
486-
bBinary,
487-
(Binary)dictionary[bBinaryKey]);
488-
Assert.Equal(
489-
bBinary,
490-
dictionary.GetValue<Binary>(bBinaryKey));
491-
Assert.Throws<InvalidCastException>(
492-
() => dictionary.GetValue<Integer>(bBinaryKey));
493-
Assert.Throws<InvalidCastException>(
494-
() => dictionary.GetValue<Text>(bBinaryKey));
495-
Assert.Throws<InvalidCastException>(
496-
() => dictionary.GetValue<Bencodex.Types.Boolean>(bBinaryKey));
497-
349+
Assert.Equal((Binary)bBinary, (Binary)dictionary[bBinaryKey]);
498350
Assert.Equal(
499351
(Bencodex.Types.Boolean)bBoolean,
500352
(Bencodex.Types.Boolean)dictionary[bBooleanKey]);
501-
Assert.Equal(
502-
(Bencodex.Types.Boolean)bBoolean,
503-
dictionary.GetValue<Bencodex.Types.Boolean>(bBooleanKey));
504-
Assert.Throws<InvalidCastException>(
505-
() => dictionary.GetValue<Integer>(bBooleanKey));
506-
Assert.Throws<InvalidCastException>(
507-
() => dictionary.GetValue<Binary>(bBooleanKey));
508-
Assert.Throws<InvalidCastException>(
509-
() => dictionary.GetValue<Text>(bBooleanKey));
510-
511-
Assert.Equal(
512-
bList,
513-
(Bencodex.Types.List)dictionary[bListKey]);
514-
Assert.Equal(
515-
bList,
516-
dictionary.GetValue<Bencodex.Types.List>(bListKey));
517-
Assert.Throws<InvalidCastException>(
518-
() => dictionary.GetValue<Integer>(bListKey));
519-
Assert.Throws<InvalidCastException>(
520-
() => dictionary.GetValue<Binary>(bListKey));
521-
Assert.Throws<InvalidCastException>(
522-
() => dictionary.GetValue<Text>(bListKey));
353+
Assert.Equal(bList, (List)dictionary[bListKey]);
523354
}
524355

525356
[Fact]

Bencodex/Types/Dictionary.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,23 +1629,6 @@ IEnumerable<KeyValuePair<IKey, IValue>> items
16291629
public bool TryGetKey(IKey equalKey, out IKey actualKey) =>
16301630
_dict.TryGetKey(equalKey, out actualKey);
16311631

1632-
public T GetValue<T>(string name)
1633-
where T : IValue
1634-
{
1635-
return (T)this[name];
1636-
}
1637-
1638-
public T GetValue<T>(ImmutableArray<byte> name)
1639-
where T : IValue
1640-
=>
1641-
(T)this[name];
1642-
1643-
public T GetValue<T>(byte[] name)
1644-
where T : IValue
1645-
{
1646-
return (T)this[name];
1647-
}
1648-
16491632
public override bool Equals(object obj) => obj is Dictionary d && Equals(d);
16501633

16511634
public bool Equals(IValue other) => other is Dictionary d && Equals(d);

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Version 0.17.0
66

77
To be released.
88

9+
- Removed `Dictionary.GetValue<T>()` methods. [[#122]]
10+
11+
[#122]: https://github.com/planetarium/bencodex.net/pull/122
12+
913

1014
Version 0.16.0
1115
--------------

0 commit comments

Comments
 (0)