Skip to content

Commit

Permalink
Migrated task unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
benquarmby committed Aug 29, 2015
1 parent 9dedbdc commit 4385073
Show file tree
Hide file tree
Showing 14 changed files with 488 additions and 168 deletions.
21 changes: 16 additions & 5 deletions specifications/JSLintNet.Specifications/FileSystemWrapperStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,38 @@ public class FileSystemWrapperStub : IFileSystemWrapper
{
public FileSystemWrapperStub()
{
this.Files = new Dictionary<string, string>();
this.TextFiles = new Dictionary<string, string>();
this.FileResults = new Dictionary<string, List<string>>();
}

public Dictionary<string, string> Files { get; set; }
public Dictionary<string, string> TextFiles { get; set; }

public Dictionary<string, List<string>> FileResults { get; set; }

public void AddFile(string directory, string fileName, string contents)
{
if (!this.FileResults.ContainsKey(directory))
{
this.FileResults[directory] = new List<string>();
}

this.FileResults[directory].Add(fileName);
this.TextFiles.Add(Path.Combine(directory, fileName), contents);
}

public string ReadAllText(string path, Encoding encoding)
{
return this.Files[path];
return this.TextFiles[path];
}

public bool FileExists(string path)
{
return this.Files.ContainsKey(path);
return this.TextFiles.ContainsKey(path);
}

public void WriteAllText(string path, string contents, Encoding encoding)
{
this.Files[path] = contents;
this.TextFiles[path] = contents;
}

public string ResolveFile(string path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,21 @@
<Compile Include="FileSystemWrapperStub.cs" />
<Compile Include="JSLintContextIntegration.cs" />
<Compile Include="JSLintContextUnit.cs" />
<Compile Include="JSLintTaskFixture.cs" />
<Compile Include="JSLintTaskTests.cs" />
<Compile Include="Tasks\JSLintTaskFixture.cs" />
<Compile Include="Tasks\JSLintTaskTests.cs" />
<Compile Include="Settings\JSLintNetSettingsUnit.cs" />
<Compile Include="JSLintOptionsUnit.cs" />
<Compile Include="Json\JsonProviderIntegration.cs" />
<Compile Include="JSLintTaskIntegration.cs" />
<Compile Include="JSLintTaskUnit.cs" />
<Compile Include="Tasks\JSLintTaskIntegration.cs" />
<Compile Include="Tasks\JSLintTaskUnit.cs" />
<Compile Include="Models\TaskFileUnit.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings\SettingsRepositoryUnit.cs" />
<Compile Include="Tasks\TaskResources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>TaskResources.resx</DependentUpon>
</Compile>
<Compile Include="UI\SettingsViewModelUnit.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -125,6 +130,8 @@
<None Include="Resources\Warnings.json" />
<None Include="Resources\NoSourceDirectory.proj" />
<None Include="Resources\Warnings.proj" />
<None Include="Tasks\Resources\MessageSettings.json" />
<None Include="Tasks\Resources\WarningSettings.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\source\JSLintNet\JSLintNet.csproj">
Expand All @@ -136,7 +143,13 @@
<Name>JSLintNet.QualityTools</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<EmbeddedResource Include="Tasks\TaskResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TaskResources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Content Include="Resources\Scripts\ignored.js" />
<Content Include="Resources\Scripts\iife.js" />
Expand All @@ -145,6 +158,9 @@
<Content Include="Resources\Scripts\Libraries\jquery-1.9.1.min.js" />
<Content Include="Resources\Scripts\Modules\Calculator.js" />
<Content Include="Resources\Scripts\objectliteral.js" />
<Content Include="Tasks\Resources\AmdNoStrict.js" />
<Content Include="Tasks\Resources\GlobalVariableDef.js" />
<Content Include="Tasks\Resources\WindowGlobalStrict.js" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
Expand Down
36 changes: 0 additions & 36 deletions specifications/JSLintNet.Specifications/JSLintTaskTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace JSLintNet.Specifications
namespace JSLintNet.Specifications.Tasks
{
using System;
using JSLintNet.Abstractions;
using JSLintNet.QualityTools;

Expand All @@ -15,6 +16,18 @@ public JSLintTaskFixture()

public FileSystemWrapperStub FileSystemWrapper { get; set; }

public void AddFile(string fileName, string contents)
{
var sourceDirectory = this.Instance.SourceDirectory;

if (string.IsNullOrEmpty(sourceDirectory))
{
throw new InvalidOperationException("Cannot add a file without first setting the source directory.");
}

this.FileSystemWrapper.AddFile(sourceDirectory, fileName, contents);
}

protected override void BeforeResolve()
{
base.BeforeResolve();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace JSLintNet.Specifications
namespace JSLintNet.Specifications.Tasks
{
using System;
using System.IO;
Expand Down
153 changes: 153 additions & 0 deletions specifications/JSLintNet.Specifications/Tasks/JSLintTaskTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
namespace JSLintNet.Specifications.Tasks
{
using JSLintNet.QualityTools.Expectations;
using Xunit;

[Trait("Category", "Unit")]
public class JSLintTaskTests
{
[Fact(DisplayName = "Should log JSLint errors at the correct line and column")]
public void CorrectLineColumn()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = "Some Path";
fixture.AddFile("file1.js", TaskResources.WindowGlobalStrict);

var actual = fixture.Instance.Execute();
var firstError = fixture.BuildEngine.ErrorEvents[0];

Assert.False(actual);
Assert.Equal(2, firstError.LineNumber);
Assert.Equal(13, firstError.ColumnNumber);
}
}

[Fact(DisplayName = "Should log each JSLint warning as a task error")]
public void TaskErrors()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = @"\\SHARE\path\";
fixture.AddFile("first.js", TaskResources.WindowGlobalStrict);
fixture.AddFile("second.js", TaskResources.AmdNoStrict);

fixture.Instance.Execute();

I.Expect(fixture.BuildEngine.ErrorEvents.Count).ToBe(3);
I.Expect(fixture.BuildEngine.WarningEvents.Count).ToBe(0);
I.Expect(fixture.BuildEngine.MessageEvents.Count).ToBe(0);
}
}

[Fact(DisplayName = "Should log each JSLint warning as a task warning when outputting warnings")]
public void TaskWarnings()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = @"\\SHARE\path\";
fixture.AddFile("JSLintNet.json", TaskResources.WarningSettings);
fixture.AddFile("first.js", TaskResources.WindowGlobalStrict);
fixture.AddFile("second.js", TaskResources.AmdNoStrict);

fixture.Instance.Execute();

I.Expect(fixture.BuildEngine.ErrorEvents.Count).ToBe(0);
I.Expect(fixture.BuildEngine.WarningEvents.Count).ToBe(3);
I.Expect(fixture.BuildEngine.MessageEvents.Count).ToBe(0);
}
}

[Fact(DisplayName = "Should log each JSLint warning as a task message when outputting messages")]
public void TaskMessages()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = @"\\SHARE\path\";
fixture.AddFile("JSLintNet.json", TaskResources.MessageSettings);
fixture.AddFile("first.js", TaskResources.WindowGlobalStrict);
fixture.AddFile("second.js", TaskResources.AmdNoStrict);

fixture.Instance.Execute();

I.Expect(fixture.BuildEngine.ErrorEvents.Count).ToBe(0);
I.Expect(fixture.BuildEngine.WarningEvents.Count).ToBe(0);
I.Expect(fixture.BuildEngine.MessageEvents.Count).ToBe(3);
}
}

