Skip to content
This repository has been archived by the owner. It is now read-only.

Commit 219b884

Browse files
committed
Add tests for ContractRequiresStringNotNullOrEmpty
1 parent 1d43b8a commit 219b884

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis;
5+
using System.Collections.Immutable;
6+
using RefactoringEssentials.CSharp.CodeRefactorings;
7+
8+
namespace RefactoringEssentials.Tests.CSharp.CodeRefactorings
9+
{
10+
[TestFixture]
11+
public class ContractRequiresStringNotNullOrEmptyTests : CSharpCodeRefactoringTestBase
12+
{
13+
[Test]
14+
public void TestLambda()
15+
{
16+
Test<ContractRequiresStringNotNullOrEmptyCodeRefactoringProvider>(@"class Foo
17+
{
18+
void Test ()
19+
{
20+
var lambda = (string $s, int e) => {
21+
};
22+
}
23+
}", @"using System.Diagnostics.Contracts;
24+
25+
class Foo
26+
{
27+
void Test ()
28+
{
29+
var lambda = (string s, int e) => {
30+
Contract.Requires(string.IsNullOrEmpty(s) == false);
31+
};
32+
}
33+
}");
34+
}
35+
36+
[Test]
37+
public void TestAnonymousMethod()
38+
{
39+
Test<ContractRequiresStringNotNullOrEmptyCodeRefactoringProvider>(@"class Foo
40+
{
41+
void Test ()
42+
{
43+
var lambda = delegate(string $-[s]-, object e) {
44+
};
45+
}
46+
}", @"using System.Diagnostics.Contracts;
47+
48+
class Foo
49+
{
50+
void Test ()
51+
{
52+
var lambda = delegate(string s, object e) {
53+
Contract.Requires(string.IsNullOrEmpty(s) == false);
54+
};
55+
}
56+
}");
57+
}
58+
59+
[Test]
60+
public void TestContractAlreadyPresent()
61+
{
62+
TestWrongContext<ContractRequiresStringNotNullOrEmptyCodeRefactoringProvider>(@"class Foo
63+
{
64+
void Test ()
65+
{
66+
var lambda = (string $s, int e) => {
67+
Contract.Requires(string.IsNullOrEmpty(s) == false);
68+
};
69+
}
70+
}");
71+
}
72+
73+
[Test]
74+
public void TestDifferentContractAlreadyPresent()
75+
{
76+
Test<ContractRequiresStringNotNullOrEmptyCodeRefactoringProvider>(@"class Foo
77+
{
78+
void Test ()
79+
{
80+
var lambda = (string $s, int e) => {
81+
Contract.Requires(string.IsNullOrEmpty(notS) == false);
82+
};
83+
}
84+
}", @"using System.Diagnostics.Contracts;
85+
86+
class Foo
87+
{
88+
void Test ()
89+
{
90+
var lambda = (string s, int e) => {
91+
Contract.Requires(string.IsNullOrEmpty(s) == false);
92+
Contract.Requires(string.IsNullOrEmpty(notS) == false);
93+
};
94+
}
95+
}");
96+
}
97+
98+
[Test]
99+
public void TestUsingStatementAlreadyPresent()
100+
{
101+
Test<ContractRequiresStringNotNullOrEmptyCodeRefactoringProvider>(@"using System.Diagnostics.Contracts;
102+
class Foo
103+
{
104+
void Test ()
105+
{
106+
var lambda = (string $s, int e) => {
107+
};
108+
}
109+
}", @"using System.Diagnostics.Contracts;
110+
class Foo
111+
{
112+
void Test ()
113+
{
114+
var lambda = (string s, int e) => {
115+
Contract.Requires(string.IsNullOrEmpty(s) == false);
116+
};
117+
}
118+
}");
119+
}
120+
121+
[Test]
122+
public void TestPopupOnlyOnName()
123+
{
124+
TestWrongContext<ContractRequiresStringNotNullOrEmptyCodeRefactoringProvider>(@"class Foo
125+
{
126+
void Test ($string param)
127+
{
128+
}
129+
}");
130+
}
131+
132+
[Test]
133+
public void Test_OldCSharp()
134+
{
135+
var parseOptions = new CSharpParseOptions(
136+
LanguageVersion.CSharp5,
137+
DocumentationMode.Diagnose | DocumentationMode.Parse,
138+
SourceCodeKind.Regular,
139+
ImmutableArray.Create("DEBUG", "TEST")
140+
);
141+
142+
Test<ContractRequiresStringNotNullOrEmptyCodeRefactoringProvider>(@"class Foo
143+
{
144+
void Test (string $test)
145+
{
146+
}
147+
}", @"using System.Diagnostics.Contracts;
148+
149+
class Foo
150+
{
151+
void Test (string test)
152+
{
153+
Contract.Requires(string.IsNullOrEmpty(test) == false);
154+
}
155+
}", parseOptions: parseOptions);
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)