Beispiel #1
0
        public void RebaseUriVisitor_VisitRun_ReplacesBaseUriDictionaryWhenIncorrect()
        {
            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);

            oldRun.Properties = new Dictionary <string, SerializedPropertyInfo>();
            SerializedPropertyInfo oldData = new SerializedPropertyInfo("42", false);

            oldRun.Properties.Add(RebaseUriVisitor.BaseUriDictionaryName, oldData);

            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\src\root"));

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            newRun.Properties.Should().ContainKey(RebaseUriVisitor.BaseUriDictionaryName);

            Dictionary <string, Uri> baseUriDictionary = RebaseUriVisitor.DeserializePropertyDictionary(newRun.Properties[RebaseUriVisitor.BaseUriDictionaryName]);

            baseUriDictionary.Should().ContainKey("SRCROOT");
            baseUriDictionary["SRCROOT"].ShouldBeEquivalentTo(new Uri(@"C:\src\root"));

            newRun.Properties.Should().ContainKey(RebaseUriVisitor.BaseUriDictionaryName + RebaseUriVisitor.IncorrectlyFormattedDictionarySuffix);
            newRun.Properties[RebaseUriVisitor.BaseUriDictionaryName + RebaseUriVisitor.IncorrectlyFormattedDictionarySuffix].ShouldBeEquivalentTo(oldData);
        }
        public bool Equals(SerializedPropertyInfo other)
        {
            if (other == null)
            {
                return(false);
            }

            if (IsString != other.IsString)
            {
                return(false);
            }

            if (!ReferenceEquals(SerializedValue, other.SerializedValue))
            {
                if (SerializedValue == null || other.SerializedValue == null)
                {
                    return(false);
                }

                if (!SerializedValue.Equals(other.SerializedValue))
                {
                    return(false);
                }
            }

            return(true);
        }
        internal static bool TryDeserializePropertyDictionary(SerializedPropertyInfo serializedProperty, out Dictionary <string, Uri> dictionary)
        {
            try
            {
                dictionary = JsonConvert.DeserializeObject <Dictionary <string, Uri> >(serializedProperty.SerializedValue, _settings);

                return(true);
            }
            // Didn't deserialize correctly
            catch (Exception ex)
            {
                if (ex is JsonSerializationException || ex is ArgumentNullException)
                {
                    dictionary = null;
                    return(false);
                }
                throw;
            }
        }
        public void SetPropertiesFrom(IPropertyBagHolderVersionOne other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            // We need the concrete class because the IPropertyBagHolderVersionOne interface
            // doesn't expose the raw Properties array.
            PropertyBagHolderVersionOne otherHolder = other as PropertyBagHolderVersionOne;

            Debug.Assert(otherHolder != null);

            Properties = other.PropertyNames.Count > 0 ? new Dictionary <string, SerializedPropertyInfo>() : null;

            foreach (string propertyName in other.PropertyNames)
            {
                SerializedPropertyInfo otherInfo = otherHolder.Properties[propertyName];
                Properties[propertyName] = new SerializedPropertyInfo(otherInfo.SerializedValue, otherInfo.IsString);
            }
        }
        public void SetProperty <T>(string propertyName, T value)
        {
            if (Properties == null)
            {
                Properties = new Dictionary <string, SerializedPropertyInfo>();
            }

            bool isString = typeof(T) == typeof(string);

            string serializedValue;

            if (value == null)
            {
                serializedValue = NullValue;
            }
            else
            {
                serializedValue = isString
                    ? JsonConvert.ToString(value)
                    : JsonConvert.SerializeObject(value);
            }

            Properties[propertyName] = new SerializedPropertyInfo(serializedValue, isString);
        }
 /// <summary>
 /// Internal as used in testing as a helper.
 /// </summary>
 internal static Dictionary <string, Uri> DeserializePropertyDictionary(SerializedPropertyInfo info)
 {
     return(JsonConvert.DeserializeObject <Dictionary <string, Uri> >(info.SerializedValue, _settings));
 }