[Fact(DisplayName = "Should return true when no files found")]
public void NoFilesOk()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = @"C:\Source\";

var actual = fixture.Instance.Execute();

I.Expect(actual).ToBeTrue();
}
}

[Fact(DisplayName = "Should return true when no files contain errors")]
public void NoErrorsOk()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = @"Some Path";
fixture.AddFile("file1.js", TaskResources.GlobalVariableDef);

var actual = fixture.Instance.Execute();

I.Expect(actual).ToBeTrue();
}
}

[Fact(DisplayName = "Should return false when one error found")]
public void OneErrorFail()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = @"D:\Solution\Project Dir";
fixture.AddFile("startup.js", TaskResources.WindowGlobalStrict);

var actual = fixture.Instance.Execute();

I.Expect(fixture.Instance.ErrorCount).ToBe(1);
I.Expect(actual).ToBeFalse();
}
}

[Fact(DisplayName = "Should return false when many errors found")]
public void ManyErrorsFail()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = @"D:\Solution\Project Dir";
fixture.AddFile("first.js", TaskResources.WindowGlobalStrict);
fixture.AddFile("second.js", TaskResources.AmdNoStrict);

var actual = fixture.Instance.Execute();

I.Expect(fixture.Instance.ErrorCount).ToBe(3);
I.Expect(actual).ToBeFalse();
}
}

[Fact(DisplayName = "Should return true when errors found but treating errors as warnings")]
public void WarningsOk()
{
using (var fixture = new JSLintTaskFixture())
{
fixture.Instance.SourceDirectory = @"Z:\Funky Path\repo-of-doom";
fixture.AddFile("JSLintNet.json", TaskResources.WarningSettings);
fixture.AddFile("second.js", TaskResources.AmdNoStrict);

var actual = fixture.Instance.Execute();

I.Expect(fixture.Instance.ErrorCount).ToBeGreaterThan(0);
I.Expect(actual).ToBeTrue();
}
}
}
}
Loading

0 comments on commit 4385073

Please sign in to comment.