Esempio n. 1
0
        public void TestDiffDeserialize()
        {
            //"mod1", "version1", "type1", ModuleStatus.Running, Config1
            //Config1 = new TestConfig("image1");
            //arrange
            Diff   nonEmptyUpdated            = Diff.Create(Module1);
            string nonEmptyUpdatedJson        = "{\"modules\":{\"mod1\":{\"version\":\"version1\",\"type\":\"test\",\"status\":\"running\",\"settings\":{\"image\":\"image1\"},\"restartPolicy\":\"on-unhealthy\",\"configuration\":{\"id\":\"1\",\"version\":\"2\"}}},\"$version\":127}";
            string nonEmptyRemovedJson        = "{\"modules\":{\"module2\": null },\"$version\":127}";
            string nonSupportedTypeModuleJson = "{\"modules\":{\"mod1\":{\"version\":\"version1\",\"type\":\"unknown\",\"status\":\"running\",\"settings\":{\"image\":\"image1\"},\"restartPolicy\":\"on-unhealthy\",\"configuration\":{\"id\":\"1\",\"version\":\"2\"}}},\"$version\":127}";
            string noTypeDiffJson             = "{\"modules\":{\"mod1\":{\"version\":\"version1\",\"status\":\"running\",\"settings\":{\"image\":\"image1\"},\"restartPolicy\":\"on-unhealthy\",\"configuration\":{\"id\":\"1\",\"version\":\"2\"}}},\"$version\":127}";

            var nonEmptyRemoved = new Diff(ImmutableList <IModule> .Empty, new List <string> {
                "module2"
            });

            var serializerInputTable = new Dictionary <string, Type>()
            {
                { "test", typeof(TestModule) }
            };
            var diffSerde = new DiffSerde(serializerInputTable);

            //act
            Diff nonEmptyUpdatedDeserialized = diffSerde.Deserialize(nonEmptyUpdatedJson);
            Diff nonEmptyRemovedDeserialized = diffSerde.Deserialize(nonEmptyRemovedJson);

            //assert
            Assert.Throws <JsonSerializationException>(() => diffSerde.Deserialize(nonSupportedTypeModuleJson));
            Assert.Throws <NotSupportedException>(() => diffSerde.Deserialize <Diff>(nonEmptyUpdatedJson));
            Assert.Throws <JsonSerializationException>(() => diffSerde.Deserialize(noTypeDiffJson));
            Assert.True(nonEmptyUpdatedDeserialized.Equals(nonEmptyUpdated));
            Assert.True(nonEmptyRemovedDeserialized.Equals(nonEmptyRemoved));
        }
        public TestProjectChangeDescription(IProjectRuleSnapshot before, IProjectRuleSnapshot after)
        {
            Before = before;
            After  = after;

            Difference = Diff.Create(before, after);
        }
Esempio n. 3
0
        public void UnchangedFieldsAreNotApplied()
        {
            var a = new Foo {
                X = 1
            };
            var diff = Diff.Create(a, a);

            a.X = 10;
            diff.Apply(ref a);

            Assert.That(a.X, Is.EqualTo(10));
        }
Esempio n. 4
0
        public void UnchangedIntFieldIsNotReported()
        {
            var a = new Foo {
                X = 1
            };
            var b = new Foo {
                X = 1
            };
            var diff = Diff.Create(a, b);

            Assert.That(diff.Changed, Is.EqualTo(0));
        }
Esempio n. 5
0
        public void ChangedFieldsAreApplied()
        {
            var a = new Foo {
                X = 1
            };
            var b = new Foo {
                X = 2
            };
            var diff = Diff.Create(a, b);

            diff.Apply(ref a);
            Assert.That(a.X, Is.EqualTo(2));
        }
Esempio n. 6
0
        public void TestDiffSerialize()
        {
            //arrange
            var serializerInputTable = new Dictionary <string, Type>()
            {
                { "test", typeof(TestModule) }
            };
            var  diffSerde       = new DiffSerde(serializerInputTable);
            Diff nonEmptyUpdated = Diff.Create(Module1);

            //act
            //assert
            Assert.Throws <NotSupportedException>(() => diffSerde.Serialize(nonEmptyUpdated));
        }
