未验证 提交 5fbe48ba 编写于 作者: F filipw

check parameters for null

improve tests by using StackTrace API and diagnostic verifier
上级 739b8055
...@@ -41,6 +41,7 @@ public static Script<T> Create<T>(string code, ScriptOptions options = null, Typ ...@@ -41,6 +41,7 @@ public static Script<T> Create<T>(string code, ScriptOptions options = null, Typ
/// <exception cref="ArgumentException">Stream is not readable or seekable.</exception> /// <exception cref="ArgumentException">Stream is not readable or seekable.</exception>
public static Script<T> Create<T>(Stream code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null) public static Script<T> Create<T>(Stream code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null)
{ {
if (code == null) throw new ArgumentNullException(nameof(code));
return Script.CreateInitialScript<T>(CSharpScriptCompiler.Instance, SourceText.From(code, options?.FileEncoding), options, globalsType, assemblyLoader); return Script.CreateInitialScript<T>(CSharpScriptCompiler.Instance, SourceText.From(code, options?.FileEncoding), options, globalsType, assemblyLoader);
} }
...@@ -67,6 +68,7 @@ public static Script<object> Create(string code, ScriptOptions options = null, T ...@@ -67,6 +68,7 @@ public static Script<object> Create(string code, ScriptOptions options = null, T
/// <exception cref="ArgumentException">Stream is not readable or seekable.</exception> /// <exception cref="ArgumentException">Stream is not readable or seekable.</exception>
public static Script<object> Create(Stream code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null) public static Script<object> Create(Stream code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null)
{ {
if (code == null) throw new ArgumentNullException(nameof(code));
return Create<object>(code, options, globalsType, assemblyLoader); return Create<object>(code, options, globalsType, assemblyLoader);
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
using System.IO; using System.IO;
using System.Globalization; using System.Globalization;
using System.Text; using System.Text;
using System.Diagnostics;
namespace Microsoft.CodeAnalysis.CSharp.Scripting.UnitTests namespace Microsoft.CodeAnalysis.CSharp.Scripting.UnitTests
{ {
...@@ -715,14 +716,16 @@ public async Task LoadedFileWithVoidReturn() ...@@ -715,14 +716,16 @@ public async Task LoadedFileWithVoidReturn()
[Fact] [Fact]
public async Task Pdb_CreateFromString_CodeFromFile_WithEmitDebugInformation_WithoutFileEncoding_CompilationErrorException() public async Task Pdb_CreateFromString_CodeFromFile_WithEmitDebugInformation_WithoutFileEncoding_CompilationErrorException()
{ {
var code = "throw new System.Exception();";
try try
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFilePath("debug.csx").WithFileEncoding(null); var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFilePath("debug.csx").WithFileEncoding(null);
var script = await CSharpScript.RunAsync("throw new System.Exception();", opts); var script = await CSharpScript.RunAsync(code, opts);
} }
catch (CompilationErrorException ex) catch (CompilationErrorException ex)
{ {
Assert.EndsWith("error CS8055: Cannot emit debug information for a source text without encoding.", ex.Message); // CS8055: Cannot emit debug information for a source text without encoding.
ex.Diagnostics.Verify(Diagnostic(ErrorCode.ERR_EncodinglessSyntaxTree, code).WithLocation(1,1));
} }
} }
...@@ -730,77 +733,77 @@ public async Task Pdb_CreateFromString_CodeFromFile_WithEmitDebugInformation_Wit ...@@ -730,77 +733,77 @@ public async Task Pdb_CreateFromString_CodeFromFile_WithEmitDebugInformation_Wit
public Task Pdb_CreateFromString_CodeFromFile_WithEmitDebugInformation_WithFileEncoding_ResultInPdbEmitted() public Task Pdb_CreateFromString_CodeFromFile_WithEmitDebugInformation_WithFileEncoding_ResultInPdbEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFilePath("debug.csx").WithFileEncoding(Encoding.UTF8); var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFilePath("debug.csx").WithFileEncoding(Encoding.UTF8);
return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), "debug.csx:line 1"); return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), line: 1, column: 1, filename: "debug.csx");
} }
[Fact] [Fact]
public Task Pdb_CreateFromString_CodeFromFile_WithoutEmitDebugInformation_WithoutFileEncoding_ResultInPdbNotEmitted() public Task Pdb_CreateFromString_CodeFromFile_WithoutEmitDebugInformation_WithoutFileEncoding_ResultInPdbNotEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFilePath(null).WithFileEncoding(null); var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFilePath(null).WithFileEncoding(null);
return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), "at Submission#0.<<Initialize>>d__0.MoveNext()"); return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts));
} }
[Fact] [Fact]
public Task Pdb_CreateFromString_CodeFromFile_WithoutEmitDebugInformation_WithFileEncoding_ResultInPdbNotEmitted() public Task Pdb_CreateFromString_CodeFromFile_WithoutEmitDebugInformation_WithFileEncoding_ResultInPdbNotEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFilePath("debug.csx").WithFileEncoding(Encoding.UTF8); var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFilePath("debug.csx").WithFileEncoding(Encoding.UTF8);
return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), "at Submission#0.<<Initialize>>d__0.MoveNext()"); return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts));
} }
[Fact] [Fact]
public Task Pdb_CreateFromStream_CodeFromFile_WithEmitDebugInformation_ResultInPdbEmitted() public Task Pdb_CreateFromStream_CodeFromFile_WithEmitDebugInformation_ResultInPdbEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFilePath("debug.csx"); var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFilePath("debug.csx");
return VerifyStackTraceAsync(() => CSharpScript.Create(new MemoryStream(Encoding.UTF8.GetBytes("throw new System.Exception();")), opts), "debug.csx:line 1"); return VerifyStackTraceAsync(() => CSharpScript.Create(new MemoryStream(Encoding.UTF8.GetBytes("throw new System.Exception();")), opts), line: 1, column: 1, filename: "debug.csx");
} }
[Fact] [Fact]
public Task Pdb_CreateFromStream_CodeFromFile_WithoutEmitDebugInformation_ResultInPdbNotEmitted() public Task Pdb_CreateFromStream_CodeFromFile_WithoutEmitDebugInformation_ResultInPdbNotEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFilePath("debug.csx"); var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFilePath("debug.csx");
return VerifyStackTraceAsync(() => CSharpScript.Create(new MemoryStream(Encoding.UTF8.GetBytes("throw new System.Exception();")), opts), "at Submission#0.<<Initialize>>d__0.MoveNext()"); return VerifyStackTraceAsync(() => CSharpScript.Create(new MemoryStream(Encoding.UTF8.GetBytes("throw new System.Exception();")), opts));
} }
[Fact] [Fact]
public Task Pdb_CreateFromString_InlineCode_WithEmitDebugInformation_WithoutFileEncoding_ResultInPdbEmitted() public Task Pdb_CreateFromString_InlineCode_WithEmitDebugInformation_WithoutFileEncoding_ResultInPdbEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFileEncoding(null); var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFileEncoding(null);
return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), "at Submission#0.<<Initialize>>d__0.MoveNext() in :line 1"); return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), line: 1, column: 1, filename: "");
} }
[Fact] [Fact]
public Task Pdb_CreateFromString_InlineCode_WithEmitDebugInformation_WithFileEncoding_ResultInPdbEmitted() public Task Pdb_CreateFromString_InlineCode_WithEmitDebugInformation_WithFileEncoding_ResultInPdbEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFileEncoding(Encoding.UTF8); var opts = ScriptOptions.Default.WithEmitDebugInformation(true).WithFileEncoding(Encoding.UTF8);
return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), "at Submission#0.<<Initialize>>d__0.MoveNext() in :line 1"); return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), line: 1, column: 1, filename: "");
} }
[Fact] [Fact]
public Task Pdb_CreateFromString_InlineCode_WithoutEmitDebugInformation_WithoutFileEncoding_ResultInPdbNotEmitted() public Task Pdb_CreateFromString_InlineCode_WithoutEmitDebugInformation_WithoutFileEncoding_ResultInPdbNotEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFileEncoding(null); var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFileEncoding(null);
return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), "at Submission#0.<<Initialize>>d__0.MoveNext()"); return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts));
} }
[Fact] [Fact]
public Task Pdb_CreateFromString_InlineCode_WithoutEmitDebugInformation_WithFileEncoding_ResultInPdbNotEmitted() public Task Pdb_CreateFromString_InlineCode_WithoutEmitDebugInformation_WithFileEncoding_ResultInPdbNotEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFileEncoding(Encoding.UTF8); var opts = ScriptOptions.Default.WithEmitDebugInformation(false).WithFileEncoding(Encoding.UTF8);
return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts), "at Submission#0.<<Initialize>>d__0.MoveNext()"); return VerifyStackTraceAsync(() => CSharpScript.Create("throw new System.Exception();", opts));
} }
[Fact] [Fact]
public Task Pdb_CreateFromStream_InlineCode_WithEmitDebugInformation_ResultInPdbEmitted() public Task Pdb_CreateFromStream_InlineCode_WithEmitDebugInformation_ResultInPdbEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(true); var opts = ScriptOptions.Default.WithEmitDebugInformation(true);
return VerifyStackTraceAsync(() => CSharpScript.Create(new MemoryStream(Encoding.UTF8.GetBytes("throw new System.Exception();")), opts), "at Submission#0.<<Initialize>>d__0.MoveNext() in :line 1"); return VerifyStackTraceAsync(() => CSharpScript.Create(new MemoryStream(Encoding.UTF8.GetBytes("throw new System.Exception();")), opts), line: 1, column: 1, filename: "");
} }
[Fact] [Fact]
public Task Pdb_CreateFromStream_InlineCode_WithoutEmitDebugInformation_ResultInPdbNotEmitted() public Task Pdb_CreateFromStream_InlineCode_WithoutEmitDebugInformation_ResultInPdbNotEmitted()
{ {
var opts = ScriptOptions.Default.WithEmitDebugInformation(false); var opts = ScriptOptions.Default.WithEmitDebugInformation(false);
return VerifyStackTraceAsync(() => CSharpScript.Create(new MemoryStream(Encoding.UTF8.GetBytes("throw new System.Exception();")), opts), "at Submission#0.<<Initialize>>d__0.MoveNext()"); return VerifyStackTraceAsync(() => CSharpScript.Create(new MemoryStream(Encoding.UTF8.GetBytes("throw new System.Exception();")), opts));
} }
[WorkItem(12348, "https://github.com/dotnet/roslyn/issues/12348")] [WorkItem(12348, "https://github.com/dotnet/roslyn/issues/12348")]
...@@ -856,20 +859,7 @@ public override Stream OpenRead(string resolvedPath) ...@@ -856,20 +859,7 @@ public override Stream OpenRead(string resolvedPath)
} }
} }
private string GetStackTraceLine(Exception ex, int index) private async Task VerifyStackTraceAsync(Func<Script<object>> scriptProvider, int line = 0, int column = 0, string filename = null)
{
if (ex == null || ex.StackTrace == null) return null;
var stackTrace = ex.StackTrace?.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
if (stackTrace.Length >= index)
{
return stackTrace[index].Trim();
}
return null;
}
private async Task VerifyStackTraceAsync(Func<Script<object>> scriptProvider, string expectedFirstLineEnding)
{ {
try try
{ {
...@@ -879,7 +869,11 @@ private async Task VerifyStackTraceAsync(Func<Script<object>> scriptProvider, st ...@@ -879,7 +869,11 @@ private async Task VerifyStackTraceAsync(Func<Script<object>> scriptProvider, st
catch (Exception ex) catch (Exception ex)
{ {
// line information is only available when PDBs have been emitted // line information is only available when PDBs have been emitted
Assert.EndsWith(expectedFirstLineEnding, GetStackTraceLine(ex, 0)); var stackTrace = new StackTrace(ex, needFileInfo: true);
var firstFrame = stackTrace.GetFrames()[0];
Assert.Equal(filename, firstFrame.GetFileName());
Assert.Equal(line, firstFrame.GetFileLineNumber());
Assert.Equal(column, firstFrame.GetFileColumnNumber());
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册