-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathAttributeCheckingTests.fs
More file actions
190 lines (153 loc) · 5.63 KB
/
AttributeCheckingTests.fs
File metadata and controls
190 lines (153 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Language
open Xunit
open FSharp.Test.Compiler
module AttributeCheckingTests =
[<Fact>]
let ``attributes check inherited AllowMultiple`` () =
Fsx """
open System
[<AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)>]
type HttpMethodAttribute() = inherit Attribute()
type HttpGetAttribute() = inherit HttpMethodAttribute()
[<HttpGet; HttpGet>] // this shouldn't error like
[<HttpMethod; HttpMethod>] // this doesn't
type C() =
member _.M() = ()
"""
|> ignoreWarnings
|> compile
|> shouldSucceed
[<Fact>]
let ``AllowMultiple=false allows adding attribute to both property and getter/setter`` () =
Fsx """
open System
[<AttributeUsage(AttributeTargets.Property ||| AttributeTargets.Method, AllowMultiple = false)>]
type FooAttribute() = inherit Attribute()
type C() =
[<Foo>]
member _.Foo
with [<Foo>] get () = "bar"
and [<Foo>] set (v: string) = ()
"""
|> ignoreWarnings
|> compile
|> shouldSucceed
[<FSharp.Test.FactForNETCOREAPP>]
let ``Regression: typechecker does not fail when attribute is on type variable (https://github.com/dotnet/fsharp/issues/13525)`` () =
let csharpBaseClass =
CSharp """
using System.Diagnostics.CodeAnalysis;
namespace CSharp
{
public interface ITreeNode
{
}
public static class Extensions
{
public static TNode Copy<TNode>([NotNull] this TNode node, ITreeNode context1 = null) where TNode : ITreeNode =>
node;
}
}""" |> withName "csLib"
let fsharpSource =
"""
module FooBar
open type CSharp.Extensions
let replaceWithCopy oldChild newChild =
let newChildCopy = newChild.Copy()
ignore newChildCopy
"""
FSharp fsharpSource
|> withLangVersionPreview
|> withReferences [csharpBaseClass]
|> compile
|> shouldSucceed
[<FSharp.Test.FactForNETCOREAPP>]
let ``C# attribute subclass inherits AllowMultiple true from base`` () =
let csharpLib =
CSharp """
using System;
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class BaseAttribute : Attribute { }
public class ChildAttribute : BaseAttribute { }
""" |> withName "csAttrLib"
FSharp """
module Test
[<Child; Child>]
type C() = class end
"""
|> withReferences [csharpLib]
|> compile
|> shouldSucceed
[<FSharp.Test.FactForNETCOREAPP>]
let ``C# attribute subclass inherits AllowMultiple false from base`` () =
let csharpLib =
CSharp """
using System;
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class BaseAttribute : Attribute { }
public class ChildAttribute : BaseAttribute { }
""" |> withName "csAttrLib"
FSharp """
module Test
[<Child; Child>]
type C() = class end
"""
|> withReferences [csharpLib]
|> compile
|> shouldFail
|> withSingleDiagnostic (Error 429, Line 4, Col 10, Line 4, Col 15, "The attribute type 'ChildAttribute' has 'AllowMultiple=false'. Multiple instances of this attribute cannot be attached to a single language element.")
[<FSharp.Test.FactForNETCOREAPP>]
let ``C# attribute multi-level inheritance inherits AllowMultiple true`` () =
let csharpLib =
CSharp """
using System;
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class BaseAttribute : Attribute { }
public class MiddleAttribute : BaseAttribute { }
public class LeafAttribute : MiddleAttribute { }
""" |> withName "csAttrLib"
FSharp """
module Test
[<Leaf; Leaf>]
type C() = class end
"""
|> withReferences [csharpLib]
|> compile
|> shouldSucceed
[<FSharp.Test.FactForNETCOREAPP>]
let ``C# attribute subclass with own AttributeUsage overrides base AllowMultiple`` () =
let csharpLib =
CSharp """
using System;
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class BaseAttribute : Attribute { }
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class ChildAttribute : BaseAttribute { }
""" |> withName "csAttrLib"
FSharp """
module Test
[<Child; Child>]
type C() = class end
"""
|> withReferences [csharpLib]
|> compile
|> shouldFail
|> withSingleDiagnostic (Error 429, Line 4, Col 10, Line 4, Col 15, "The attribute type 'ChildAttribute' has 'AllowMultiple=false'. Multiple instances of this attribute cannot be attached to a single language element.")
[<FSharp.Test.FactForNETCOREAPP>]
let ``F# attribute subclass of C# base inherits AllowMultiple true`` () =
let csharpLib =
CSharp """
using System;
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class BaseAttribute : Attribute { }
""" |> withName "csAttrLib"
FSharp """
module Test
type ChildAttribute() = inherit BaseAttribute()
[<Child; Child>]
type C() = class end
"""
|> withReferences [csharpLib]
|> compile
|> shouldSucceed