|
| 1 | +using System.Threading.Tasks; |
| 2 | +using Verifier = Thinktecture.Runtime.Tests.Verifiers.CodeFixVerifier<Thinktecture.CodeAnalysis.Diagnostics.ThinktectureRuntimeExtensionsAnalyzer, Thinktecture.CodeAnalysis.CodeFixes.ThinktectureRuntimeExtensionsCodeFixProvider>; |
| 3 | + |
| 4 | +namespace Thinktecture.Runtime.Tests.AnalyzerAndCodeFixTests; |
| 5 | + |
| 6 | +// ReSharper disable once InconsistentNaming |
| 7 | +public class TTRESG068_ObjectFactoryUseWithEntityFrameworkConflict |
| 8 | +{ |
| 9 | + private const string _DIAGNOSTIC_ID = "TTRESG068"; |
| 10 | + |
| 11 | + [Fact] |
| 12 | + public async Task Should_trigger_when_two_attributes_have_UseWithEntityFramework_true() |
| 13 | + { |
| 14 | + var code = """ |
| 15 | + #nullable enable |
| 16 | + using System; |
| 17 | + using Thinktecture; |
| 18 | +
|
| 19 | + namespace TestNamespace |
| 20 | + { |
| 21 | + [ObjectFactory<int>(UseWithEntityFramework = true)] |
| 22 | + [ObjectFactory<string>(UseWithEntityFramework = true)] |
| 23 | + public partial class {|#0:TestClass|} |
| 24 | + { |
| 25 | + public static ValidationError? Validate(int value, IFormatProvider? provider, out TestClass? item) |
| 26 | + { |
| 27 | + item = null; |
| 28 | + return null; |
| 29 | + } |
| 30 | +
|
| 31 | + public static ValidationError? Validate(string? value, IFormatProvider? provider, out TestClass? item) |
| 32 | + { |
| 33 | + item = null; |
| 34 | + return null; |
| 35 | + } |
| 36 | +
|
| 37 | + public int ToValue() => 0; |
| 38 | + public string ToValue(int _) => ""; |
| 39 | + } |
| 40 | + } |
| 41 | + """; |
| 42 | + |
| 43 | + var expected = Verifier.Diagnostic(_DIAGNOSTIC_ID).WithLocation(0).WithArguments("TestClass"); |
| 44 | + await Verifier.VerifyAnalyzerAsync(code, [typeof(ObjectFactoryAttribute<>).Assembly], expected, |
| 45 | + Verifier.Diagnostic("TTRESG062").WithSpan(9, 25, 9, 34).WithArguments("TestClass", "string")); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public async Task Should_trigger_when_three_attributes_have_UseWithEntityFramework_true() |
| 50 | + { |
| 51 | + var code = """ |
| 52 | + #nullable enable |
| 53 | + using System; |
| 54 | + using Thinktecture; |
| 55 | +
|
| 56 | + namespace TestNamespace |
| 57 | + { |
| 58 | + [ObjectFactory<int>(UseWithEntityFramework = true)] |
| 59 | + [ObjectFactory<string>(UseWithEntityFramework = true)] |
| 60 | + [ObjectFactory<Guid>(UseWithEntityFramework = true)] |
| 61 | + public partial class {|#0:TestClass|} |
| 62 | + { |
| 63 | + public static ValidationError? Validate(int value, IFormatProvider? provider, out TestClass? item) |
| 64 | + { |
| 65 | + item = null; |
| 66 | + return null; |
| 67 | + } |
| 68 | +
|
| 69 | + public static ValidationError? Validate(string? value, IFormatProvider? provider, out TestClass? item) |
| 70 | + { |
| 71 | + item = null; |
| 72 | + return null; |
| 73 | + } |
| 74 | +
|
| 75 | + public static ValidationError? Validate(Guid value, IFormatProvider? provider, out TestClass? item) |
| 76 | + { |
| 77 | + item = null; |
| 78 | + return null; |
| 79 | + } |
| 80 | +
|
| 81 | + public int ToValue() => 0; |
| 82 | + public string ToValue(int _) => ""; |
| 83 | + public Guid ToValue(string _) => Guid.Empty; |
| 84 | + } |
| 85 | + } |
| 86 | + """; |
| 87 | + |
| 88 | + var expected = Verifier.Diagnostic(_DIAGNOSTIC_ID).WithLocation(0).WithArguments("TestClass"); |
| 89 | + await Verifier.VerifyAnalyzerAsync(code, [typeof(ObjectFactoryAttribute<>).Assembly], expected, |
| 90 | + Verifier.Diagnostic("TTRESG062").WithSpan(10, 25, 10, 34).WithArguments("TestClass", "string"), |
| 91 | + Verifier.Diagnostic("TTRESG062").WithSpan(10, 25, 10, 34).WithArguments("TestClass", "Guid")); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task Should_not_trigger_when_only_one_attribute_has_UseWithEntityFramework_true() |
| 96 | + { |
| 97 | + var code = """ |
| 98 | + #nullable enable |
| 99 | + using System; |
| 100 | + using Thinktecture; |
| 101 | +
|
| 102 | + namespace TestNamespace |
| 103 | + { |
| 104 | + [ObjectFactory<int>(UseWithEntityFramework = true)] |
| 105 | + [ObjectFactory<string>(UseWithEntityFramework = false)] |
| 106 | + public partial class TestClass |
| 107 | + { |
| 108 | + public static ValidationError? Validate(int value, IFormatProvider? provider, out TestClass? item) |
| 109 | + { |
| 110 | + item = null; |
| 111 | + return null; |
| 112 | + } |
| 113 | +
|
| 114 | + public static ValidationError? Validate(string? value, IFormatProvider? provider, out TestClass? item) |
| 115 | + { |
| 116 | + item = null; |
| 117 | + return null; |
| 118 | + } |
| 119 | +
|
| 120 | + public int ToValue() => 0; |
| 121 | + } |
| 122 | + } |
| 123 | + """; |
| 124 | + |
| 125 | + await Verifier.VerifyAnalyzerAsync(code, [typeof(ObjectFactoryAttribute<>).Assembly]); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public async Task Should_not_trigger_when_no_attributes_have_UseWithEntityFramework_true() |
| 130 | + { |
| 131 | + var code = """ |
| 132 | + #nullable enable |
| 133 | + using System; |
| 134 | + using Thinktecture; |
| 135 | +
|
| 136 | + namespace TestNamespace |
| 137 | + { |
| 138 | + [ObjectFactory<int>(UseWithEntityFramework = false)] |
| 139 | + [ObjectFactory<string>(UseWithEntityFramework = false)] |
| 140 | + public partial class TestClass |
| 141 | + { |
| 142 | + public static ValidationError? Validate(int value, IFormatProvider? provider, out TestClass? item) |
| 143 | + { |
| 144 | + item = null; |
| 145 | + return null; |
| 146 | + } |
| 147 | +
|
| 148 | + public static ValidationError? Validate(string? value, IFormatProvider? provider, out TestClass? item) |
| 149 | + { |
| 150 | + item = null; |
| 151 | + return null; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + """; |
| 156 | + |
| 157 | + await Verifier.VerifyAnalyzerAsync(code, [typeof(ObjectFactoryAttribute<>).Assembly]); |
| 158 | + } |
| 159 | + |
| 160 | + [Fact] |
| 161 | + public async Task Should_not_trigger_for_single_attribute_with_UseWithEntityFramework_true() |
| 162 | + { |
| 163 | + var code = """ |
| 164 | + #nullable enable |
| 165 | + using System; |
| 166 | + using Thinktecture; |
| 167 | +
|
| 168 | + namespace TestNamespace |
| 169 | + { |
| 170 | + [ObjectFactory<int>(UseWithEntityFramework = true)] |
| 171 | + public partial class TestClass |
| 172 | + { |
| 173 | + public static ValidationError? Validate(int value, IFormatProvider? provider, out TestClass? item) |
| 174 | + { |
| 175 | + item = null; |
| 176 | + return null; |
| 177 | + } |
| 178 | +
|
| 179 | + public int ToValue() => 0; |
| 180 | + } |
| 181 | + } |
| 182 | + """; |
| 183 | + |
| 184 | + await Verifier.VerifyAnalyzerAsync(code, [typeof(ObjectFactoryAttribute<>).Assembly]); |
| 185 | + } |
| 186 | + |
| 187 | + [Fact] |
| 188 | + public async Task Should_not_trigger_when_UseWithEntityFramework_not_explicitly_set() |
| 189 | + { |
| 190 | + var code = """ |
| 191 | + #nullable enable |
| 192 | + using System; |
| 193 | + using Thinktecture; |
| 194 | +
|
| 195 | + namespace TestNamespace |
| 196 | + { |
| 197 | + [ObjectFactory<int>] |
| 198 | + [ObjectFactory<string>] |
| 199 | + public partial class TestClass |
| 200 | + { |
| 201 | + public static ValidationError? Validate(int value, IFormatProvider? provider, out TestClass? item) |
| 202 | + { |
| 203 | + item = null; |
| 204 | + return null; |
| 205 | + } |
| 206 | +
|
| 207 | + public static ValidationError? Validate(string? value, IFormatProvider? provider, out TestClass? item) |
| 208 | + { |
| 209 | + item = null; |
| 210 | + return null; |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + """; |
| 215 | + |
| 216 | + await Verifier.VerifyAnalyzerAsync(code, [typeof(ObjectFactoryAttribute<>).Assembly]); |
| 217 | + } |
| 218 | +} |
0 commit comments