Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/fsharp/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1336,3 +1336,4 @@ descriptionUnavailable,"(description unavailable...)"
3180,abImplicitHeapAllocation,"The mutable local '%s' is implicitly allocated as a reference cell because it has been captured by a closure. This warning is for informational purposes only to indicate where implicit allocations are performed."
estApplyStaticArgumentsForMethodNotImplemented,"A type provider implemented GetStaticParametersForMethod, but ApplyStaticArgumentsForMethod was not implemented or invalid"
3181,etErrorApplyingStaticArgumentsToMethod,"An error occured applying the static arguments to a provided method"
3182,nameofNotPermitted,"This expression does not have a name."
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<Compile Include="FSharp.Core\Microsoft.FSharp.Reflection\FSharpReflection.fs" />
<Compile Include="FSharp.Core\Microsoft.FSharp.Quotations\FSharpQuotations.fs" />
<Compile Include="TypeForwarding.fs" />
<Compile Include="NameOfTests.fs" />
<Compile Include="NUnitFrameworkShims.fs" Condition="'$(TargetFramework)' == 'sl3-wp'" />
<Compile Include="SurfaceArea.4.0.fs" Condition="'$(TargetFramework)' == 'net40'"/>
</ItemGroup>
Expand Down
202 changes: 202 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/NameOfTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace FSharp.Core.Unittests
open System
open NUnit.Framework

[<TestFixture>]
type BasicNameOfTests() =
let localConstant = 23
member this.MemberMethod() = 0
member this.MemberProperty = this.MemberMethod()
static member StaticMethod() = 0
static member StaticProperty = BasicNameOfTests.StaticMethod()

[<Test>]
member this.``local variable name lookup`` () =
let a = 0
let result = nameof a
Assert.AreEqual("a",result)
Assert.AreEqual("result",nameof result)

[<Test>]
member this.``local int function name`` () =
let myFunction x = 0 * x
let b = nameof myFunction
Assert.AreEqual("myFunction",b)

[<Test>]
member this.``local curried function name`` () =
let curriedFunction x y = x * y
let b = nameof curriedFunction
Assert.AreEqual("curriedFunction",b)

[<Test>]
member this.``local tupled function name`` () =
let tupledFunction(x,y) = x * y
let b = nameof tupledFunction
Assert.AreEqual("tupledFunction",b)

[<Test>]
member this.``local unit function name`` () =
let myFunction() = 1
let b = nameof(myFunction)
Assert.AreEqual("myFunction",b)

[<Test>]
member this.``local function paarameter name`` () =
let myFunction parameter1 = nameof parameter1

Assert.AreEqual("parameter1",myFunction "x")
Assert.AreEqual("parameter1",myFunction "x")

[<Test>]
member this.``can get name from inside a local function (needs to be let rec)`` () =
let rec myLocalFunction x =
let z = 2 * x
nameof myLocalFunction + " " + z.ToString()

Assert.AreEqual("myLocalFunction 46",myLocalFunction 23)
Assert.AreEqual("myLocalFunction 50",myLocalFunction 25)

[<Test>]
member this.CanGetNameFromInsideAMember () =
let b = nameof(this.CanGetNameFromInsideAMember)
Assert.AreEqual("CanGetNameFromInsideAMember",b)

[<Test>]
member this.``member function name`` () =
let b = nameof(this.MemberMethod)
Assert.AreEqual("MemberMethod",b)

[<Test>]
member this.``member function which is defined below`` () =
let b = nameof(this.MemberMethodDefinedBelow)
Assert.AreEqual("MemberMethodDefinedBelow",b)

member this.MemberMethodDefinedBelow(x,y) = x * y

[<Test>]
member this.``static member function name`` () =
let b = nameof(BasicNameOfTests.StaticMethod)
Assert.AreEqual("StaticMethod",b)

[<Test>]
member this.``class member lookup`` () =
let b = nameof(localConstant)
Assert.AreEqual("localConstant",b)

[<Test>]
member this.``member property name`` () =
let b = nameof(this.MemberProperty)
Assert.AreEqual("MemberProperty",b)

[<Test>]
member this.``static property name`` () =
let b = nameof(BasicNameOfTests.StaticProperty)
Assert.AreEqual("StaticProperty",b)

member this.get_XYZ() = 1

[<Test>]
member this.``member method starting with get_`` () =
let b = nameof(this.get_XYZ)
Assert.AreEqual("get_XYZ",b)

static member get_SXYZ() = 1

[<Test>]
member this.``static method starting with get_`` () =
let b = nameof(BasicNameOfTests.get_SXYZ)
Assert.AreEqual("get_SXYZ",b)

[<Test>]
member this.``nameof local property with encapsulated name`` () =
let ``local property with encapsulated name and %.f`` = 0
let b = nameof(``local property with encapsulated name and %.f``)
Assert.AreEqual("local property with encapsulated name and %.f",b)

