Skip to content

Commit 63d6823

Browse files
committed
Unit tests for scriptcs#630
1 parent 2e3ffd1 commit 63d6823

3 files changed

Lines changed: 88 additions & 2 deletions

File tree

src/ScriptCs.Core/FilePreProcessor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,10 @@ private void InDirectory(string path, Action action)
137137

138138
private bool IsNonDirectiveLine(string line)
139139
{
140-
var trimmedLine = line.TrimStart(' ');
141140
var directiveLineProcessors =
142141
_lineProcessors.Where(lp => lp is IDirectiveLineProcessor).Select(lp => lp as DirectiveLineProcessor);
143142

144-
return line.Trim() != string.Empty && !directiveLineProcessors.Any(lp => lp.Matches(trimmedLine));
143+
return line.Trim() != string.Empty && !directiveLineProcessors.Any(lp => lp.Matches(line));
145144
}
146145

147146
private static bool IsUsingLine(string line)

test/ScriptCs.Core.Tests/DirectiveLineProcessorTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ public void ShouldReturnTrueAndContinueProcessingDirectiveIfAfterCodeAndBehaviou
3434
}
3535
}
3636

37+
public class TheMatchesMethod
38+
{
39+
[Fact]
40+
public void ShouldReturnTrueWhenLineMatchesDirectiveString()
41+
{
42+
var directiveLineProcessor = new TestableDirectiveLineProcessor();
43+
directiveLineProcessor.Matches("#Test x").ShouldBeTrue();
44+
}
45+
46+
[Fact]
47+
public void ShouldReturnFalseWhenLineDoesNotMatchDirectiveString()
48+
{
49+
var directiveLineProcessor = new TestableDirectiveLineProcessor();
50+
directiveLineProcessor.Matches("#NotATest x").ShouldBeFalse();
51+
}
52+
}
53+
3754
public class TestableDirectiveLineProcessor : DirectiveLineProcessor
3855
{
3956
private BehaviorAfterCode? _behaviourAfterCode;

test/ScriptCs.Core.Tests/FileProcessorTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,5 +573,75 @@ private IFilePreProcessor GetFilePreProcessor()
573573
return new FilePreProcessor(_fileSystem.Object, Mock.Of<ILog>(), lineProcessors);
574574
}
575575
}
576+
577+
public class TheParseScriptMethod
578+
{
579+
private readonly Mock<IFileSystem> _fileSystem;
580+
581+
public TheParseScriptMethod()
582+
{
583+
_fileSystem = new Mock<IFileSystem>();
584+
_fileSystem.SetupGet(x => x.NewLine).Returns(Environment.NewLine);
585+
_fileSystem.Setup(fs => fs.GetFullPath(It.IsAny<string>())).Returns<string>((path) => path);
586+
}
587+
588+
[Fact]
589+
public void ShouldProcessCustomDirectiveWhenItComesBeforeCode()
590+
{
591+
var testableDirectiveProcessor = new DirectiveLineProcessorTests.TestableDirectiveLineProcessor();
592+
var filePreprocessor = GetFilePreProcessor(testableDirectiveProcessor, new LoadLineProcessor(_fileSystem.Object));
593+
var lines = new List<string>
594+
{
595+
"#Test something",
596+
"Console.WriteLine(\"Success\");"
597+
};
598+
599+
filePreprocessor.ParseScript(lines, new FileParserContext());
600+
testableDirectiveProcessor.InheritedProcessLineCalled.ShouldBeTrue();
601+
}
602+
603+
[Fact]
604+
public void ShouldProcessALoadDirectiveWhenItComesAfterACustomDirective()
605+
{
606+
var testableDirectiveProcessor = new DirectiveLineProcessorTests.TestableDirectiveLineProcessor();
607+
var loadLineProcessor = new TestableLoadLineProcessor(_fileSystem.Object);
608+
var filePreprocessor = GetFilePreProcessor(testableDirectiveProcessor, loadLineProcessor);
609+
var lines = new List<string>
610+
{
611+
"#Test something",
612+
"#load myscript.csx",
613+
"Console.WriteLine(\"Success\");"
614+
};
615+
616+
filePreprocessor.ParseScript(lines, new FileParserContext());
617+
loadLineProcessor.InheritedProcessLineCalled.ShouldBeTrue();
618+
}
619+
620+
private IFilePreProcessor GetFilePreProcessor(ILineProcessor customDirectiveProcessor, ILineProcessor loadLineProcessor)
621+
{
622+
var lineProcessors = new ILineProcessor[]
623+
{
624+
new UsingLineProcessor(),
625+
new ReferenceLineProcessor(_fileSystem.Object),
626+
loadLineProcessor,
627+
customDirectiveProcessor
628+
};
629+
630+
return new FilePreProcessor(_fileSystem.Object, Mock.Of<ILog>(), lineProcessors);
631+
}
632+
633+
public class TestableLoadLineProcessor : LoadLineProcessor
634+
{
635+
public TestableLoadLineProcessor(IFileSystem fileSystem)
636+
: base(fileSystem)
637+
{ }
638+
public bool InheritedProcessLineCalled { get; private set; }
639+
protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
640+
{
641+
InheritedProcessLineCalled = true;
642+
return true;
643+
}
644+
}
645+
}
576646
}
577647
}

0 commit comments

Comments
 (0)