Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #29 from shiftkey/FixingDotLessTest

Fixing the last dotLess test
  • Loading branch information...
commit 6059665c187718e74df04aa121d060de987d3c47 2 parents 2243a71 + cdd60c2
Paul Jenkins authored
94 src/Pretzel.Logic/Minification/CssMinifier.cs
View
@@ -1,56 +1,40 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.Linq;
-using System.IO;
-using dotless.Core;
-using System.IO.Abstractions;
-using System.Text.RegularExpressions;
-
-namespace Pretzel.Logic.Minification
-{
- public class CssMinifier
- {
- private readonly static Regex IMPORT_PATTERN = new Regex(@"@import +url\(([""']){0,1}(.*?)\1{0,1}\);", RegexOptions.Compiled | RegexOptions.IgnoreCase);
-
- private readonly IFileSystem fileSystem;
- private readonly IEnumerable<FileInfo> files;
- private readonly string outputPath;
-
- public CssMinifier(IFileSystem fileSystem, IEnumerable<FileInfo> files, string outputPath)
- {
- this.files = files;
- this.outputPath = outputPath;
- this.fileSystem = fileSystem;
- }
-
- public void Minify()
- {
- var bundled = fileSystem.BundleFiles(files);
-
- //todo resolve imports
- //_fileSystem.Directory.SetCurrentDirectory("");
-
- //minify
- var engineFactory = new EngineFactory();
- engineFactory.Configuration.MinifyOutput = true;
-
- var engine = engineFactory.GetEngine();
- var minified = engine.TransformToCss(bundled, files.First().FullName);
-
- //build file
- fileSystem.File.WriteAllText(outputPath, minified);
- }
-
- public string ProcessCss(FileInfo file)
- {
- var content = fileSystem.File.ReadAllText(file.FullName);
-
- //todo resolve imports
- //_fileSystem.Directory.SetCurrentDirectory("");
- var engineFactory = new EngineFactory();
- engineFactory.Configuration.MinifyOutput = true;
- var engine = engineFactory.GetEngine();
-
- return engine.TransformToCss(content, file.FullName);
- }
- }
-}
+using System.IO;
+using dotless.Core;
+using System.IO.Abstractions;
+
+namespace Pretzel.Logic.Minification
+{
+ public class CssMinifier
+ {
+ private readonly IFileSystem fileSystem;
+ private readonly IEnumerable<FileInfo> files;
+ private readonly string outputPath;
+ private readonly Func<ILessEngine> getEngine;
+
+ public CssMinifier(IFileSystem fileSystem, IEnumerable<FileInfo> files, string outputPath, Func<ILessEngine> getEngine)
+ {
+ this.files = files;
+ this.outputPath = outputPath;
+ this.getEngine = getEngine;
+ this.fileSystem = fileSystem;
+ }
+
+ public void Minify()
+ {
+ var bundled = fileSystem.BundleFiles(files);
+ var engine = getEngine();
+ var minified = engine.TransformToCss(bundled, files.First().FullName);
+ fileSystem.File.WriteAllText(outputPath, minified);
+ }
+
+ public string ProcessCss(FileInfo file)
+ {
+ var content = fileSystem.File.ReadAllText(file.FullName);
+ var engine = getEngine();
+ return engine.TransformToCss(content, file.FullName);
+ }
+ }
+}
77 src/Pretzel.Tests/Minification/CssMinificationTests.cs
View
@@ -1,23 +1,16 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Xunit;
-using System.IO.Abstractions.TestingHelpers;
-using Pretzel.Logic.Minification;
-using System.IO;
-
+using System.Collections.Generic;
+using System.IO;
+using System.IO.Abstractions.TestingHelpers;
+using System.Text;
+using Pretzel.Logic.Minification;
+using Xunit;
+
namespace Pretzel.Tests.Minification
-{
+{
public class CssMinificationTests
{
private const string _outputPath = @"c:\css\output.css";
- public CssMinificationTests()
- {
-
- }
-
[Fact]
public void Should_Minify_Single_File()
{
@@ -25,11 +18,14 @@ public void Should_Minify_Single_File()
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filepath, new MockFileData("a { color: Red; }") }
- });
-
- var files = new List<FileInfo>() { new FileInfo(filepath) };
+ });
- var minifier = new CssMinifier(fileSystem, files, _outputPath);
+ var files = new List<FileInfo> { new FileInfo(filepath) };
+
+ var factory = new TestContainerFactory();
+ var engine = factory.GetEngine(fileSystem, @"c:\css");
+
+ var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
var result = minifier.ProcessCss(new FileInfo(filepath));
@@ -55,11 +51,14 @@ public void Should_Compile_Single_Less_File()
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filepath, new MockFileData(lessContent) }
- });
-
- var files = new List<FileInfo>() { new FileInfo(filepath) };
-
- var minifier = new CssMinifier(fileSystem, files, _outputPath);
+ });
+
+ var files = new List<FileInfo> { new FileInfo(filepath) };
+
+ var factory = new TestContainerFactory();
+ var engine = factory.GetEngine(fileSystem, @"c:\css");
+
+ var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
var minified = minifier.ProcessCss(new FileInfo(filepath));
Assert.Equal(lessOutput, minified);
@@ -74,9 +73,12 @@ public void Should_Write_Single_File_To_Output_Path()
{ filepath, new MockFileData("a { color: Red; }") }
});
- var files = new List<FileInfo>() { new FileInfo(filepath) };
+ var files = new List<FileInfo>() { new FileInfo(filepath) };
+
+ var factory = new TestContainerFactory();
+ var engine = factory.GetEngine(fileSystem, @"c:\css");
- var minifier = new CssMinifier(fileSystem, files, _outputPath);
+ var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
minifier.Minify();
var minifiedFile = fileSystem.File.ReadAllText(_outputPath, Encoding.UTF8);
@@ -96,9 +98,12 @@ public void Should_Combine_Files_To_Output_Path()
{ filepath2, new MockFileData("a { color: Blue; }") }
});
- var files = new List<FileInfo>() { new FileInfo(filepath1), new FileInfo(filepath2) };
+ var files = new List<FileInfo> { new FileInfo(filepath1), new FileInfo(filepath2) };
+
+ var factory = new TestContainerFactory();
+ var engine = factory.GetEngine(fileSystem, @"c:\css");
- var minifier = new CssMinifier(fileSystem, files, _outputPath);
+ var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
minifier.Minify();
var expectedOutput = @"a{color:Red}a{color:Blue}";
@@ -129,9 +134,12 @@ public void Should_Compile_Less_To_Css_To_Output_Path()
{ filepath, new MockFileData(lessContent) }
});
- var files = new List<FileInfo>() { new FileInfo(filepath) };
+ var files = new List<FileInfo>() { new FileInfo(filepath) };
+
+ var factory = new TestContainerFactory();
+ var engine = factory.GetEngine(fileSystem, @"c:\css");
- var minifier = new CssMinifier(fileSystem, files, _outputPath);
+ var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
minifier.Minify();
var minifiedFile = fileSystem.File.ReadAllText(_outputPath, Encoding.UTF8);
@@ -151,11 +159,14 @@ public void Should_Process_Less_Imports()
{ filepath2, new MockFileData("@import \"style.less\"; a { color: @brand_color; }") }
});
- var files = new List<FileInfo>() { new FileInfo(filepath1), new FileInfo(filepath2) };
+ var files = new List<FileInfo> { new FileInfo(filepath1), new FileInfo(filepath2) };
+
+ var factory = new TestContainerFactory();
+ var engine = factory.GetEngine(fileSystem, @"c:\css");
- var minifier = new CssMinifier(fileSystem, files, _outputPath);
+ var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
- var expectedOutput = @"a{color:#4D926F}";
+ var expectedOutput = @"a{color:#4d926f}";
var minifiedFile = minifier.ProcessCss(new FileInfo(filepath2));
27 src/Pretzel.Tests/Minification/TestContainerFactory.cs
View
@@ -0,0 +1,27 @@
+using System.IO.Abstractions;
+using dotless.Core;
+using dotless.Core.Importers;
+using dotless.Core.Input;
+using dotless.Core.Loggers;
+using dotless.Core.Parser;
+using dotless.Core.Stylizers;
+
+namespace Pretzel.Tests.Minification
+{
+ public class TestContainerFactory
+ {
+ public ILessEngine GetEngine(IFileSystem fileSystem, string directory)
+ {
+ IStylizer stylizer = new HtmlStylizer();
+
+ IPathResolver pathResolver = new TestPathResolver(directory);
+ IFileReader reader = new TestFileReader(fileSystem, pathResolver);
+ var importer = new Importer(reader);
+ var parser = new Parser(stylizer, importer);
+ ILogger logger = new ConsoleLogger(LogLevel.Error);
+ var engine = new LessEngine(parser, logger, true);
+ engine.Compress = true;
+ return engine;
+ }
+ }
+}
29 src/Pretzel.Tests/Minification/TestFileReader.cs
View
@@ -0,0 +1,29 @@
+using System.IO.Abstractions;
+using dotless.Core.Input;
+
+namespace Pretzel.Tests.Minification
+{
+ public class TestFileReader : IFileReader
+ {
+ private readonly IFileSystem fileSystem;
+ private readonly IPathResolver pathResolver;
+
+ public TestFileReader(IFileSystem fileSystem, IPathResolver pathResolver)
+ {
+ this.fileSystem = fileSystem;
+ this.pathResolver = pathResolver;
+ }
+
+ public string GetFileContents(string fileName)
+ {
+ var path = pathResolver.GetFullPath(fileName);
+ return fileSystem.File.ReadAllText(path);
+ }
+
+ public bool DoesFileExist(string fileName)
+ {
+ var path = pathResolver.GetFullPath(fileName);
+ return fileSystem.File.Exists(path);
+ }
+ }
+}
20 src/Pretzel.Tests/Minification/TestPathResolver.cs
View
@@ -0,0 +1,20 @@
+using System.IO;
+using dotless.Core.Input;
+
+namespace Pretzel.Tests.Minification
+{
+ public class TestPathResolver : IPathResolver
+ {
+ private readonly string directory;
+
+ public TestPathResolver(string directory)
+ {
+ this.directory = directory;
+ }
+
+ public string GetFullPath(string path)
+ {
+ return Path.Combine(directory, path);
+ }
+ }
+}
4 src/Pretzel.Tests/Pretzel.Tests.csproj
View
@@ -33,6 +33,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="dotless.Core, Version=1.2.2.0, Culture=neutral, PublicKeyToken=96b446c9e63eae34, processorArchitecture=MSIL" />
<Reference Include="DotLiquid, Version=1.6.1.0, Culture=neutral, PublicKeyToken=82e46016ecf9f07c, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core" />
@@ -54,6 +55,9 @@
<ItemGroup>
<Compile Include="Minification\CssMinificationTests.cs" />
<Compile Include="Minification\JsMinificationTests.cs" />
+ <Compile Include="Minification\TestContainerFactory.cs" />
+ <Compile Include="Minification\TestFileReader.cs" />
+ <Compile Include="Minification\TestPathResolver.cs" />
<Compile Include="PretzelContext.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Recipe\RecipeTests.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.