public void given_empty_post() { var source = new Post(); var bin = source.ToBinary(); var restored = Post.TryGetFromBinary(bin); ShouldBeEqual(source, restored); }
static void ShouldBeEqual(Post source, Post restored) { Assert.AreEqual(source.Id, restored.Id, "Id"); Assert.AreEqual(source.AnswerCount, restored.AnswerCount, "AnswerCount"); Assert.AreEqual(source.Body ?? "", restored.Body, "Body"); Assert.AreEqual(source.CommentCount, restored.CommentCount, "CommentCount"); Assert.AreEqual(source.CreationDate, restored.CreationDate, "CreationDate"); Assert.AreEqual(source.FavoriteCount, restored.FavoriteCount, "FavoriteCount"); Assert.AreEqual(source.LastEditDate, restored.LastEditDate, "LastEditDate"); Assert.AreEqual(source.OwnerUserId, restored.OwnerUserId, "OwnerUserId"); Assert.AreEqual(source.PostTypeId, restored.PostTypeId, "PostTypeId"); CollectionAssert.AreEqual(source.Tags, restored.Tags, "Tags"); Assert.AreEqual(source.Title ?? "", restored.Title, "Title"); Assert.AreEqual(source.ViewCount, restored.ViewCount, "ViewCount"); }
public void given_filled_post() { var source = new Post { Id = 101, AnswerCount = 4, Body = "Post body test", CommentCount = 5, CreationDate = new DateTime(2011, 12, 1, 13, 13, 13), FavoriteCount = 9, LastEditDate = new DateTime(2012, 12, 1, 13, 13, 13), OwnerUserId = 45, PostTypeId = 23, Tags = new[] { "c#", ".net", "java" }, Title = "Post title test", ViewCount = 555 }; var bin = source.ToBinary(); var restored = Post.TryGetFromBinary(bin); ShouldBeEqual(source, restored); }
private static Post PostParse(string line) { try { long defaultLong; DateTime defaultDate; var post = new Post { Id = long.TryParse(Get(line, "Id"), out defaultLong) ? defaultLong : -1, PostTypeId = long.TryParse(Get(line, "PostTypeId"), out defaultLong) ? defaultLong : -1, CreationDate = DateTime.TryParse(Get(line, "CreationDate"), out defaultDate) ? defaultDate : DateTime.MinValue, ViewCount = long.TryParse(Get(line, "ViewCount"), out defaultLong) ? defaultLong : -1, Body = HttpUtility.HtmlDecode(Get(line, "Body")), OwnerUserId = long.TryParse(Get(line, "OwnerUserId"), out defaultLong) ? defaultLong : -1, LastEditDate = DateTime.TryParse(Get(line, "LastEditDate"), out defaultDate) ? defaultDate : DateTime.MinValue, Title = HttpUtility.HtmlDecode(Get(line, "Title")), AnswerCount = long.TryParse(Get(line, "AnswerCount"), out defaultLong) ? defaultLong : -1, CommentCount = long.TryParse(Get(line, "CommentCount"), out defaultLong) ? defaultLong : -1, FavoriteCount = long.TryParse(Get(line, "FavoriteCount"), out defaultLong) ? defaultLong : -1, Tags = (">" + HttpUtility.HtmlDecode(Get(line, "Tags")) + "<").Split(new[] { "><" }, StringSplitOptions.RemoveEmptyEntries) }; return post; } catch (Exception) { return null; } }
public static Post TryGetFromBinary(byte[] data) { using (var mem = new MemoryStream(data)) using (var bin = new BinaryReader(mem)) { if (bin.ReadInt32() != Signature) return null; var post = new Post { Id = bin.ReadInt64(), PostTypeId = bin.ReadInt64(), CreationDate = DateTime.FromBinary(bin.ReadInt64()), ViewCount = bin.ReadInt64(), Body = bin.ReadString(), OwnerUserId = bin.ReadInt64(), LastEditDate = DateTime.FromBinary(bin.ReadInt64()), Title = bin.ReadString(), AnswerCount = bin.ReadInt64(), CommentCount = bin.ReadInt64(), FavoriteCount = bin.ReadInt64() }; var tags = new List<string>(); var tagCount = bin.ReadInt32(); for (var i = 0; i < tagCount; i++) { tags.Add(bin.ReadString()); } post.Tags = tags.ToArray(); return post; } }