Пример #1
0
        public void Test1()
        {
            Console.WriteLine($"==== {nameof(PartialExample)}.{nameof(Test1)} ==== ");

            const string expectedJson = @"{
                ""a"":{
                    ""__partial"":{
                        ""tested"": ""123""
                    }
                },
                ""b"":""abc""
            }";
            const string actualJson   = @"{
                ""a"": {
                    ""tested"": ""123"",
                    ""ignored"": ""42""
                },
                ""b"":""abc""
            }";

            var jsonComparer = TestUtils.JsonComparer.JsonComparer.GetDefault();
            var errors       = jsonComparer.Compare(expectedJson, actualJson);

            Console.WriteLine(JsonComparerOutputFormatter.GetReadableMessage(expectedJson, actualJson, errors));
        }
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            var actualJToken = actual as JToken ?? JsonConvert.DeserializeObject <JToken>(actual as string, new JsonSerializerSettings
            {
                DateParseHandling = DateParseHandling.None
            });
            var expectedJToken = (JToken)Arguments[0];
            var jsonComparer   = _jsonComparer ?? JsonComparer.GetDefault();
            var errors         = jsonComparer.Compare(expectedJToken, actualJToken);
            var message        = JsonComparerOutputFormatter.GetReadableMessage(expectedJToken, actualJToken, errors);

            return(new JsonEquivalentConstraintResult(this, actual, errors?.Count == 0, message));
        }
Пример #3
0
        public void Test5()
        {
            Console.WriteLine("==== CaptureExample.Test5 ==== ");

            var jsonComparer = TestUtils.JsonComparer.JsonComparer.GetDefault(((captureName, token) => {
                Console.WriteLine($"Captured value: name={captureName} token={token}");
            }));

            var expectedJson = JToken.Parse(@"{""a"":{""__capture"":{""regex"":""(?<localCapture>bcd)""}}}");
            var actualJson   = JToken.Parse(@"{""a"":""abcdef""}");

            var errors = jsonComparer.Compare(expectedJson, actualJson);

            Console.WriteLine(JsonComparerOutputFormatter.GetReadableMessage(expectedJson, actualJson, errors));
        }
Пример #4
0
        public void Test1()
        {
            Console.WriteLine("==== CaptureExample.Test1 ==== ");

            var jsonComparer = TestUtils.JsonComparer.JsonComparer.GetDefault(((captureName, token) => {
                Console.WriteLine($"Captured value: name={captureName} token={token}");
            }));

            const string expectedJson = @"{""a"":{""__capture"":{""name"": ""some-name"", ""type"":""integer""}}, ""b"":""abc""}";
            const string actualJson   = @"{""a"":42, ""b"":""abc""}";

            var errors = jsonComparer.Compare(expectedJson, actualJson);

            Console.WriteLine(JsonComparerOutputFormatter.GetReadableMessage(expectedJson, actualJson, errors));
        }
Пример #5
0
        public void Test1()
        {
            Console.WriteLine("==== SimpleExample.Test1 ==== ");
            const string expectedJson = @"{
                ""a"":1,
                ""b"":""abc""
            }";
            const string actualJson   = @"{
                ""a"":42,
                ""b"":""abc""
            }";
            var          jsonComparer = TestUtils.JsonComparer.JsonComparer.GetDefault();
            var          errors       = jsonComparer.Compare(expectedJson, actualJson);

            Console.WriteLine(JsonComparerOutputFormatter.GetReadableMessage(expectedJson, actualJson, errors));
        }
Пример #6
0
        public void Test4()
        {
            Console.WriteLine("==== CaptureExample.Test4 ==== ");

            var jsonComparer = TestUtils.JsonComparer.JsonComparer.GetDefault(((captureName, token) => {
                Console.WriteLine($"Captured value: name={captureName} token={token}");
            }));


            var expectedJson = JToken.Parse(@"{""a"":{""__capture"":{""name"": ""some-global-capture-name"", ""regex"":""^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$""}}}");
            var actualJson   = JToken.Parse(@"{""a"":""B6E73AF8-BDB9-41B2-BB77-28575B08A28C""}");

            var errors = jsonComparer.Compare(expectedJson, actualJson);

            Console.WriteLine(JsonComparerOutputFormatter.GetReadableMessage(expectedJson, actualJson, errors));
        }
        public void Test5()
        {
            Console.WriteLine("==== MatchExample.Test5 ==== ");
            const string expectedJson = @"{
                ""date"":{
                    ""__match"":{
                        ""regex"": ""^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{7}Z?""
                    }
                }
            }";
            const string actualJson   = @"{
                ""date"": ""2042-05-04T06:01:06.0000000Z""
            }";
            var          jsonComparer = TestUtils.JsonComparer.JsonComparer.GetDefault();
            var          errors       = jsonComparer.Compare(expectedJson, actualJson);

            Console.WriteLine(JsonComparerOutputFormatter.GetReadableMessage(expectedJson, actualJson, errors));
        }
        public void Test4()
        {
            Console.WriteLine("==== MatchExample.Test4 ==== ");
            const string expectedJson = @"{
                ""a"":{
                    ""__match"":{
                        ""type"": ""integer""
                    }
                },
                ""b"":""abc""
            }";
            const string actualJson   = @"{
                ""a"":42,
                ""b"":""abc""
            }";
            var          jsonComparer = TestUtils.JsonComparer.JsonComparer.GetDefault();
            var          errors       = jsonComparer.Compare(expectedJson, actualJson);

            Console.WriteLine(JsonComparerOutputFormatter.GetReadableMessage(expectedJson, actualJson, errors));
        }
        public void Test3()
        {
            Console.WriteLine("==== MatchExample.Test3 ==== ");
            var expectedJToken = JToken.Parse(@"{
                ""a"":{
                    ""__match"":{
                        ""regex"": "".+""
                    }
                },
                ""b"":""abc""
            }");
            var actualJToken   = JToken.Parse(@"{
                ""a"":""abc"",
                ""b"":""def""
            }");
            var jsonComparer   = TestUtils.JsonComparer.JsonComparer.GetDefault();
            var errors         = jsonComparer.Compare(expectedJToken, actualJToken);

            Console.WriteLine(JsonComparerOutputFormatter.GetReadableMessage(expectedJToken, actualJToken, errors));
        }