|
1 | 1 | using System; |
| 2 | +using System.Collections; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Reflection; |
@@ -1003,6 +1004,132 @@ def call(instance): |
1003 | 1004 | } |
1004 | 1005 |
|
1005 | 1006 | #endregion |
| 1007 | + |
| 1008 | + private static TestCaseData[] IDictionaryContainsTestCases => new TestCaseData[] |
| 1009 | + { |
| 1010 | + new(typeof(TestDictionary<string, string>)), |
| 1011 | + new(typeof(Dictionary<string, string>)), |
| 1012 | + }; |
| 1013 | + |
| 1014 | + [TestCaseSource(nameof(IDictionaryContainsTestCases))] |
| 1015 | + public void IDictionaryContainsMethodIsBound(Type dictType) |
| 1016 | + { |
| 1017 | + using var _ = Py.GIL(); |
| 1018 | + |
| 1019 | + var module = PyModule.FromString("IDictionaryContainsMethodIsBound", $@" |
| 1020 | +from clr import AddReference |
| 1021 | +AddReference(""Python.EmbeddingTest"") |
| 1022 | +
|
| 1023 | +from Python.EmbeddingTest import * |
| 1024 | +
|
| 1025 | +def contains(dictionary, key): |
| 1026 | + return key in dictionary |
| 1027 | +"); |
| 1028 | + |
| 1029 | + using var contains = module.GetAttr("contains"); |
| 1030 | + |
| 1031 | + var dictionary = (Activator.CreateInstance(dictType) as IDictionary)!; |
| 1032 | + var key1 = "key1"; |
| 1033 | + dictionary.Add(key1, "value1"); |
| 1034 | + |
| 1035 | + using var pyDictionary = dictionary.ToPython(); |
| 1036 | + using var pyKey1 = key1.ToPython(); |
| 1037 | + |
| 1038 | + var result = contains.Invoke(pyDictionary, pyKey1).As<bool>(); |
| 1039 | + Assert.IsTrue(result); |
| 1040 | + |
| 1041 | + using var pyKey2 = "key2".ToPython(); |
| 1042 | + result = contains.Invoke(pyDictionary, pyKey2).As<bool>(); |
| 1043 | + Assert.IsFalse(result); |
| 1044 | + } |
| 1045 | + |
| 1046 | + [TestCaseSource(nameof(IDictionaryContainsTestCases))] |
| 1047 | + public void CanCheckIfNoneIsInDictionary(Type dictType) |
| 1048 | + { |
| 1049 | + using var _ = Py.GIL(); |
| 1050 | + |
| 1051 | + var module = PyModule.FromString("CanCheckIfNoneIsInDictionary", $@" |
| 1052 | +from clr import AddReference |
| 1053 | +AddReference(""Python.EmbeddingTest"") |
| 1054 | +
|
| 1055 | +from Python.EmbeddingTest import * |
| 1056 | +
|
| 1057 | +def contains(dictionary, key): |
| 1058 | + return key in dictionary |
| 1059 | +"); |
| 1060 | + |
| 1061 | + using var contains = module.GetAttr("contains"); |
| 1062 | + |
| 1063 | + var dictionary = (Activator.CreateInstance(dictType) as IDictionary)!; |
| 1064 | + dictionary.Add("key1", "value1"); |
| 1065 | + |
| 1066 | + using var pyDictionary = dictionary.ToPython(); |
| 1067 | + |
| 1068 | + var result = false; |
| 1069 | + Assert.DoesNotThrow(() => result = contains.Invoke(pyDictionary, PyObject.None).As<bool>()); |
| 1070 | + Assert.IsFalse(result); |
| 1071 | + } |
| 1072 | + |
| 1073 | + public class TestDictionary<TValue, TKey> : IDictionary |
| 1074 | + { |
| 1075 | + private readonly Dictionary<TValue, TKey> _data = new(); |
| 1076 | + |
| 1077 | + public object this[object key] { get => ((IDictionary)_data)[key]; set => ((IDictionary)_data)[key] = value; } |
| 1078 | + |
| 1079 | + public bool IsFixedSize => ((IDictionary)_data).IsFixedSize; |
| 1080 | + |
| 1081 | + public bool IsReadOnly => ((IDictionary)_data).IsReadOnly; |
| 1082 | + |
| 1083 | + public ICollection Keys => ((IDictionary)_data).Keys; |
| 1084 | + |
| 1085 | + public ICollection Values => ((IDictionary)_data).Values; |
| 1086 | + |
| 1087 | + public int Count => ((ICollection)_data).Count; |
| 1088 | + |
| 1089 | + public bool IsSynchronized => ((ICollection)_data).IsSynchronized; |
| 1090 | + |
| 1091 | + public object SyncRoot => ((ICollection)_data).SyncRoot; |
| 1092 | + |
| 1093 | + public void Add(object key, object value) |
| 1094 | + { |
| 1095 | + ((IDictionary)_data).Add(key, value); |
| 1096 | + } |
| 1097 | + |
| 1098 | + public void Clear() |
| 1099 | + { |
| 1100 | + ((IDictionary)_data).Clear(); |
| 1101 | + } |
| 1102 | + |
| 1103 | + public bool Contains(object key) |
| 1104 | + { |
| 1105 | + return ((IDictionary)_data).Contains(key); |
| 1106 | + } |
| 1107 | + |
| 1108 | + public void CopyTo(Array array, int index) |
| 1109 | + { |
| 1110 | + ((ICollection)_data).CopyTo(array, index); |
| 1111 | + } |
| 1112 | + |
| 1113 | + public IDictionaryEnumerator GetEnumerator() |
| 1114 | + { |
| 1115 | + return ((IDictionary)_data).GetEnumerator(); |
| 1116 | + } |
| 1117 | + |
| 1118 | + public void Remove(object key) |
| 1119 | + { |
| 1120 | + ((IDictionary)_data).Remove(key); |
| 1121 | + } |
| 1122 | + |
| 1123 | + IEnumerator IEnumerable.GetEnumerator() |
| 1124 | + { |
| 1125 | + return ((IEnumerable)_data).GetEnumerator(); |
| 1126 | + } |
| 1127 | + |
| 1128 | + public bool ContainsKey(TKey key) |
| 1129 | + { |
| 1130 | + return Contains(key); |
| 1131 | + } |
| 1132 | + } |
1006 | 1133 | } |
1007 | 1134 |
|
1008 | 1135 | public class NestedTestParent |
|
0 commit comments