/src/installer/tests/HostActivation.Tests/NativeHosting/HostContext.PropertyCompatibilityTestData.cs

https://github.com/dotnet/runtime · C# · 154 lines · 129 code · 11 blank · 14 comment · 3 complexity · 4c779c4c7cc4ed73daaa4319a120e7d0 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System;
  4. using System.Collections.Generic;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
  8. {
  9. public partial class HostContext : IClassFixture<HostContext.SharedTestState>
  10. {
  11. public class PropertyTestData : IXunitSerializable
  12. {
  13. public string Name;
  14. public string NewValue;
  15. public string ExistingValue;
  16. void IXunitSerializable.Deserialize(IXunitSerializationInfo info)
  17. {
  18. Name = info.GetValue<string>("Name");
  19. NewValue = info.GetValue<string>("NewValue");
  20. ExistingValue = info.GetValue<string>("ExistingValue");
  21. }
  22. void IXunitSerializable.Serialize(IXunitSerializationInfo info)
  23. {
  24. info.AddValue("Name", Name);
  25. info.AddValue("NewValue", NewValue);
  26. info.AddValue("ExistingValue", ExistingValue);
  27. }
  28. public override string ToString()
  29. {
  30. return $"Name: {Name}, NewValue: {NewValue}, ExistingValue: {ExistingValue}";
  31. }
  32. }
  33. private static List<PropertyTestData[]> GetPropertiesTestData(
  34. string propertyName1,
  35. string propertyValue1,
  36. string propertyName2,
  37. string propertyValue2)
  38. {
  39. var list = new List<PropertyTestData[]>()
  40. {
  41. // No additional properties
  42. new PropertyTestData[] { },
  43. // Match
  44. new PropertyTestData[]
  45. {
  46. new PropertyTestData { Name = propertyName1, NewValue = propertyValue1, ExistingValue = propertyValue1 }
  47. },
  48. // Substring
  49. new PropertyTestData[]
  50. {
  51. new PropertyTestData { Name = propertyName1, NewValue = propertyValue1.Remove(propertyValue1.Length - 1), ExistingValue = propertyValue1 }
  52. },
  53. // Different in case only
  54. new PropertyTestData[]
  55. {
  56. new PropertyTestData { Name = propertyName1, NewValue = propertyValue1.ToLower(), ExistingValue = propertyValue1 }
  57. },
  58. // Different value
  59. new PropertyTestData[]
  60. {
  61. new PropertyTestData { Name = propertyName1, NewValue = "NEW_PROPERTY_VALUE", ExistingValue = propertyValue1 }
  62. },
  63. // Different value (empty)
  64. new PropertyTestData[]
  65. {
  66. new PropertyTestData { Name = propertyName1, NewValue = string.Empty, ExistingValue = propertyValue1 }
  67. },
  68. // New property
  69. new PropertyTestData[]
  70. {
  71. new PropertyTestData { Name = "NEW_PROPERTY_NAME", NewValue = "NEW_PROPERTY_VALUE", ExistingValue = null }
  72. },
  73. // Match, new property
  74. new PropertyTestData[]
  75. {
  76. new PropertyTestData { Name = propertyName1, NewValue = propertyValue1, ExistingValue = propertyValue1 },
  77. new PropertyTestData { Name = "NEW_PROPERTY_NAME", NewValue = "NEW_PROPERTY_VALUE", ExistingValue = null }
  78. },
  79. // One match, one different
  80. new PropertyTestData[]
  81. {
  82. new PropertyTestData { Name = propertyName1, NewValue = propertyValue1, ExistingValue = propertyValue1 },
  83. new PropertyTestData { Name = propertyName2, NewValue = "NEW_PROPERTY_VALUE", ExistingValue = propertyValue2 }
  84. },
  85. // Both different
  86. new PropertyTestData[]
  87. {
  88. new PropertyTestData { Name = propertyName1, NewValue = "NEW_PROPERTY_VALUE", ExistingValue = propertyValue1 },
  89. new PropertyTestData { Name = propertyName2, NewValue = "NEW_PROPERTY_VALUE", ExistingValue = propertyValue2 }
  90. },
  91. };
  92. if (propertyValue2 != null)
  93. {
  94. list.Add(
  95. // Both match
  96. new PropertyTestData[]
  97. {
  98. new PropertyTestData { Name = propertyName1, NewValue = propertyValue1, ExistingValue = propertyValue1 },
  99. new PropertyTestData { Name = propertyName2, NewValue = propertyValue2, ExistingValue = propertyValue2 }
  100. });
  101. list.Add(
  102. // Both match, new property
  103. new PropertyTestData[]
  104. {
  105. new PropertyTestData { Name = propertyName1, NewValue = propertyValue1, ExistingValue = propertyValue1 },
  106. new PropertyTestData { Name = propertyName2, NewValue = propertyValue2, ExistingValue = propertyValue2 },
  107. new PropertyTestData { Name = "NEW_PROPERTY_NAME", NewValue = "NEW_PROPERTY_VALUE", ExistingValue = null }
  108. });
  109. }
  110. return list;
  111. }
  112. public static IEnumerable<object[]> GetPropertyCompatibilityTestData(string scenario, bool hasSecondProperty)
  113. {
  114. List<PropertyTestData[]> properties;
  115. switch (scenario)
  116. {
  117. case Scenario.ConfigMultiple:
  118. properties = GetPropertiesTestData(
  119. SharedTestState.ConfigPropertyName,
  120. SharedTestState.ConfigPropertyValue,
  121. SharedTestState.ConfigMultiPropertyName,
  122. hasSecondProperty ? SharedTestState.ConfigMultiPropertyValue : null);
  123. break;
  124. case Scenario.Mixed:
  125. case Scenario.NonContextMixed:
  126. properties = GetPropertiesTestData(
  127. SharedTestState.AppPropertyName,
  128. SharedTestState.AppPropertyValue,
  129. SharedTestState.AppMultiPropertyName,
  130. hasSecondProperty ? SharedTestState.AppMultiPropertyValue : null);
  131. break;
  132. default:
  133. throw new Exception($"Unexpected scenario: {scenario}");
  134. }
  135. var list = new List<object[]> ();
  136. foreach (var p in properties)
  137. {
  138. list.Add(new object[] { scenario, hasSecondProperty, p });
  139. }
  140. return list;
  141. }
  142. }
  143. }