Esempio n. 7
0
        /// <summary>
        /// Parses a list of browser names to use.
        /// </summary>
        /// <param name="names">The names to parse.</param>
        /// <param name="log">A log for sending errors to.</param>
        /// <returns>A list of web driver builders.</returns>
        private static IReadOnlyDictionary <string, Func <IWebDriver> > ParseBrowserNames(
            IEnumerable <string> names, ILog log)
        {
            var browsersToUse = new Dictionary <string, Func <IWebDriver> >();

            foreach (var name in names.Distinct())
            {
                if (Drivers.ContainsKey(name))
                {
                    browsersToUse[name] = Drivers[name];
                }
                else
                {
                    // Try to guess what the user meant.
                    var suggestion = NameSuggestion.SuggestName(name, Drivers.Keys);
                    if (suggestion == null)
                    {
                        // Log an error if we couldn't find a reasonable guess.
                        log.Log(
                            new Pixie.LogEntry(
                                Severity.Error,
                                "unknown browser",
                                Quotation.QuoteEvenInBold(
                                    "specified browser ",
                                    name,
                                    " is not a known browser.")));
                    }
                    else
                    {
                        // Give the user a hand otherwise.
                        var diff = Diff.Create(name, suggestion);
                        log.Log(
                            new Pixie.LogEntry(
                                Severity.Error,
                                "unknown browser",
                                Quotation.QuoteEvenInBold(
                                    "specified browser ",
                                    TextDiff.RenderDeletions(diff),
                                    " is not a known browser; did you mean ",
                                    TextDiff.RenderInsertions(diff),
                                    "?")));
                    }
                }
            }
            return(browsersToUse);
        }
Esempio n. 8
0
        public void TestEquals()
        {
            Diff nonEmptyUpdated = Diff.Create(Module1);
            var  nonEmptyRemoved = new Diff(ImmutableList <IModule> .Empty, new List <string> {
                "module2"
            });
            Diff   alsoNonEmptyDiff = nonEmptyUpdated;
            object nonEmptyUpdatedObjectSameReference = nonEmptyUpdated;

            Assert.False(nonEmptyUpdated.Equals(null));
            Assert.True(nonEmptyUpdated.Equals(alsoNonEmptyDiff));
            Assert.False(nonEmptyUpdated.Equals(new object()));
            Assert.True(nonEmptyUpdated.Equals(nonEmptyUpdatedObjectSameReference));

            Assert.False(Diff.Empty.Equals(nonEmptyUpdated));
            Assert.False(Diff.Empty.Equals(nonEmptyRemoved));
            Assert.False(nonEmptyUpdated.Equals(nonEmptyRemoved));

            Assert.Equal(Module1, Module1A);
            Assert.True(nonEmptyUpdated.Equals(Diff.Create(Module1A)));
        }
Esempio n. 9
0
        public static (List <T>, IEnumerable <Diff <T> >) MergeWithDiffs <T>(this IReadOnlyList <T> @this, IReadOnlyList <T> items, int offset, int length, Comparer <T> comparer = null)
        {
            if (@this == null)
            {
                throw new ArgumentNullException(nameof(@this));
            }
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }
            if (length < 1)
            {
                throw new ArgumentNullException(nameof(length), $"length must be > 0, but was {length}.");
            }
            if (offset + length > items.Count)
            {
                throw new ArgumentNullException(nameof(offset) + " and " + nameof(length), $"offset ({offset}) + length ({length}) (= {offset + length}) must be <= items.Count ({items.Count}).");
            }
            comparer = comparer ?? Comparer <T> .Default;
            var result = new List <T>(@this.Count + items.Count);
            var diffs = new List <Diff <T> >(items.Count);
            int itemsIndex = offset, thisIndex = 0;
            var newEndIndex = itemsIndex + length - 1;

            while (itemsIndex <= newEndIndex && thisIndex < @this.Count)
            {
                var item       = items[itemsIndex];
                var thisItem   = @this[thisIndex];
                var comparison = comparer.Compare(thisItem, item);
                if (comparison < 0)
                {
                    result.Add(thisItem);
                    thisIndex++;
                    continue;
                }
                if (comparison > 0)
                {
                    result.Add(item);
                    itemsIndex++;
                    continue;
                }
                Diff <T> diff = Diff <T> .Create(thisItem, item);

                if (diff != null)
                {
                    diffs.Add(diff);
                }
                result.Add(item);
                thisIndex++;
                itemsIndex++;
            }
            if (thisIndex < @this.Count)
            {
                result.AddRange(@this.Tail(thisIndex));
            }
            else if (itemsIndex <= newEndIndex)
            {
                result.AddRange(items.Slice(itemsIndex, newEndIndex - itemsIndex + 1));
            }
            return(result, diffs.Count > 0 ? diffs : null);
        }