Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarfgp committed Jul 30, 2024
1 parent d21a32b commit 1251e35
Showing 1 changed file with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ let x = { new _ with member this.MyMember() = 42 }
(Error 772, Line 5, Col 11, Line 5, Col 16, "'new' must be used with a named type")
]

[<Fact>]
let ``Object expression cannot implement unnamed interface preview`` () =
Fsx """
type IFirst =
abstract member MyMember: unit -> int
let x = { new _ with member this.MyMember() = 42 }
"""
|> withLangVersionPreview
|> typecheck
|> shouldFail
|> withDiagnostics [
(Error 772, Line 5, Col 11, Line 5, Col 16, "'new' must be used with a named type")
]

[<Fact>]
let ``Object expression implementing an interface without members`` () =
Fsx """
Expand All @@ -34,6 +49,17 @@ let implementer() ={ new IFirst }
|> typecheck
|> shouldSucceed

[<Fact>]
let ``Object expression implementing an interface without members preview`` () =
Fsx """
type IFirst = interface end
let implementer() ={ new IFirst }
"""
|> withLangVersionPreview
|> typecheck
|> shouldSucceed

[<Fact>]
let ``Parameterized object expression implementing an interface with members`` () =
Fsx """
Expand All @@ -57,6 +83,30 @@ let implSomeDU someDu =
|> typecheck
|> shouldSucceed

[<Fact>]
let ``Parameterized object expression implementing an interface with members preview`` () =
Fsx """
type AsString =
abstract member asString: unit -> string
type SomeDu =
| A of int
| B of string
| C of float
let implSomeDU someDu =
{ new AsString with
member self.asString () =
match someDu with
| A a -> string a
| B b -> b
| C c -> string c }
"""
|> withLangVersionPreview
|> typecheck
|> shouldSucceed


[<Fact>]
let ``Object expression can not implement an abstract class having no abstract members.`` () =
Fsx """
Expand All @@ -76,7 +126,25 @@ let foo2 = { new Foo() with member __.ToString() = base.ToString() }
|> withDiagnostics [
(Error 738, Line 5, Col 17, Line 5, Col 20, "Invalid object expression. Objects without overrides or interfaces should use the expression form 'new Type(args)' without braces.");
(Error 759, Line 7, Col 12, Line 7, Col 21, "Instances of this type cannot be created since it has been marked abstract or not all methods have been given implementations. Consider using an object expression '{ new ... with ... }' instead.")
]
]

// FIXME This tests should succeed
[<Fact>]
let ``Object expression can not implement an abstract class and interface having no abstract members.`` () =
Fsx """
type IFirst = interface end
[<AbstractClass>]
type MyClass() = class end
{ new MyClass() with
member x.ToString() = "OK"
interface IFirst } |> ignore
"""
|> withLangVersion80
|> typecheck
|> shouldSucceed

[<Fact>]
let ``Object expression can implement an abstract class having no abstract members.`` () =
Expand Down Expand Up @@ -122,6 +190,31 @@ let y = { new C() with
(Error 365, Line 9, Col 20, Line 10, Col 60, "No implementation was given for 'abstract B.M: string -> unit'")
(Error 365, Line 13, Col 9, Line 14, Col 49, "No implementation was given for 'abstract B.M: string -> unit'")
]

[<Fact>]
let ``Error when object expression does not implement all abstract members of the abstract class preview`` () =
Fsx """
[<AbstractClass>]
type B() =
abstract M : int -> float
abstract M : string -> unit
and [<AbstractClass>]
C() =
inherit B()
static let v = { new C() with
member x.M(a:int) : float = 1.0 }
default x.M(a:int) : float = 1.0
let y = { new C() with
member x.M(a:int) : float = 1.0 }
"""
|> withLangVersionPreview
|> typecheck
|> shouldFail
|> withDiagnostics [
(Error 365, Line 9, Col 20, Line 10, Col 60, "No implementation was given for 'abstract B.M: string -> unit'")
(Error 365, Line 13, Col 9, Line 14, Col 49, "No implementation was given for 'abstract B.M: string -> unit'")
]

[<Fact>]
let ``Error when object expression does not implement all abstract members of a generic abstract class`` () =
Expand All @@ -146,6 +239,29 @@ let f() = { new BaseHashtable<_,_>(2) with
(Error 359, Line 10, Col 11, Line 13, Col 12, "More than one override implements 'Next: StrongToWeakEntry<'a> array -> int when 'a: not struct'")
]

[<Fact>]
let ``Error when object expression does not implement all abstract members of a generic abstract class preview`` () =
Fsx """
[<AbstractClass>]
type BaseHashtable<'Entry, 'Key>(initialCapacity) =
abstract member Next : entries : 'Entry array -> int
[<Struct>]
type StrongToWeakEntry<'Value when 'Value : not struct> =
val mutable public next : int
let f() = { new BaseHashtable<_,_>(2) with
override this.Next (entries:StrongToWeakEntry<_> array) = 1
override this.Next entries = 1
}
"""
|> withLangVersionPreview
|> typecheck
|> shouldFail
|> withDiagnostics [
(Error 359, Line 10, Col 11, Line 13, Col 12, "More than one override implements 'Next: StrongToWeakEntry<'a> array -> int when 'a: not struct'")
]

[<Fact>]
let ``Object expression can not implementing an interface when it contains a method with no types that can refer to the type for which the implementation is being used`` () =
Fsx """
Expand Down

0 comments on commit 1251e35

Please sign in to comment.