GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Change behavior of DirectiveLineProcessor to display an error if dire… · scriptcs/scriptcs@b0d7696

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • ScriptCs.Contracts
      • DirectiveLineProcessor.cs
    • ScriptCs.Core
      • LoadLineProcessor.cs
      • ReferenceLineProcessor.cs
  • test/ScriptCs.Core.Tests
    • FileProcessorTests.cs
    • LoadLineProcessorTests.cs
    • ReferenceLineProcessorTests.cs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/ScriptCs.Contracts/DirectiveLineProcessor.cs‎

Copy file name to clipboardExpand all lines: src/ScriptCs.Contracts/DirectiveLineProcessor.cs
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
namespace ScriptCs.Contracts
1+
using System;
2+
namespace ScriptCs.Contracts
23
{
34
public abstract class DirectiveLineProcessor : ILineProcessor
45
{
5-
protected virtual bool IgnoreAfterCode
6+
protected virtual bool ThrowIfAfterCode
67
{
78
get { return false; }
89
}
@@ -21,9 +22,9 @@ public bool ProcessLine(IFileParser parser, FileParserContext context, string li
2122
return false;
2223
}
2324

24-
if (!isBeforeCode && IgnoreAfterCode)
25+
if (!isBeforeCode && ThrowIfAfterCode)
2526
{
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));
2728
}
2829

2930
return ProcessLine(parser, context, line);
Collapse file

‎src/ScriptCs.Core/LoadLineProcessor.cs‎

Copy file name to clipboardExpand all lines: src/ScriptCs.Core/LoadLineProcessor.cs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected override string DirectiveName
2222
get { return "load"; }
2323
}
2424

25-
protected override bool IgnoreAfterCode
25+
protected override bool ThrowIfAfterCode
2626
{
2727
get { return true; }
2828
}
Collapse file

‎src/ScriptCs.Core/ReferenceLineProcessor.cs‎

Copy file name to clipboardExpand all lines: src/ScriptCs.Core/ReferenceLineProcessor.cs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected override string DirectiveName
2222
get { return "r"; }
2323
}
2424

25-
protected override bool IgnoreAfterCode
25+
protected override bool ThrowIfAfterCode
2626
{
2727
get { return true; }
2828
}
Collapse file

‎test/ScriptCs.Core.Tests/FileProcessorTests.cs‎

Copy file name to clipboardExpand all lines: test/ScriptCs.Core.Tests/FileProcessorTests.cs
+11-10Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public class ProcessFileMethod
3232
{
3333
"using System;",
3434
@"Console.WriteLine(""Hello Script 2"");",
35-
@"Console.WriteLine(""Loading Script 3"");",
36-
@"#load ""script3.csx""",
37-
@"Console.WriteLine(""Loaded Script 3"");",
3835
@"Console.WriteLine(""Goodbye Script 2"");"
3936
};
4037

@@ -54,6 +51,16 @@ public class ProcessFileMethod
5451
@"Console.WriteLine(""Goodbye Script 4"");"
5552
};
5653

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+
5764
private readonly Mock<IFileSystem> _fileSystem;
5865

5966
public ProcessFileMethod()
@@ -263,14 +270,8 @@ public void ShouldNotBeAllowedToLoadAfterUsing()
263270
_fileSystem.Setup(x => x.ReadFileLines(It.Is<string>(f => f == "file.csx"))).Returns(file.ToArray());
264271

265272
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"));
269274

270-
Assert.Equal(1, splitOutput.Count(x => x.TrimStart(' ').StartsWith("using ")));
271-
272-
// consider #line directive
273-
Assert.Equal(4, splitOutput.Length);
274275
_fileSystem.Verify(x => x.ReadFileLines(It.Is<string>(i => i == "script4.csx")), Times.Never());
275276
}
276277

Collapse file

‎test/ScriptCs.Core.Tests/LoadLineProcessorTests.cs‎

Copy file name to clipboardExpand all lines: test/ScriptCs.Core.Tests/LoadLineProcessorTests.cs
+4-7Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Should;
1010

1111
using Xunit.Extensions;
12+
using Should.Core.Assertions;
1213

1314
namespace ScriptCs.Tests
1415
{
@@ -52,7 +53,7 @@ public void ShouldReturnFalseOtherwise(IFileParser parser, LoadLineProcessor pro
5253
}
5354

5455
[Theory, ScriptCsAutoData]
55-
public void ShouldReturnTrueButNotParseFileIfAfterCode(
56+
public void ShouldThrowExceptionIfAfterCode(
5657
[Frozen] Mock<IFileParser> parser,
5758
[Frozen] Mock<IFileSystem> fileSystem,
5859
LoadLineProcessor processor)
@@ -65,13 +66,9 @@ public void ShouldReturnTrueButNotParseFileIfAfterCode(
6566
const string FullPath = "C:\\script.csx";
6667

6768
fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath);
68-
69-
// Act
70-
var result = processor.ProcessLine(parser.Object, context, Line, false);
7169

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));
7572
}
7673

7774
[Theory, ScriptCsAutoData]
Collapse file

‎test/ScriptCs.Core.Tests/ReferenceLineProcessorTests.cs‎

Copy file name to clipboardExpand all lines: test/ScriptCs.Core.Tests/ReferenceLineProcessorTests.cs
+4-7Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Should;
66
using Xunit.Extensions;
77
using System.Linq;
8+
using Should.Core.Assertions;
89

910
namespace ScriptCs.Tests
1011
{
@@ -48,7 +49,7 @@ public void ShouldReturnFalseOtherwise(IFileParser parser, ReferenceLineProcesso
4849
}
4950

5051
[Theory, ScriptCsAutoData]
51-
public void ShouldReturnTrueButNotAddReferenceIfAfterCode(
52+
public void ShouldThrowExceptionIfAfterCode(
5253
[Frozen] Mock<IFileSystem> fileSystem,
5354
ReferenceLineProcessor processor,
5455
IFileParser parser)
@@ -62,12 +63,8 @@ public void ShouldReturnTrueButNotAddReferenceIfAfterCode(
6263

6364
fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath);
6465

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));
7168
}
7269

7370
[Theory, ScriptCsAutoData]

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.