public static void ClassLibrary1ProjectFileFieldNameMustNotBeginWithUnderscoreDisabled() { var code = ProjectFile.Find("ClassLibrary1.csproj"); var expected = "Expected no diagnostics, found:\r\n" + "SA13090 Field \'_value\' must not begin with an underscore\r\n" + $" at line 5 and character 20 in file {code.DirectoryName}\\ClassLibrary1Class1.cs | private int ↓_value;"; var analyzer = new FieldNameMustNotBeginWithUnderscoreDisabled(); var exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer, code)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscoreDisabled), code)); StringAssert.Contains(expected, exception.Message); var descriptor = FieldNameMustNotBeginWithUnderscoreDisabled.Descriptor; exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscoreDisabled), descriptor, code)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer, descriptor, code)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer, code, compilationOptions: CodeFactory.DefaultCompilationOptions(analyzer, descriptor, null))); StringAssert.Contains(expected, exception.Message); }
public static void ProjectFromDisk() { var after = @"// ReSharper disable All namespace ClassLibrary1 { public class ClassLibrary1Class1 { private int value; public ClassLibrary1Class1(int value) { this.value = value; } } } "; var csproj = ProjectFile.Find("ClassLibrary1.csproj"); var analyzer = new FieldNameMustNotBeginWithUnderscore(); var expectedDiagnostic = ExpectedDiagnostic.Create( FieldNameMustNotBeginWithUnderscore.DiagnosticId, "Field '_value' must not begin with an underscore", Path.Combine(csproj.DirectoryName, "ClassLibrary1Class1.cs"), 5, 20); var solution = CodeFactory.CreateSolution( csproj, analyzer, expectedDiagnostic, metadataReferences: new[] { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) }); RoslynAssert.CodeFix(analyzer, new DoNotUseUnderscoreFix(), expectedDiagnostic, solution, after); }
public void ClassLibrary1ProjectFileFieldNameMustNotBeginWithUnderscoreDisabled() { var csproj = ProjectFile.Find("ClassLibrary1.csproj"); var expected = "Expected no diagnostics, found:\r\n" + "SA13090 Field \'_value\' must not begin with an underscore\r\n" + $" at line 9 and character 20 in file {csproj.DirectoryName}\\ClassLibrary1Class1.cs | private int ↓_value;"; var analyzer = new FieldNameMustNotBeginWithUnderscoreDisabled(); var exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid <FieldNameMustNotBeginWithUnderscoreDisabled>(csproj)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscoreDisabled), csproj)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid(analyzer, csproj)); StringAssert.Contains(expected, exception.Message); var descriptor = FieldNameMustNotBeginWithUnderscoreDisabled.Descriptor; exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid <FieldNameMustNotBeginWithUnderscoreDisabled>(descriptor, csproj)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscoreDisabled), descriptor, csproj)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid(analyzer, descriptor, csproj)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid(analyzer, csproj, CodeFactory.DefaultCompilationOptions(analyzer, descriptor, AnalyzerAssert.SuppressedDiagnostics), AnalyzerAssert.MetadataReferences)); StringAssert.Contains(expected, exception.Message); if (Throw) { AnalyzerAssert.Valid <FieldNameMustNotBeginWithUnderscoreDisabled>(csproj); } }
public void ClassLibrary1NopAnalyzer() { var analyzer = new NopAnalyzer(); var code = ProjectFile.Find("ClassLibrary1.csproj"); var descriptor = NopAnalyzer.Descriptor; RoslynAssert.Valid(typeof(NopAnalyzer), descriptor, code); RoslynAssert.Valid(analyzer, descriptor, code); RoslynAssert.Valid(analyzer, code, compilationOptions: CodeFactory.DefaultCompilationOptions(analyzer, descriptor, null)); }
public void WpfAppNoErrorAnalyzer() { var analyzer = new NoErrorAnalyzer(); var csproj = ProjectFile.Find("WpfApp1.csproj"); var descriptor = NoErrorAnalyzer.Descriptor; AnalyzerAssert.Valid <NoErrorAnalyzer>(descriptor, csproj); AnalyzerAssert.Valid(typeof(NoErrorAnalyzer), descriptor, csproj); AnalyzerAssert.Valid(analyzer, descriptor, csproj); AnalyzerAssert.Valid(analyzer, csproj, CodeFactory.DefaultCompilationOptions(analyzer, descriptor, null), AnalyzerAssert.MetadataReferences); }
public static async Task Project() { var analyzer = new FieldNameMustNotBeginWithUnderscore(); var sln = CodeFactory.CreateSolution(ProjectFile.Find("Gu.Roslyn.Asserts.csproj")); var benchmark = await Benchmark.CreateAsync(sln.Projects.Single(), analyzer).ConfigureAwait(false); CollectionAssert.IsNotEmpty(benchmark.SyntaxNodeActions); Assert.AreSame(analyzer, benchmark.Analyzer); benchmark.Run(); benchmark.Run(); }
public void SetUp() { var original = ProjectFile.Find("Gu.Localization.TestStub.csproj"); var tempDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), original.Directory.Name)); if (tempDir.Exists) { tempDir.Delete(recursive: true); } original.Directory.CopyTo(tempDir); this.projectFile = tempDir.FindFile(original.Name); }
public static void ClassLibrary2ProjectFileFieldNameMustNotBeginWithUnderscoreDisabled() { var code = ProjectFile.Find("ClassLibrary2.csproj"); var expected = "Expected no diagnostics, found:\r\n" + "SA13090 Field \'_value\' must not begin with an underscore\r\n" + $" at line 5 and character 20 in file {code.DirectoryName}\\ClassLibrary2Class1.cs | private int ↓_value;"; var analyzer = new FieldNameMustNotBeginWithUnderscoreDisabled(); var exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer, code)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer.GetType(), code)); StringAssert.Contains(expected, exception.Message); }
public static void ClassLibrary2SolutionFileFieldNameMustNotBeginWithUnderscoreDisabled() { var code = ProjectFile.Find("ClassLibrary2.csproj"); var expected = "Expected no diagnostics, found:\r\n" + "SA13090 Field \'_value\' must not begin with an underscore\r\n" + $" at line 5 and character 20 in file {code.DirectoryName}\\ClassLibrary2Class1.cs | private int ↓_value;"; var solution = CodeFactory.CreateSolution(code, new[] { new FieldNameMustNotBeginWithUnderscoreDisabled() }, Gu.Roslyn.Asserts.MetadataReferences.FromAttributes()); var analyzer = new FieldNameMustNotBeginWithUnderscoreDisabled(); var exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer, solution)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer.GetType(), solution)); StringAssert.Contains(expected, exception.Message); }
public static void SetUp() { var original = ProjectFile.Find("Gu.Localization.TestStub.csproj"); var tempDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), original.Directory.Name)); if (tempDir.Exists) { tempDir.Delete(recursive: true); } original.Directory.CopyTo(tempDir); projectFile = tempDir.FindFile(original.Name); fooFile = tempDir.FindFile("Foo.cs"); resxFile = tempDir.FindFile("Properties\\Resources.resx"); }
public void ParseInfo() { var file = ProjectFile.Find("ClassLibrary1.csproj"); var csproj = ProjectFile.ParseInfo(file); var expected = new[] { "AllowCompilationErrors.cs", "ClassLibrary1Class1.cs", "AssemblyInfo.cs", }; CollectionAssert.AreEquivalent(expected, csproj.Documents.Select(x => x.Name)); foreach (var document in csproj.Documents) { Assert.AreEqual(true, File.Exists(document.FilePath)); } }
public static void ProjectFileNopAnalyzer() { Assert.Inconclusive("This has dependency problems in bot net472 and netcoreapp."); var code = ProjectFile.Find("Gu.Roslyn.Asserts.csproj"); var metadataReferences = Gu.Roslyn.Asserts.MetadataReferences.Transitive( typeof(Microsoft.CodeAnalysis.CSharp.CSharpCompilation), typeof(Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider)) .ToArray(); var descriptor = Descriptors.Id1; var analyzer = new NopAnalyzer(descriptor); RoslynAssert.Valid(analyzer, code, metadataReferences: metadataReferences); RoslynAssert.Valid(typeof(NopAnalyzer), code, metadataReferences: metadataReferences); RoslynAssert.Valid(analyzer, descriptor, code, metadataReferences: metadataReferences); RoslynAssert.Valid(typeof(NopAnalyzer), descriptor, code, metadataReferences: metadataReferences); }
public void ClassLibrary2ProjectFileFieldNameMustNotBeginWithUnderscoreDisabled() { var csproj = ProjectFile.Find("ClassLibrary2.csproj"); var expected = "Expected no diagnostics, found:\r\n" + "SA13090 Field \'_value\' must not begin with an underscore\r\n" + $" at line 9 and character 20 in file {csproj.DirectoryName}\\ClassLibrary2Class1.cs | private int ↓_value;"; var exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid <FieldNameMustNotBeginWithUnderscoreDisabled>(csproj)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscoreDisabled), csproj)); StringAssert.Contains(expected, exception.Message); exception = Assert.Throws <AssertException>(() => AnalyzerAssert.Valid(new FieldNameMustNotBeginWithUnderscoreDisabled(), csproj)); StringAssert.Contains(expected, exception.Message); if (Throw) { AnalyzerAssert.Valid <FieldNameMustNotBeginWithUnderscoreDisabled>(csproj); } }
public void ProjectFromDisk() { var fixedCode = @"// ReSharper disable InconsistentNaming // ReSharper disable ArrangeThisQualifier // ReSharper disable NotAccessedField.Local #pragma warning disable IDE0009 // Member access should be qualified. #pragma warning disable IDE0044 // Add readonly modifier namespace ClassLibrary1 { public class ClassLibrary1Class1 { private int value; public ClassLibrary1Class1(int value) { this.value = value; } } } "; var csproj = ProjectFile.Find("ClassLibrary1.csproj"); var analyzer = new FieldNameMustNotBeginWithUnderscore(); var expectedDiagnostic = ExpectedDiagnostic.Create( FieldNameMustNotBeginWithUnderscore.DiagnosticId, "Field '_value' must not begin with an underscore", Path.Combine(csproj.DirectoryName, "ClassLibrary1Class1.cs"), 9, 20); var sln = CodeFactory.CreateSolution( csproj, analyzer, expectedDiagnostic, metadataReferences: new[] { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) }); AnalyzerAssert.CodeFix(analyzer, new DontUseUnderscoreCodeFixProvider(), expectedDiagnostic, sln, fixedCode); }
public void Find(string name) { var projectFile = ProjectFile.Find(name); Assert.AreEqual(name, projectFile.Name); }
string type = String.Empty; // 对象类型 #endregion #region ASP.NET 事件 protected void Page_Load(object sender, EventArgs e) { op = RequestData.Get <string>("op"); id = RequestData.Get <string>("id"); type = RequestData.Get <string>("type"); ProjectFile ent = null; switch (this.RequestAction) { case RequestActionEnum.Update: ent = this.GetMergedData <ProjectFile>(); ent.DoUpdate(); this.SetMessage("修改成功!"); break; case RequestActionEnum.Insert: case RequestActionEnum.Create: ent = this.GetPostedData <ProjectFile>(); //ent.CreateTime = DateTime.Now; // DataTable dt = DataHelper.QueryDataTable("select case when TypeId is not null then TypeId+Id+',' else Id+',' end as typid from NCRL_SP..ProjectFile where Id='" + RequestData.Get<string>("id") + "'"); // if (dt.Rows.Count != 0) // { // ent.TypeID = dt.Rows[0]["typid"].ToString(); // } // ent.IsDelete = "0"; ent.DoCreate(); this.SetMessage("新建成功!"); break; default: if (RequestActionString == "createsub") { ent = this.GetPostedData <ProjectFile>(); ent.CreateTime = DateTime.Now; DataTable dt = DataHelper.QueryDataTable("select case when TypeId is not null then TypeId+Id+',' else Id+',' end as typid from NCRL_SP..ProjectFile where Id='" + RequestData.Get <string>("id") + "'"); if (dt.Rows.Count != 0) { ent.TypeID = dt.Rows[0]["typid"].ToString(); ent.ParentID = RequestData.Get <string>("id"); } ent.IsDelete = "0"; ent.DoCreate(); this.SetMessage("新建成功!"); } break; } if (op != "c" && op != "cs") { if (!String.IsNullOrEmpty(id)) { ent = ProjectFile.Find(id); } this.SetFormData(ent); } }