/// <summary> /// Converts the torrent to a <see cref="BDictionary"/>. /// </summary> /// <returns></returns> public virtual BDictionary ToBDictionary() { var torrent = new BDictionary(); var trackerCount = Trackers.Flatten().Count(); if (trackerCount > 0) { torrent[TorrentFields.Announce] = new BList(Trackers.First().Select(x => new BString(x, Encoding))); } if (trackerCount > 1) { torrent[TorrentFields.AnnounceList] = new BList(Trackers.Select(x => new BList(x, Encoding))); } if (Encoding != null) { torrent[TorrentFields.Encoding] = new BString(Encoding.WebName.ToUpper(), Encoding); } if (Comment != null) { torrent[TorrentFields.Comment] = new BString(Comment, Encoding); } if (CreatedBy != null) { torrent[TorrentFields.CreatedBy] = new BString(CreatedBy, Encoding); } if (CreationDate != null) { torrent[TorrentFields.CreationDate] = (BNumber)CreationDate; } var info = CreateInfoDictionary(Encoding); if (info.Any()) { torrent[TorrentFields.Info] = info; } if (ExtraFields != null) { torrent.MergeWith(ExtraFields, ExistingKeyAction.Merge); } return(torrent); }
public void MergeWith_NumberReplacesExistingKey() { var dict1 = new BDictionary { { "key", 1 } }; var dict2 = new BDictionary { { "key", 42 } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(1); dict1["key"].Should().Be((BNumber)42); }
public void MergeWith_StringReplacesExistingKey() { var dict1 = new BDictionary { { "key", "value" } }; var dict2 = new BDictionary { { "key", "replaced value" } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(1); dict1["key"].Should().Be((BString)"replaced value"); }
public void MergeWith_NumberWithNewKeyIsAdded() { var dict1 = new BDictionary { { "key", 1 } }; var dict2 = new BDictionary { { "another key", 42 } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(2); dict1["key"].Should().Be((BNumber)1); dict1["another key"].Should().Be((BNumber)42); }
public void MergeWith_StringWithNewKeyIsAdded() { var dict1 = new BDictionary { { "key", "value" } }; var dict2 = new BDictionary { { "another key", "value" } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(2); dict1["key"].Should().Be((BString)"value"); dict1["another key"].Should().Be((BString)"value"); }
public void MergeWith_DictionaryReplacesExistingKeyOfDifferentType() { var dict1 = new BDictionary { { "main", new BList { "item1" } } }; var dict2 = new BDictionary { { "main", new BDictionary { { "key", "value" } } } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(1); dict1.Get <BDictionary>("main").Should().HaveCount(1).And.ContainKey("key"); }
public void MergeWith_ExistingKeyOption_Skip_DictionarySkippedForExistingKeyOfSameType() { var dict1 = new BDictionary { { "main", new BDictionary { { "key", "value" } } } }; var dict2 = new BDictionary { { "main", new BDictionary { { "key2", "value2" } } } }; dict1.MergeWith(dict2, ExistingKeyAction.Skip); dict1.Should().HaveCount(1); dict1.Get <BDictionary>("main").Should().HaveCount(1).And.ContainKey("key"); }
public void MergeWith_ExistingKeyOption_Merge_DictionaryMergedWithExistingKeyOfSameType() { var dict1 = new BDictionary { { "main", new BDictionary { { "key", "value" } } } }; var dict2 = new BDictionary { { "main", new BDictionary { { "key2", "value2" } } } }; dict1.MergeWith(dict2, ExistingKeyAction.Merge); dict1.Should().HaveCount(1); dict1.Get <BDictionary>("main").Should().HaveCount(2).And.ContainKeys("key", "key2"); }
public void MergeWith_ListWithNewKeyIsAdded() { var list = new BList { 1, 2, 3 }; var dict1 = new BDictionary { { "key", 1 } }; var dict2 = new BDictionary { { "another key", list } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(2); dict1["key"].Should().Be((BNumber)1); dict1["another key"].Should().Be(list); }
public void MergeWith_DictionaryWithNewKeyIsAdded() { var dict1 = new BDictionary { { "main", new BDictionary { { "key", "value" } } } }; var dict2 = new BDictionary { { "main2", new BDictionary { { "key2", "value2" } } } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(2); dict1.Get <BDictionary>("main").Should().HaveCount(1).And.ContainKeys("key"); dict1.Get <BDictionary>("main2").Should().HaveCount(1).And.ContainKeys("key2"); }
public void MergeWith_ListReplacesExistingKeyOfDifferentType() { var dict1 = new BDictionary { { "key", "value" } }; var dict2 = new BDictionary { { "key", new BList { "item1", "item2" } } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(1); dict1["key"].Should().BeOfType <BList>(); dict1.Get <BList>("key").Should() .HaveCount(2) .And.ContainInOrder((BString)"item1", (BString)"item2"); }
public void MergeWith_ExistingKeyOption_Skip_ListIsSkippedForExistingKeyOfSameType() { var dict1 = new BDictionary { { "key", new BList { "item1" } } }; var dict2 = new BDictionary { { "key", new BList { "item2", "item3" } } }; dict1.MergeWith(dict2, ExistingKeyAction.Skip); dict1.Should().HaveCount(1); dict1["key"].Should().BeOfType <BList>(); dict1.Get <BList>("key").Should() .HaveCount(1) .And.ContainInOrder((BString)"item1"); }
public void MergeWith_ExistingKeyOption_Skip_DictionarySkippedForExistingKeyOfSameType() { var dict1 = new BDictionary {{"main", new BDictionary {{"key", "value"}}}}; var dict2 = new BDictionary {{"main", new BDictionary {{"key2", "value2"}}}}; dict1.MergeWith(dict2, ExistingKeyAction.Skip); dict1.Should().HaveCount(1); dict1.Get<BDictionary>("main").Should().HaveCount(1).And.ContainKey("key"); }
public void MergeWith_StringReplacesExistingKey() { var dict1 = new BDictionary {{"key", "value"}}; var dict2 = new BDictionary {{"key", "replaced value"}}; dict1.MergeWith(dict2); dict1.Should().HaveCount(1); dict1["key"].Should().Be((BString)"replaced value"); }
public void MergeWith_StringWithNewKeyIsAdded() { var dict1 = new BDictionary {{"key", "value"}}; var dict2 = new BDictionary {{"another key", "value"}}; dict1.MergeWith(dict2); dict1.Should().HaveCount(2); dict1["key"].Should().Be((BString)"value"); dict1["another key"].Should().Be((BString)"value"); }
public void MergeWith_NumberWithNewKeyIsAdded() { var dict1 = new BDictionary {{"key", 1}}; var dict2 = new BDictionary {{"another key", 42}}; dict1.MergeWith(dict2); dict1.Should().HaveCount(2); dict1["key"].Should().Be((BNumber) 1); dict1["another key"].Should().Be((BNumber) 42); }
public void MergeWith_NumberReplacesExistingKey() { var dict1 = new BDictionary {{"key", 1}}; var dict2 = new BDictionary {{"key", 42}}; dict1.MergeWith(dict2); dict1.Should().HaveCount(1); dict1["key"].Should().Be((BNumber) 42); }
public void MergeWith_ListWithNewKeyIsAdded() { var list = new BList {1, 2, 3}; var dict1 = new BDictionary {{"key", 1}}; var dict2 = new BDictionary {{"another key", list}}; dict1.MergeWith(dict2); dict1.Should().HaveCount(2); dict1["key"].Should().Be((BNumber) 1); dict1["another key"].Should().Be(list); }
public void MergeWith_ListReplacesExistingKeyOfDifferentType() { var dict1 = new BDictionary {{"key", "value"}}; var dict2 = new BDictionary {{"key", new BList {"item1", "item2"}}}; dict1.MergeWith(dict2); dict1.Should().HaveCount(1); dict1["key"].Should().BeOfType<BList>(); dict1.Get<BList>("key").Should() .HaveCount(2) .And.ContainInOrder((BString) "item1", (BString) "item2"); }
public void MergeWith_ExistingKeyOption_Skip_ListIsSkippedForExistingKeyOfSameType() { var dict1 = new BDictionary {{"key", new BList {"item1"}}}; var dict2 = new BDictionary {{"key", new BList {"item2", "item3"}}}; dict1.MergeWith(dict2, ExistingKeyAction.Skip); dict1.Should().HaveCount(1); dict1["key"].Should().BeOfType<BList>(); dict1.Get<BList>("key").Should() .HaveCount(1) .And.ContainInOrder((BString) "item1"); }
public void MergeWith_DictionaryWithNewKeyIsAdded() { var dict1 = new BDictionary { { "main", new BDictionary { { "key", "value" } } } }; var dict2 = new BDictionary { { "main2", new BDictionary { { "key2", "value2" } } } }; dict1.MergeWith(dict2); dict1.Should().HaveCount(2); dict1.Get<BDictionary>("main").Should().HaveCount(1).And.ContainKeys("key"); dict1.Get<BDictionary>("main2").Should().HaveCount(1).And.ContainKeys("key2"); }
public void MergeWith_ExistingKeyOption_Merge_DictionaryMergedWithExistingKeyOfSameType() { var dict1 = new BDictionary {{"main", new BDictionary {{"key", "value"}}}}; var dict2 = new BDictionary {{"main", new BDictionary {{"key2", "value2"}}}}; dict1.MergeWith(dict2, ExistingKeyAction.Merge); dict1.Should().HaveCount(1); dict1.Get<BDictionary>("main").Should().HaveCount(2).And.ContainKeys("key", "key2"); }
/// <summary> /// Converts the torrent to a <see cref="BDictionary"/>. /// </summary> /// <returns></returns> public virtual BDictionary ToBDictionary() { var torrent = new BDictionary(); var trackerCount = Trackers.Flatten().Count(); if (trackerCount > 0) torrent[TorrentFields.Announce] = new BList(Trackers.First().Select(x => new BString(x, Encoding))); if (trackerCount > 1) torrent[TorrentFields.AnnounceList] = new BList(Trackers.Select(x => new BList(x, Encoding))); if (Encoding != null) torrent[TorrentFields.Encoding] = new BString(Encoding.WebName.ToUpper(), Encoding); if (Comment != null) torrent[TorrentFields.Comment] = new BString(Comment, Encoding); if (CreatedBy != null) torrent[TorrentFields.CreatedBy] = new BString(CreatedBy, Encoding); if (CreationDate != null) torrent[TorrentFields.CreationDate] = (BNumber)CreationDate; var info = CreateInfoDictionary(Encoding); if (info.Any()) torrent[TorrentFields.Info] = info; if (ExtraFields != null) torrent.MergeWith(ExtraFields, ExistingKeyAction.Merge); return torrent; }
public void MergeWith_DictionaryReplacesExistingKeyOfDifferentType() { var dict1 = new BDictionary {{"main", new BList {"item1"}}}; var dict2 = new BDictionary {{"main", new BDictionary {{"key", "value"}}}}; dict1.MergeWith(dict2); dict1.Should().HaveCount(1); dict1.Get<BDictionary>("main").Should().HaveCount(1).And.ContainKey("key"); }