public void JToken_JTokenFormatter_ComplexObject() { var testVal = JObject.Parse(@"{ ""msg"": ""JS error message"", ""logLevel"": ""error"", ""data"": { ""link"": ""https://test.com"", ""description"": ""error description"", ""values"": [ ""value1"", ""value2"", ""value3"" ] } }"); var formatter = new JTokenFormatter(new DefaultJTokenFormatters(), ",", 3); var result = formatter.Format(testVal, 0); var propParser = new PropertyParser(new DefaultFormatters(), ","); var result2 = propParser.Parse(testVal); Assert.Equal(5, result.Count); Assert.Equal("JS error message", result["msg"]); Assert.Equal("error", result["logLevel"]); Assert.Equal("https://test.com", result["data_link"]); Assert.Equal("error description", result["data_description"]); Assert.Equal("value1,value2,value3", result["data_values"]); }
public void JToken_JTokenFormatter_Formatting_Object() { var testValue = JObject.FromObject(new { Alpha = "a", Beta = 1.0, Receiver = new { Name = "bkennedy", User_Id = 2 }, Sender = new { Name = "admin", User_Id = 1 } }); var formatter = new JTokenFormatter(new DefaultJTokenFormatters(), ",", 3); var result = formatter.Format(testValue, 0); Assert.Equal(6, result.Count); Assert.Equal("a", result["Alpha"]); Assert.Equal("1", result["Beta"]); Assert.Equal("bkennedy", result["Receiver_Name"]); Assert.Equal("2", result["Receiver_User_Id"]); Assert.Equal("admin", result["Sender_Name"]); Assert.Equal("1", result["Sender_User_Id"]); }
public void JToken_JTokenFormatter_Formatting() { var testArray = JArray.FromObject(new int[] { 1, 1, 2, 3, 5, 8, 13 }); var formatter = new JTokenFormatter(new DefaultJTokenFormatters(), ",", 3); var result = formatter.Format(testArray, 0); Assert.Single(result); Assert.Equal("1,1,2,3,5,8,13", result[string.Empty]); }
public void ContainJson(string expectedJson, string reason = "", params object[] reasonArgs) { var contentType = Subject.Content.Headers?.ContentType?.MediaType; var stringContent = Subject.Content.ReadAsStringAsync(); var jsonContent = JToken.Parse(stringContent.Result); var expectedJsonContent = JToken.Parse(expectedJson); var jsonFormatter = new JTokenFormatter(); var jsonFormattingContext = new FormattingContext { UseLineBreaks = false }; var message = "Expected response to have content of type application/json with value " + jsonFormatter.Format(expectedJsonContent, jsonFormattingContext, null).Replace("{", "{{").Replace("}", "}}") + $", but it had content of type {string.Join(", ", contentType)} and value " + jsonFormatter.Format(jsonContent, jsonFormattingContext, null).Replace("{", "{{").Replace("}", "}}"); Execute.Assertion .ForCondition(contentType == "application/json") .ForCondition(JToken.DeepEquals(jsonContent, expectedJsonContent)) .BecauseOf(reason, reasonArgs) .FailWith(message); }
public void Should_preserve_indenting() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var json = JToken.Parse("{ \"id\":1 }"); var sut = new JTokenFormatter(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var actual = sut.Format(json, new FormattingContext { UseLineBreaks = true }, (path, value) => ""); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- actual.Should().Be(json.ToString(Newtonsoft.Json.Formatting.Indented)); }
public void Should_Remove_line_breaks_and_indenting() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var json = JToken.Parse("{ \"id\":1 }"); var sut = new JTokenFormatter(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- // ReSharper disable once RedundantArgumentDefaultValue var actual = sut.Format(json, new FormattingContext { UseLineBreaks = false }, (path, value) => ""); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- actual.Should().Be(json.ToString().RemoveNewLines()); }