GeezerVison is showing github.com — Change behavior of DirectiveLineProcessor to display an error if dire… · scriptcs/scriptcs@b0d7696
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/ScriptCs.Contracts/DirectiveLineProcessor.cs +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -namespace ScriptCs.Contracts | | 1 | +using System; | | 2 | +namespace ScriptCs.Contracts | 2 | 3 | { | 3 | 4 | public abstract class DirectiveLineProcessor : ILineProcessor | 4 | 5 | { | 5 | | - protected virtual bool IgnoreAfterCode | | 6 | + protected virtual bool ThrowIfAfterCode | 6 | 7 | { | 7 | 8 | get { return false; } | 8 | 9 | } | @@ -21,9 +22,9 @@ public bool ProcessLine(IFileParser parser, FileParserContext context, string li | 21 | 22 | return false; | 22 | 23 | } | 23 | 24 |
| 24 | | - if (!isBeforeCode && IgnoreAfterCode) | | 25 | + if (!isBeforeCode && ThrowIfAfterCode) | 25 | 26 | { | 26 | | - return true; | | 27 | + throw new Exception(string.Format("Encountered {0}directive after the start of code. Please move this directive to the beginning of the file.", DirectiveString)); | 27 | 28 | } | 28 | 29 |
| 29 | 30 | return ProcessLine(parser, context, line); | |
Collapse file Copy file name to clipboardExpand all lines: src/ScriptCs.Core/LoadLineProcessor.cs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,7 +22,7 @@ protected override string DirectiveName | 22 | 22 | get { return "load"; } | 23 | 23 | } | 24 | 24 |
| 25 | | - protected override bool IgnoreAfterCode | | 25 | + protected override bool ThrowIfAfterCode | 26 | 26 | { | 27 | 27 | get { return true; } | 28 | 28 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/ScriptCs.Core/ReferenceLineProcessor.cs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,7 +22,7 @@ protected override string DirectiveName | 22 | 22 | get { return "r"; } | 23 | 23 | } | 24 | 24 |
| 25 | | - protected override bool IgnoreAfterCode | | 25 | + protected override bool ThrowIfAfterCode | 26 | 26 | { | 27 | 27 | get { return true; } | 28 | 28 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/ScriptCs.Core.Tests/FileProcessorTests.cs +11-10Lines changed: 11 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,9 +32,6 @@ public class ProcessFileMethod | 32 | 32 | { | 33 | 33 | "using System;", | 34 | 34 | @"Console.WriteLine(""Hello Script 2"");", | 35 | | - @"Console.WriteLine(""Loading Script 3"");", | 36 | | - @"#load ""script3.csx""", | 37 | | - @"Console.WriteLine(""Loaded Script 3"");", | 38 | 35 | @"Console.WriteLine(""Goodbye Script 2"");" | 39 | 36 | }; | 40 | 37 |
| @@ -54,6 +51,16 @@ public class ProcessFileMethod | 54 | 51 | @"Console.WriteLine(""Goodbye Script 4"");" | 55 | 52 | }; | 56 | 53 |
| | 54 | + private List<string> _file5 = new List<string> | | 55 | + { | | 56 | + "using System;", | | 57 | + @"Console.WriteLine(""Hello Script 2"");", | | 58 | + @"Console.WriteLine(""Loading Script 3"");", | | 59 | + @"#load ""script3.csx""", | | 60 | + @"Console.WriteLine(""Loaded Script 3"");", | | 61 | + @"Console.WriteLine(""Goodbye Script 2"");" | | 62 | + }; | | 63 | + | 57 | 64 | private readonly Mock<IFileSystem> _fileSystem; | 58 | 65 |
| 59 | 66 | public ProcessFileMethod() | @@ -263,14 +270,8 @@ public void ShouldNotBeAllowedToLoadAfterUsing() | 263 | 270 | _fileSystem.Setup(x => x.ReadFileLines(It.Is<string>(f => f == "file.csx"))).Returns(file.ToArray()); | 264 | 271 |
| 265 | 272 | var processor = GetFilePreProcessor(); | 266 | | - var result = processor.ProcessFile("file.csx"); | 267 | | - | 268 | | - var splitOutput = result.Code.Split(new[] { Environment.NewLine }, StringSplitOptions.None); | | 273 | + Assert.Throws(typeof(Exception), () => processor.ProcessFile("file.csx")); | 269 | 274 |
| 270 | | - Assert.Equal(1, splitOutput.Count(x => x.TrimStart(' ').StartsWith("using "))); | 271 | | - | 272 | | - // consider #line directive | 273 | | - Assert.Equal(4, splitOutput.Length); | 274 | 275 | _fileSystem.Verify(x => x.ReadFileLines(It.Is<string>(i => i == "script4.csx")), Times.Never()); | 275 | 276 | } | 276 | 277 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/ScriptCs.Core.Tests/LoadLineProcessorTests.cs +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | using Should; | 10 | 10 |
| 11 | 11 | using Xunit.Extensions; | | 12 | +using Should.Core.Assertions; | 12 | 13 |
| 13 | 14 | namespace ScriptCs.Tests | 14 | 15 | { | @@ -52,7 +53,7 @@ public void ShouldReturnFalseOtherwise(IFileParser parser, LoadLineProcessor pro | 52 | 53 | } | 53 | 54 |
| 54 | 55 | [Theory, ScriptCsAutoData] | 55 | | - public void ShouldReturnTrueButNotParseFileIfAfterCode( | | 56 | + public void ShouldThrowExceptionIfAfterCode( | 56 | 57 | [Frozen] Mock<IFileParser> parser, | 57 | 58 | [Frozen] Mock<IFileSystem> fileSystem, | 58 | 59 | LoadLineProcessor processor) | @@ -65,13 +66,9 @@ public void ShouldReturnTrueButNotParseFileIfAfterCode( | 65 | 66 | const string FullPath = "C:\\script.csx"; | 66 | 67 |
| 67 | 68 | fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath); | 68 | | - | 69 | | - // Act | 70 | | - var result = processor.ProcessLine(parser.Object, context, Line, false); | 71 | 69 |
| 72 | | - // Assert | 73 | | - result.ShouldBeTrue(); | 74 | | - parser.Verify(x => x.ParseFile(FullPath, It.IsAny<FileParserContext>()), Times.Never()); | | 70 | + // Act / Assert | | 71 | + Assert.Throws(typeof(Exception), () => processor.ProcessLine(parser.Object, context, Line, false)); | 75 | 72 | } | 76 | 73 |
| 77 | 74 | [Theory, ScriptCsAutoData] | |
Collapse file Copy file name to clipboardExpand all lines: test/ScriptCs.Core.Tests/ReferenceLineProcessorTests.cs +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | using Should; | 6 | 6 | using Xunit.Extensions; | 7 | 7 | using System.Linq; | | 8 | +using Should.Core.Assertions; | 8 | 9 |
| 9 | 10 | namespace ScriptCs.Tests | 10 | 11 | { | @@ -48,7 +49,7 @@ public void ShouldReturnFalseOtherwise(IFileParser parser, ReferenceLineProcesso | 48 | 49 | } | 49 | 50 |
| 50 | 51 | [Theory, ScriptCsAutoData] | 51 | | - public void ShouldReturnTrueButNotAddReferenceIfAfterCode( | | 52 | + public void ShouldThrowExceptionIfAfterCode( | 52 | 53 | [Frozen] Mock<IFileSystem> fileSystem, | 53 | 54 | ReferenceLineProcessor processor, | 54 | 55 | IFileParser parser) | @@ -62,12 +63,8 @@ public void ShouldReturnTrueButNotAddReferenceIfAfterCode( | 62 | 63 |
| 63 | 64 | fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath); | 64 | 65 |
| 65 | | - // Act | 66 | | - var result = processor.ProcessLine(parser, context, Line, false); | 67 | | - | 68 | | - // Assert | 69 | | - result.ShouldBeTrue(); | 70 | | - context.References.Count.ShouldEqual(0); | | 66 | + // Act / Assert | | 67 | + Assert.Throws(typeof(Exception), () => processor.ProcessLine(parser, context, Line, false)); | 71 | 68 | } | 72 | 69 |
| 73 | 70 | [Theory, ScriptCsAutoData] | |
|