Skip to content

Commit

Permalink
fix hash function failing for null property values
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Oct 16, 2024
1 parent 99096ef commit 36c543f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/DynamicObj/HashCodes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ let hashDateTime (dt : System.DateTime) : int =


let hash obj =
obj.GetHashCode()
if obj = null then
0
else
obj.GetHashCode()

let boxHashOption (a: 'a option) : obj =
if a.IsSome then a.Value.GetHashCode() else (0).GetHashCode()
Expand Down
15 changes: 15 additions & 0 deletions tests/DynamicObject.Tests/DynamicObjs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,21 @@ let tests_GetHashCode = testList "GetHashCode" [
b2.SetProperty("c", 2)
a2.SetProperty("b", b2)
Expect.equal (a.GetHashCode()) (a2.GetHashCode()) "Values should be equal"

testCase "null Value same key" <| fun _ ->
let a = DynamicObj()
a.SetProperty("b", null)
let b = DynamicObj()
b.SetProperty("b", null)
Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Values should be equal"

testCase "null Value different key" <| fun _ ->
let a = DynamicObj()
a.SetProperty("b", null)
let b = DynamicObj()
a.SetProperty("c", null)
Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Values should not be equal"

]

let main = testList "DynamicObj (Class)" [
Expand Down
7 changes: 7 additions & 0 deletions tests/DynamicObject.Tests/Inheritance.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ let tests_remove = testList "Remove" [

Expect.equal p.FirstName null "Static property should "

testCase "Remove Static HashCodeDoesNotFail" <| fun _ ->
let p = Person("123","John")

p.RemoveProperty("FirstName") |> ignore

p.GetHashCode() |> ignore

testCase "Remove Static Immutable" <| fun _ ->
let p = Person("123","John")
let f = fun () -> p.RemoveProperty("Id") |> ignore
Expand Down

0 comments on commit 36c543f

Please sign in to comment.