[<TestFixture>]
type MethodGroupTests() =
member this.MethodGroup() = ()
member this.MethodGroup(i:int) = ()

[<Test>]
member this.``method group name lookup`` () =
let b = nameof(this.MethodGroup)
Assert.AreEqual("MethodGroup",b)

[<TestFixture>]
type FrameworkMethodTests() =
[<Test>]
member this.``library function name`` () =
let b = nameof(List.map)
Assert.AreEqual("Map",b)

[<Test>]
member this.``static class function name`` () =
let b = nameof(Tuple.Create)
Assert.AreEqual("Create",b)

[<TestFixture>]
type OperatorNameTests() =

[<Test>]
member this.``lookup name of typeof operator`` () =
let b = nameof(typeof<int>)
Assert.AreEqual("TypeOf",b)

[<Test>]
member this.``lookup name of + operator`` () =
let b = nameof(+)
Assert.AreEqual("op_Addition",b)

[<Test>]
member this.``lookup name of |> operator`` () =
let a = nameof(|>)
Assert.AreEqual("op_PipeRight",a)
let b = nameof(op_PipeRight)
Assert.AreEqual("op_PipeRight",b)

[<Test>]
member this.``lookup name of nameof operator`` () =
let b = nameof(nameof)
Assert.AreEqual("NameOf",b)

[<TestFixture>]
type PatternMatchingOfOperatorNameTests() =
member this.Method1(i:int) = ()

[<Test>]
member this.``use it as a match case guard`` () =
match "Method1" with
| x when x = nameof(this.Method1) -> ()
| _ -> Assert.Fail("not expected")

[<TestFixture>]
type NameOfOperatorInQuotations() =
[<Test>]
member this.``use it in a quotation`` () =
let q =
<@
let f(x:int) = nameof x
f 20
@>
()

[<TestFixture>]
type NameOfOperatorForGenerics() =
[<Test>]
member this.``use it in a generic function`` () =
let fullyGeneric x = x
let b = nameof(fullyGeneric)
Assert.AreEqual("fullyGeneric",b)

[<TestFixture>]
type UserDefinedNameOfTests() =
[<Test>]
member this.``userdefined nameof should shadow the operator`` () =
let nameof x = "test" + x.ToString()

let y = nameof 1
Assert.AreEqual("test1",y)
1 change: 1 addition & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2592,6 +2592,7 @@ Microsoft.FSharp.Core.Operators: System.Object Box[T](T)" +
Microsoft.FSharp.Core.Operators: System.RuntimeMethodHandle MethodHandleOf[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult])" +
#endif
@"
Microsoft.FSharp.Core.Operators: System.String NameOf[T](T)
Microsoft.FSharp.Core.Operators: System.String ToString()
Microsoft.FSharp.Core.Operators: System.String ToString[T](T)
Microsoft.FSharp.Core.Operators: System.String op_Concatenate(System.String, System.String)
Expand Down
2 changes: 1 addition & 1 deletion src/fsharp/FSharp.Core/Linq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ module LeafExpressionConverter =

let isFunctionType typ = equivHeadTypes typ (typeof<(int -> int)>)
let getFunctionType typ =
if not (isFunctionType typ) then invalidArg "typ" "cannot convert recursion except for function types"
if not (isFunctionType typ) then invalidArg (nameof typ) "cannot convert recursion except for function types"
let tyargs = typ.GetGenericArguments()
tyargs.[0], tyargs.[1]

Expand Down
6 changes: 3 additions & 3 deletions src/fsharp/FSharp.Core/Query.fs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type QueryBuilder() =
and default ^Value : int>
(source: QuerySource<'T,'Q>, f : 'T -> Nullable< ^Value >) : Nullable< ^Value > =
let source = source.Source
checkNonNull "source" source
checkNonNull (nameof source) source
use e = source.GetEnumerator()
let mutable acc : ^Value = LanguagePrimitives.GenericZero< (^Value) >
while e.MoveNext() do
Expand All @@ -122,7 +122,7 @@ type QueryBuilder() =

(source: QuerySource<'T,'Q>, selector: 'T -> Nullable< ^Value >) : Nullable< ^Value > =
let source = source.Source
checkNonNull "source" source
checkNonNull (nameof source) source
use e = source.GetEnumerator()
let mutable acc = LanguagePrimitives.GenericZero< (^Value) >
let mutable count = 0
Expand All @@ -140,7 +140,7 @@ type QueryBuilder() =
and default ^Value : float >
(source: QuerySource<'T,'Q>, selector: 'T -> ^Value) : ^Value =
let source = source.Source
checkNonNull "source" source
checkNonNull (nameof source) source
use e = source.GetEnumerator()
let mutable acc = LanguagePrimitives.GenericZero< (^U) >
let mutable count = 0
Expand Down
Loading