Код: Выделить всё
assert message == expected_result
isequal=(message==expected_result)
Обратите внимание, что message==expected_result в Python будет сравниваться СОДЕРЖИМОЕ словаря вместо указателя словаря.
После запроса ChatGPT мне сказали изменить код следующим образом:
Код: Выделить всё
Assert.That(message, [Is].EqualTo(expectedResult))
Dim isEqual As Boolean = message.Equals(expectedResult)
Я не могу заставить его выдавать нормальное логическое значение.
А насчет Equals у меня есть сомнения. Возможно, он сможет проверить равенство для простого словаря, но как насчет словаря, содержащего словарь?
Скажем так, имя ключа всегда является строкой.
Поэтому я использую словарь (строки, объекты)
Конечно, я могу перебрать это, просто написав этот код
Код: Выделить всё
Function AreDictionariesEqual(dict1 As Dictionary(Of String, Object), dict2 As Dictionary(Of String, Object)) As Boolean
' Check if both dictionaries are the same reference
If ReferenceEquals(dict1, dict2) Then
Return True
End If
' Check if either dictionary is null
If dict1 Is Nothing OrElse dict2 Is Nothing Then
Return False
End If
' Check if both dictionaries have the same count
If dict1.Count dict2.Count Then
Return False
End If
' Compare each key-value pair
For Each kvp In dict1
Dim key = kvp.Key
Dim value1 = kvp.Value
If Not dict2.ContainsKey(key) Then
Return False
End If
Dim value2 = dict2(key)
' Compare the values depending on their types
If value1 IsNot Nothing AndAlso value2 IsNot Nothing Then
If value1.GetType() Is value2.GetType() Then
' If both values are dictionaries, recurse
If TypeOf value1 Is Dictionary(Of String, Object) AndAlso TypeOf value2 Is Dictionary(Of String, Object) Then
If Not AreDictionariesEqual(DirectCast(value1, Dictionary(Of String, Object)), DirectCast(value2, Dictionary(Of String, Object))) Then
Return False
End If
ElseIf Not value1.Equals(value2) Then
Return False
End If
Else
Return False
End If
ElseIf value1 Is Nothing AndAlso value2 Is Nothing Then
' Both values are null, so they're considered equal
Continue For
Else
Return False
End If
Next
Return True
End Function
Подробнее здесь: https://stackoverflow.com/questions/792 ... his-python