예제 #1
0
        public void HandWrittenTests(string original, string expected, string[] patterns)
        {
            // Arrange
            var originalJson      = JToken.Parse(original);
            var expectedJson      = JToken.Parse(expected);
            var patternCollection = PatternCollection.Parse(patterns);

            Console.WriteLine($"PATTERNS:\n{patterns.MergeLines()}\n");
            Console.WriteLine($"ORIGINAL:\n{originalJson}\n");
            Console.WriteLine($"EXPECTED:\n{expectedJson}\n");

            // Act
            originalJson.Subset(patternCollection);

            // Assert
            Console.WriteLine($"WAS:\n{originalJson}\n");
            Assert.That(JToken.DeepEquals(expectedJson, originalJson));
        }
        /// <summary>
        /// Generates a collection of patterns for the specified <see cref="JContainer"/>.
        /// </summary>
        public PatternCollection GeneratePatterns(JContainer root, int desiredCount, bool skipValues = true)
        {
            var descendants = root.DescendantsAndSelf().ToArray();

            descendants.Shuffle(random);

            if (skipValues)
            {
                descendants = descendants.Where(x => x is JObject || x is JArray).ToArray();
            }

            var patterns = descendants
                           .Select(x => x.Path)
                           .Where(x => !string.IsNullOrWhiteSpace(x))
                           .Take(desiredCount)
                           .Select(x => (random.NextBool() ? "!" : "") + x)
                           .Select(AddWildcards)
                           .ToArray();

            return(PatternCollection.Parse(patterns));
        }