public void ValidJson_ReturnsObject() { // Arrange var v0 = Rnd.Lng; var v1 = Rnd.Str; var v2 = Rnd.Int; var v3 = Rnd.DateTime; var v4 = Rnd.Flip; var input = "{" + $"\"id\":\"{v0}\"," + $"\"str\":\"{v1}\"," + $"\"num\":{v2}," + $"\"dt\":\"{v3:s}\"," + $"\"mbe\":{JsonF.Bool(v4)}," + "\"empty\":null" + "}"; var expected = new Test { Id = new(v0), Str = v1, Num = v2, DT = v3, Mbe = v4 }; // Act var result = JsonF.Deserialise <Test>(input).Unwrap(() => new Test()); // Assert Assert.Equal(expected, result, new TestComparer()); }
public void Object_Returns_Json() { // Arrange var v0 = Rnd.Lng; var v1 = Rnd.Str; var v2 = Rnd.Int; var v3 = Rnd.DateTime; var v4 = Rnd.Flip; var input = new Test { Id = new(v0), Str = v1, Num = v2, DT = v3, Mbe = v4 }; var expected = "{" + $"\"id\":\"{v0}\"," + $"\"str\":\"{v1}\"," + $"\"num\":{v2}," + $"\"dt\":\"{v3:s}\"," + $"\"mbe\":{JsonF.Bool(v4)}," + "\"empty\":null" + "}"; // Act var result = JsonF.Serialise(input); // Assert Assert.Equal(expected, result); } }
/// <summary> /// Deserialise list from JSON /// </summary> /// <typeparam name="T">Enumerated value type</typeparam> /// <param name="json">JSON serialised list</param> public static EnumeratedList <T> Deserialise <T>(string json) where T : Enumerated { var strings = JsonF.Deserialise <List <string> >(json).Unwrap(() => new List <string>()); return(new EnumeratedList <T>(strings)); }
/// <summary> /// Parse a generic embedded object /// </summary> /// <param name="content">Post content</param> /// <param name="type">EmbedType</param> /// <param name="provider">Provider</param> /// <param name="format">Function to parse matched content</param> internal static string Parse(string content, EmbedType type, Provider provider, Func <string, EmbedParsed, string> format) { // Get Embedded info const string pattern = "<!-- wp:embed ({.*?}) -->(.*?)<!-- /wp:embed -->"; var matches = Regex.Matches(content, pattern, RegexOptions.Singleline); if (matches.Count == 0) { return(content); } // Parse each match foreach (Match match in matches) { // Info is encoded as JSON so deserialise it first var info = match.Groups[1].Value; _ = JsonF.Deserialise <EmbedParsed>(info).IfSome(embed => { // Only replace matching embed types if (embed.Type == type && embed.ProviderNameSlug == provider) { // Replace content using child Format() method content = content.Replace( match.Value, format(Rnd.StringF.Get(10), embed) ); } }); } // Return parsed content return(content); }
/// <summary> /// Parse WordPress photo gallery /// </summary> /// <param name="content">Post content</param> internal static string Parse(string content) { // Get Gallery info const string pattern = "<!-- wp:gallery ({.*?}) -->(.*?)<!-- /wp:gallery -->"; var matches = Regex.Matches(content, pattern, RegexOptions.Singleline); if (matches.Count == 0) { return(content); } // Replacement format const string format = "<div id=\"{0}\" class=\"hide image-gallery\" data-ids=\"{1}\" data-cols=\"{2}\"></div>"; // Parse each match foreach (Match match in matches) { // Info is encoded as JSON so deserialise it first var info = match.Groups[1].Value; _ = JsonF.Deserialise <GalleryParsed>(info).IfSome(gallery => content = content.Replace( match.Value, string.Format(CultureInfo.InvariantCulture, format, Rnd.StringF.Get(10), string.Join(",", gallery.Ids), gallery.Columns) ) ); } // Return parsed content return(content); }
public void Returns_Correct_Value(bool value, string expected) { // Arrange // Act var result = JsonF.Bool(value); // Assert Assert.Equal(expected, result); }
public void Null_Returns_Empty(object input) { // Arrange // Act var result = JsonF.Serialise(input); // Assert Assert.Equal(JsonF.Empty, result); }
public void Deserialise_Null_Or_Invalid_Value_Returns_None_With_DeserialisingValueExceptionMsg(string input) { // Arrange // Act var result = JsonF.Deserialise <Maybe <Test> >(input); // Assert result.AssertNone().AssertType <DeserialiseExceptionMsg>(); }
public void Null_Or_Whitespace_Returns_None(string input) { // Arrange // Act var result = JsonF.Deserialise <Test>(input); // Assert result.AssertNone().AssertType <DeserialisingNullOrEmptyStringMsg>(); }
public void Serialise_Null_Returns_Empty_Json(Enumerated input) { // Arrange // Act var result = JsonF.Serialise(input); // Assert Assert.Equal(JsonF.Empty, result); }
public void Returns_MajorProphets_Books() { // Arrange const string?prophets = "[\"Isaiah\",\"Jeremiah\",\"Lamentations\",\"Ezekiel\",\"Daniel\"]"; // Act var result = JsonF.Serialise(BibleBooks.MajorProphets); // Assert Assert.Equal(prophets, result); }
public void Returns_Gospels() { // Arrange const string?gospels = "[\"Matthew\",\"Mark\",\"Luke\",\"John\"]"; // Act var result = JsonF.Serialise(BibleBooks.Gospels); // Assert Assert.Equal(gospels, result); }
public void Deserialise_Object_With_Maybe_Property_Null_Or_Invalid_Value_Returns_None_With_DeserialisingValueExceptionMsg(string input) { // Arrange var json = $"{{\"test\":{input}}}"; // Act var result = JsonF.Deserialise <Wrapper>(json); // Assert result.AssertNone().AssertType <DeserialiseExceptionMsg>(); }
public void Returns_All_Books() { // Arrange const string?all = "[\"Genesis\",\"Exodus\",\"Leviticus\",\"Numbers\",\"Deuteronomy\",\"Joshua\",\"Judges\",\"Ruth\",\"1 Samuel\",\"2 Samuel\",\"1 Kings\",\"2 Kings\",\"1 Chronicles\",\"2 Chronicles\",\"Ezra\",\"Nehemiah\",\"Esther\",\"Job\",\"Psalms\",\"Proverbs\",\"Ecclesiastes\",\"Song of Songs\",\"Isaiah\",\"Jeremiah\",\"Lamentations\",\"Ezekiel\",\"Daniel\",\"Hosea\",\"Joel\",\"Amos\",\"Obadiah\",\"Jonah\",\"Micah\",\"Nahum\",\"Habakkuk\",\"Zephaniah\",\"Haggai\",\"Zechariah\",\"Malachi\",\"Matthew\",\"Mark\",\"Luke\",\"John\",\"Acts\",\"Romans\",\"1 Corinthians\",\"2 Corinthians\",\"Galatians\",\"Ephesians\",\"Philippians\",\"Colossians\",\"1 Thessalonians\",\"2 Thessalonians\",\"1 Timothy\",\"2 Timothy\",\"Titus\",\"Philemon\",\"Hebrews\",\"James\",\"1 Peter\",\"2 Peter\",\"1 John\",\"2 John\",\"3 John\",\"Jude\",\"Revelation\"]"; // Act var result = JsonF.Serialise(BibleBooks.All); // Assert Assert.Equal(all, result); }
public void Returns_Epistles() { // Arrange const string?epistles = "[\"Acts\",\"Romans\",\"1 Corinthians\",\"2 Corinthians\",\"Galatians\",\"Ephesians\",\"Philippians\",\"Colossians\",\"1 Thessalonians\",\"2 Thessalonians\",\"1 Timothy\",\"2 Timothy\",\"Titus\",\"Philemon\",\"Hebrews\",\"James\",\"1 Peter\",\"2 Peter\",\"1 John\",\"2 John\",\"3 John\",\"Jude\",\"Revelation\"]"; // Act var result = JsonF.Serialise(BibleBooks.Epistles); // Assert Assert.Equal(epistles, result); }
public void Returns_OldTestament_Books() { // Arrange const string?ot = "[\"Genesis\",\"Exodus\",\"Leviticus\",\"Numbers\",\"Deuteronomy\",\"Joshua\",\"Judges\",\"Ruth\",\"1 Samuel\",\"2 Samuel\",\"1 Kings\",\"2 Kings\",\"1 Chronicles\",\"2 Chronicles\",\"Ezra\",\"Nehemiah\",\"Esther\",\"Job\",\"Psalms\",\"Proverbs\",\"Ecclesiastes\",\"Song of Songs\",\"Isaiah\",\"Jeremiah\",\"Lamentations\",\"Ezekiel\",\"Daniel\",\"Hosea\",\"Joel\",\"Amos\",\"Obadiah\",\"Jonah\",\"Micah\",\"Nahum\",\"Habakkuk\",\"Zephaniah\",\"Haggai\",\"Zechariah\",\"Malachi\"]"; // Act var result = JsonF.Serialise(BibleBooks.OldTestament); // Assert Assert.Equal(ot, result); }
public void Returns_MinorProphets_Books() { // Arrange const string?prophets = "[\"Hosea\",\"Joel\",\"Amos\",\"Obadiah\",\"Jonah\",\"Micah\",\"Nahum\",\"Habakkuk\",\"Zephaniah\",\"Haggai\",\"Zechariah\",\"Malachi\"]"; // Act var result = JsonF.Serialise(BibleBooks.MinorProphets); // Assert Assert.Equal(prophets, result); }
public void Returns_Law_Books() { // Arrange const string?law = "[\"Genesis\",\"Exodus\",\"Leviticus\",\"Numbers\",\"Deuteronomy\"]"; // Act var result = JsonF.Serialise(BibleBooks.Law); // Assert Assert.Equal(law, result); }
public void InvalidJson_Returns_None() { // Arrange var input = Rnd.Str; // Act var result = JsonF.Deserialise <Test>(input); // Assert result.AssertNone().AssertType <DeserialiseExceptionMsg>(); }
public void Returns_History_Books() { // Arrange const string?history = "[\"Joshua\",\"Judges\",\"Ruth\",\"1 Samuel\",\"2 Samuel\",\"1 Kings\",\"2 Kings\",\"1 Chronicles\",\"2 Chronicles\",\"Ezra\",\"Nehemiah\",\"Esther\"]"; // Act var result = JsonF.Serialise(BibleBooks.History); // Assert Assert.Equal(history, result); }
public void Returns_NewTestament_Books() { // Arrange const string?nt = "[\"Matthew\",\"Mark\",\"Luke\",\"John\",\"Acts\",\"Romans\",\"1 Corinthians\",\"2 Corinthians\",\"Galatians\",\"Ephesians\",\"Philippians\",\"Colossians\",\"1 Thessalonians\",\"2 Thessalonians\",\"1 Timothy\",\"2 Timothy\",\"Titus\",\"Philemon\",\"Hebrews\",\"James\",\"1 Peter\",\"2 Peter\",\"1 John\",\"2 John\",\"3 John\",\"Jude\",\"Revelation\"]"; // Act var result = JsonF.Serialise(BibleBooks.NewTestament); // Assert Assert.Equal(nt, result); }
public void Deserialise_Object_With_Enumerated_Property_But_Null_Value_And_Disallow_Empty_Returns_None(string input) { // Arrange var json = $"{{ \"enumeratedValue\": {input} }}"; // Act var result = JsonF.Deserialise <EnumeratedWrapperTest1>(json); // Assert result.AssertNone().AssertType <DeserialiseExceptionMsg>(); }
public void Returns_Wisdom_Books() { // Arrange const string?wisdom = "[\"Job\",\"Psalms\",\"Proverbs\",\"Ecclesiastes\",\"Song of Songs\"]"; // Act var result = JsonF.Serialise(BibleBooks.Wisdom); // Assert Assert.Equal(wisdom, result); }
public void Returns_PaulineLetters() { // Arrange const string?pauline = "[\"Romans\",\"1 Corinthians\",\"2 Corinthians\",\"Galatians\",\"Ephesians\",\"Philippians\",\"Colossians\",\"1 Thessalonians\",\"2 Thessalonians\",\"1 Timothy\",\"2 Timothy\",\"Titus\",\"Philemon\"]"; // Act var result = JsonF.Serialise(BibleBooks.PaulineLetters); // Assert Assert.Equal(pauline, result); }
public void Object_Input_String_Key_Returns_Encrypted_Json() { // Arrange // Act var json = JsonF.Serialise(defaultInputObject); var result = defaultInputObject.Encrypt(defaultStringKey); // Assert Assert.NotEqual(json, result); }
public void Serialise_Returns_Json_Value() { // Arrange var value = Rnd.Str; var enumerated = new EnumeratedTest(value); // Act var result = JsonF.Serialise(enumerated); // Assert Assert.Equal($"\"{value}\"", result); }
public void Object_Creates_With_Json() { // Arrange var value = new Test(Rnd.Str, Rnd.Int); var json = JsonF.Serialise(value).UnsafeUnwrap(); // Act var result = Jsonb.Create(value); // Assert Assert.Equal(json, result.ToString()); }
public void Deserialise_Null_Returns_Object_With_Empty_Value() { // Arrange var json = JsonF.Empty; // Act var result = JsonF.Deserialise <EnumeratedTest0>(json); // Assert var some = result.AssertSome(); Assert.Equal(string.Empty, some.ToString()); }
public void Serialise_None_Returns_Empty_Json() { // Arrange var maybe = Create.None <int>(); // Act var result = JsonF.Serialise(maybe); // Assert var some = result.AssertSome(); Assert.Equal(JsonF.Empty, some); }
public void ArrayDoesNotContainBibleBooks_ReturnsOriginalArray() { // Arrange var array = new[] { Rnd.Str, Rnd.Str, Rnd.Str, Rnd.Str }; var shuffled = array.Shuffle().ToList(); var originalOrder = JsonF.Serialise(shuffled); // Act shuffled.SortBibleBooks(b => b); // Assert Assert.Equal(originalOrder, JsonF.Serialise(shuffled)); }