Skip to content

Commit

Permalink
Fix comparaison between optional list when one is None
Browse files Browse the repository at this point in the history
Fix #3617
  • Loading branch information
MangelMaxime committed Nov 28, 2023
1 parent 8ee247a commit 4725aea
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

#### Python

* Fix #3617: Fix comparaison between list option when one is None

## 4.6.0 - 2023-11-27

### Changed
Expand Down
12 changes: 12 additions & 0 deletions src/fable-library-py/fable_library/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ def equals(a: Any, b: Any) -> bool:
if a is b:
return True

# Check for NoneTypes (ex Some [1] = None)
match (a, b):
# Don't test (None, None) here, because a is b already covers that
# case (None, None):
# return True
case (None, _):
return False
case (_, None):
return False
case (_, _):
pass

if is_array_like(a):
return equal_arrays(a, b)

Expand Down
12 changes: 12 additions & 0 deletions src/quicktest-py/quicktest.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ let main argv =
// use file = builtins.``open``(StringPath "data.txt")
// file.read() |> printfn "File contents: %s"

let res = Some [ 1 ] = None
// Some 1 = None
// Some 1 = None
// equal true (Some [| "1"; "2" |] = None)
// let xs3 = Some ( 1, 2, 4 )

// equal false (xs3 = None)


printfn $"Result: {res}"


0
107 changes: 107 additions & 0 deletions tests/Python/TestComparison.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ let ``test Typed array equality works`` () =
equal false (xs1 <> xs2)
equal true (xs1 <> xs4)

[<Fact>]
let ``test Typed array option equality works`` () =
let xs1 = Some [| 1; 2; 3 |]
let xs2 = Some [| 1; 2; 3 |]
let xs3 = Some [| 1; 2; 4 |]
let xs4 = Some [| 1; 2 |]
let xs5 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal false (xs1 = xs4)
equal true (xs1 <> xs4)
equal false (xs1 = xs5)
equal true (xs1 <> xs5)
equal true (xs5 = None)
equal false (xs5 <> None)

[<Fact>]
let ``test Array equality works`` () =
let xs1 = [| "1"; "2"; "3" |]
Expand All @@ -93,6 +111,24 @@ let ``test Array equality works`` () =
equal false (xs1 <> xs2)
equal true (xs1 <> xs4)

[<Fact>]
let ``test Array option equality works`` () =
let xs1 = Some [| "1"; "2"; "3" |]
let xs2 = Some [| "1"; "2"; "3" |]
let xs3 = Some [| "1"; "2"; "4" |]
let xs4 = Some [| "1"; "2" |]
let xs5 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal false (xs1 = xs4)
equal true (xs1 <> xs4)
equal false (xs1 = xs5)
equal true (xs1 <> xs5)
equal true (xs5 = None)
equal false (xs5 <> None)

[<Fact>]
let ``test Tuple equality works`` () =
let xs1 = ( 1, 2, 3 )
Expand All @@ -103,6 +139,21 @@ let ``test Tuple equality works`` () =
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)

[<Fact>]
let ``test Tuple option equality works``() =
let xs1 = Some ( 1, 2, 3 )
let xs2 = Some ( 1, 2, 3 )
let xs3 = Some ( 1, 2, 4 )
let xs5 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal false (xs1 = xs5)
equal true (xs1 <> xs5)
equal true (xs5 = None)
equal false (xs5 <> None)

[<Fact>]
let ``test List equality works`` () =
let xs1 = [ 1; 2; 3 ]
Expand All @@ -113,6 +164,24 @@ let ``test List equality works`` () =
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)

[<Fact>]
let ``test List option equality works``() =
let xs1 = Some [ 1; 2; 3 ]
let xs2 = Some [ 1; 2; 3 ]
let xs3 = Some [ 1; 2; 4 ]
let xs4 = Some [ 1; 2; 3; 1 ]
let xs5 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal false (xs1 = xs4)
equal true (xs1 <> xs4)
equal false (xs1 = xs5)
equal true (xs1 <> xs5)
equal true (xs5 = None)
equal false (xs5 <> None)

[<Fact>]
let ``test Set equality works`` () =
let xs1 = Set [ 1; 2; 3 ]
Expand All @@ -127,6 +196,25 @@ let ``test Set equality works`` () =
equal true (xs1 = xs4)
equal false (xs1 <> xs5)

[<Fact>]
let ``test Set option equality works`` () =
let xs1 = Some (Set [ 1; 2; 3 ])
let xs2 = Some (Set [ 1; 2; 3 ])
let xs3 = Some (Set [ 1; 2; 4 ])
let xs4 = Some (Set [ 3; 2; 1 ])
let xs5 = Some (Set [ 1; 2; 3; 1 ])
let xs6 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal true (xs1 = xs4)
equal false (xs1 <> xs5)
equal false (xs1 = xs6)
equal true (xs1 <> xs6)
equal true (xs6 = None)
equal false (xs6 <> None)

[<Fact>]
let ``test Map equality works`` () =
let xs1 = Map [ ("a", 1); ("b", 2); ("c", 3) ]
Expand All @@ -139,6 +227,25 @@ let ``test Map equality works`` () =
equal false (xs1 <> xs2)
equal true (xs1 = xs4)

[<Fact>]
let ``test Map option equality works`` () =
let xs1 = Some (Map [ ("a", 1); ("b", 2); ("c", 3) ])
let xs2 = Some (Map [ ("a", 1); ("b", 2); ("c", 3) ])
let xs3 = Some (Map [ ("a", 1); ("b", 2); ("c", 4) ])
let xs4 = Some (Map [ ("c", 3); ("b", 2); ("a", 1) ])
let xs5 = Some (Map [ ("a", 1); ("b", 2); ("c", 3); ("d", 1) ])
let xs6 = None
equal true (xs1 = xs2)
equal false (xs1 = xs3)
equal true (xs1 <> xs3)
equal false (xs1 <> xs2)
equal true (xs1 = xs4)
equal true (xs1 <> xs5)
equal false (xs1 = xs6)
equal true (xs1 <> xs6)
equal true (xs6 = None)
equal false (xs6 <> None)

[<Fact>]
let ``test Union equality works`` () =
let u1 = A 2
Expand Down

0 comments on commit 4725aea

Please sign in to comment.