|
1 | 1 | using System;
|
| 2 | +using System.Collections; |
2 | 3 | using System.Collections.Generic;
|
3 | 4 | using System.Linq;
|
4 | 5 | using System.Reflection;
|
@@ -1083,6 +1084,164 @@ def is_enum_value_defined():
|
1083 | 1084 | Assert.Throws<PythonException>(() => module.InvokeMethod("is_enum_value_defined"));
|
1084 | 1085 | }
|
1085 | 1086 | }
|
| 1087 | + |
| 1088 | + private static TestCaseData[] IDictionaryContainsTestCases => |
| 1089 | + [ |
| 1090 | + new(typeof(TestDictionary<string, string>)), |
| 1091 | + new(typeof(Dictionary<string, string>)), |
| 1092 | + new(typeof(TestKeyValueContainer<string, string>)), |
| 1093 | + new(typeof(DynamicClassDictionary<string, string>)), |
| 1094 | + ]; |
| 1095 | + |
| 1096 | + [TestCaseSource(nameof(IDictionaryContainsTestCases))] |
| 1097 | + public void IDictionaryContainsMethodIsBound(Type dictType) |
| 1098 | + { |
| 1099 | + using var _ = Py.GIL(); |
| 1100 | + |
| 1101 | + var module = PyModule.FromString("IDictionaryContainsMethodIsBound", $@" |
| 1102 | +from clr import AddReference |
| 1103 | +AddReference(""Python.EmbeddingTest"") |
| 1104 | +
|
| 1105 | +from Python.EmbeddingTest import * |
| 1106 | +
|
| 1107 | +def contains(dictionary, key): |
| 1108 | + return key in dictionary |
| 1109 | +"); |
| 1110 | + |
| 1111 | + using var contains = module.GetAttr("contains"); |
| 1112 | + |
| 1113 | + var dictionary = Convert.ChangeType(Activator.CreateInstance(dictType), dictType); |
| 1114 | + var key1 = "key1"; |
| 1115 | + (dictionary as dynamic).Add(key1, "value1"); |
| 1116 | + |
| 1117 | + using var pyDictionary = dictionary.ToPython(); |
| 1118 | + using var pyKey1 = key1.ToPython(); |
| 1119 | + |
| 1120 | + var result = contains.Invoke(pyDictionary, pyKey1).As<bool>(); |
| 1121 | + Assert.IsTrue(result); |
| 1122 | + |
| 1123 | + using var pyKey2 = "key2".ToPython(); |
| 1124 | + result = contains.Invoke(pyDictionary, pyKey2).As<bool>(); |
| 1125 | + Assert.IsFalse(result); |
| 1126 | + } |
| 1127 | + |
| 1128 | + [TestCaseSource(nameof(IDictionaryContainsTestCases))] |
| 1129 | + public void CanCheckIfNoneIsInDictionary(Type dictType) |
| 1130 | + { |
| 1131 | + using var _ = Py.GIL(); |
| 1132 | + |
| 1133 | + var module = PyModule.FromString("CanCheckIfNoneIsInDictionary", $@" |
| 1134 | +from clr import AddReference |
| 1135 | +AddReference(""Python.EmbeddingTest"") |
| 1136 | +
|
| 1137 | +from Python.EmbeddingTest import * |
| 1138 | +
|
| 1139 | +def contains(dictionary, key): |
| 1140 | + return key in dictionary |
| 1141 | +"); |
| 1142 | + |
| 1143 | + using var contains = module.GetAttr("contains"); |
| 1144 | + |
| 1145 | + var dictionary = Convert.ChangeType(Activator.CreateInstance(dictType), dictType); |
| 1146 | + (dictionary as dynamic).Add("key1", "value1"); |
| 1147 | + |
| 1148 | + using var pyDictionary = dictionary.ToPython(); |
| 1149 | + |
| 1150 | + var result = false; |
| 1151 | + Assert.DoesNotThrow(() => result = contains.Invoke(pyDictionary, PyObject.None).As<bool>()); |
| 1152 | + Assert.IsFalse(result); |
| 1153 | + } |
| 1154 | + |
| 1155 | + public class TestDictionary<TKey, TValue> : IDictionary |
| 1156 | + { |
| 1157 | + private readonly Dictionary<TKey, TValue> _data = new(); |
| 1158 | + |
| 1159 | + public object this[object key] { get => ((IDictionary)_data)[key]; set => ((IDictionary)_data)[key] = value; } |
| 1160 | + |
| 1161 | + public bool IsFixedSize => ((IDictionary)_data).IsFixedSize; |
| 1162 | + |
| 1163 | + public bool IsReadOnly => ((IDictionary)_data).IsReadOnly; |
| 1164 | + |
| 1165 | + public ICollection Keys => ((IDictionary)_data).Keys; |
| 1166 | + |
| 1167 | + public ICollection Values => ((IDictionary)_data).Values; |
| 1168 | + |
| 1169 | + public int Count => ((ICollection)_data).Count; |
| 1170 | + |
| 1171 | + public bool IsSynchronized => ((ICollection)_data).IsSynchronized; |
| 1172 | + |
| 1173 | + public object SyncRoot => ((ICollection)_data).SyncRoot; |
| 1174 | + |
| 1175 | + public void Add(object key, object value) |
| 1176 | + { |
| 1177 | + ((IDictionary)_data).Add(key, value); |
| 1178 | + } |
| 1179 | + |
| 1180 | + public void Clear() |
| 1181 | + { |
| 1182 | + ((IDictionary)_data).Clear(); |
| 1183 | + } |
| 1184 | + |
| 1185 | + public bool Contains(object key) |
| 1186 | + { |
| 1187 | + return ((IDictionary)_data).Contains(key); |
| 1188 | + } |
| 1189 | + |
| 1190 | + public void CopyTo(Array array, int index) |
| 1191 | + { |
| 1192 | + ((ICollection)_data).CopyTo(array, index); |
| 1193 | + } |
| 1194 | + |
| 1195 | + public IDictionaryEnumerator GetEnumerator() |
| 1196 | + { |
| 1197 | + return ((IDictionary)_data).GetEnumerator(); |
| 1198 | + } |
| 1199 | + |
| 1200 | + public void Remove(object key) |
| 1201 | + { |
| 1202 | + ((IDictionary)_data).Remove(key); |
| 1203 | + } |
| 1204 | + |
| 1205 | + IEnumerator IEnumerable.GetEnumerator() |
| 1206 | + { |
| 1207 | + return ((IEnumerable)_data).GetEnumerator(); |
| 1208 | + } |
| 1209 | + |
| 1210 | + public bool ContainsKey(TKey key) |
| 1211 | + { |
| 1212 | + return Contains(key); |
| 1213 | + } |
| 1214 | + } |
| 1215 | + |
| 1216 | + public class TestKeyValueContainer<TKey, TValue> |
| 1217 | + where TKey: class |
| 1218 | + where TValue: class |
| 1219 | + { |
| 1220 | + private readonly Dictionary<TKey, TValue> _data = new(); |
| 1221 | + public int Count => _data.Count; |
| 1222 | + public bool ContainsKey(TKey key) |
| 1223 | + { |
| 1224 | + return _data.ContainsKey(key); |
| 1225 | + } |
| 1226 | + public void Add(TKey key, TValue value) |
| 1227 | + { |
| 1228 | + _data.Add(key, value); |
| 1229 | + } |
| 1230 | + } |
| 1231 | + |
| 1232 | + public class DynamicClassDictionary<TKey, TValue> : TestPropertyAccess.DynamicFixture |
| 1233 | + { |
| 1234 | + private readonly Dictionary<TKey, TValue> _data = new(); |
| 1235 | + public int Count => _data.Count; |
| 1236 | + public bool ContainsKey(TKey key) |
| 1237 | + { |
| 1238 | + return _data.ContainsKey(key); |
| 1239 | + } |
| 1240 | + public void Add(TKey key, TValue value) |
| 1241 | + { |
| 1242 | + _data.Add(key, value); |
| 1243 | + } |
| 1244 | + } |
1086 | 1245 | }
|
1087 | 1246 |
|
1088 | 1247 | public class NestedTestParent
|
|
0 commit comments