Skip to content

Commit 2611c8b

Browse files
committed
test for types and arrays in F# attribute contructor arguments
1 parent 0ed8391 commit 2611c8b

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

tests/service/ProjectAnalysisTests.fs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,3 +4496,83 @@ let ``Test project36 FSharpMemberOrFunctionOrValue.LiteralValue`` () =
44964496

44974497
let notLit = project36Module.MembersFunctionsAndValues.[1]
44984498
shouldEqual true notLit.LiteralValue.IsNone
4499+
4500+
module Project37 =
4501+
open System.IO
4502+
4503+
let fileName1 = Path.ChangeExtension(Path.GetTempFileName(), ".fs")
4504+
let base2 = Path.GetTempFileName()
4505+
let dllName = Path.ChangeExtension(base2, ".dll")
4506+
let projFileName = Path.ChangeExtension(base2, ".fsproj")
4507+
let fileSource1 = """
4508+
[<System.AttributeUsage(System.AttributeTargets.Method)>]
4509+
type AttrTestAttribute() =
4510+
inherit System.Attribute()
4511+
4512+
new (t: System.Type) = AttrTestAttribute()
4513+
new (t: System.Type[]) = AttrTestAttribute()
4514+
new (t: int[]) = AttrTestAttribute()
4515+
4516+
type TestUnion = | A of string
4517+
type TestRecord = { B : int }
4518+
4519+
module Test =
4520+
[<AttrTest(typeof<int>)>]
4521+
let withType = 0
4522+
[<AttrTest(typeof<list<int>>)>]
4523+
let withGenericType = 0
4524+
[<AttrTest(typeof<int * int>)>]
4525+
let withTupleType = 0
4526+
[<AttrTest(typeof<int -> int>)>]
4527+
let withFuncType = 0
4528+
[<AttrTest([| typeof<TestUnion>; typeof<TestRecord> |])>]
4529+
let withTypeArray = 0
4530+
[<AttrTest([| 0; 1; 2 |])>]
4531+
let withIntArray = 0
4532+
"""
4533+
File.WriteAllText(fileName1, fileSource1)
4534+
let fileNames = [fileName1]
4535+
let args = mkProjectCommandLineArgs (dllName, fileNames)
4536+
let options = checker.GetProjectOptionsFromCommandLineArgs (projFileName, args)
4537+
let wholeProjectResults =
4538+
checker.ParseAndCheckProject(options)
4539+
|> Async.RunSynchronously
4540+
4541+
[<Test>]
4542+
let ``Test project37 typeof and arrays in attribute constructor arguments`` () =
4543+
let allSymbolsUses = Project37.wholeProjectResults.GetAllUsesOfAllSymbols() |> Async.RunSynchronously
4544+
for su in allSymbolsUses do
4545+
match su.Symbol with
4546+
| :? FSharpMemberOrFunctionOrValue as funcSymbol ->
4547+
let getAttrArg() =
4548+
let arg = funcSymbol.Attributes.[0].ConstructorArguments.[0] |> snd
4549+
arg :?> FSharpType
4550+
match funcSymbol.DisplayName with
4551+
| "withType" ->
4552+
let t = getAttrArg()
4553+
t.TypeDefinition.DisplayName |> shouldEqual "int"
4554+
| "withGenericType" ->
4555+
let t = getAttrArg()
4556+
t.TypeDefinition.DisplayName |> shouldEqual "list"
4557+
t.GenericArguments.[0].TypeDefinition.DisplayName |> shouldEqual "int"
4558+
| "withTupleType" ->
4559+
let t = getAttrArg()
4560+
t.IsTupleType |> shouldEqual true
4561+
t.GenericArguments.[0].TypeDefinition.DisplayName |> shouldEqual "int"
4562+
t.GenericArguments.[1].TypeDefinition.DisplayName |> shouldEqual "int"
4563+
| "withFuncType" ->
4564+
let t = getAttrArg()
4565+
t.IsFunctionType |> shouldEqual true
4566+
t.GenericArguments.[0].TypeDefinition.DisplayName |> shouldEqual "int"
4567+
t.GenericArguments.[1].TypeDefinition.DisplayName |> shouldEqual "int"
4568+
| "withTypeArray" ->
4569+
let attr = funcSymbol.Attributes.[0].ConstructorArguments.[0] |> snd
4570+
let ta = attr :?> obj[] |> Array.map (fun t -> t :?> FSharpType)
4571+
ta.[0].TypeDefinition.DisplayName |> shouldEqual "TestUnion"
4572+
ta.[1].TypeDefinition.DisplayName |> shouldEqual "TestRecord"
4573+
| "withIntArray" ->
4574+
let attr = funcSymbol.Attributes.[0].ConstructorArguments.[0] |> snd
4575+
let a = attr :?> obj[] |> Array.map (fun t -> t :?> int)
4576+
a |> shouldEqual [| 0; 1; 2 |]
4577+
| _ -> ()
4578+
| _ -> ()

0 commit comments

Comments
 (0)