Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
... ... @@ -1,56 +1,40 @@
1   -using System.Collections.Generic;
  1 +using System;
  2 +using System.Collections.Generic;
2 3 using System.Linq;
3   -using System.IO;
4   -using dotless.Core;
5   -using System.IO.Abstractions;
6   -using System.Text.RegularExpressions;
7   -
8   -namespace Pretzel.Logic.Minification
9   -{
10   - public class CssMinifier
11   - {
12   - private readonly static Regex IMPORT_PATTERN = new Regex(@"@import +url\(([""']){0,1}(.*?)\1{0,1}\);", RegexOptions.Compiled | RegexOptions.IgnoreCase);
13   -
14   - private readonly IFileSystem fileSystem;
15   - private readonly IEnumerable<FileInfo> files;
16   - private readonly string outputPath;
17   -
18   - public CssMinifier(IFileSystem fileSystem, IEnumerable<FileInfo> files, string outputPath)
19   - {
20   - this.files = files;
21   - this.outputPath = outputPath;
22   - this.fileSystem = fileSystem;
23   - }
24   -
25   - public void Minify()
26   - {
27   - var bundled = fileSystem.BundleFiles(files);
28   -
29   - //todo resolve imports
30   - //_fileSystem.Directory.SetCurrentDirectory("");
31   -
32   - //minify
33   - var engineFactory = new EngineFactory();
34   - engineFactory.Configuration.MinifyOutput = true;
35   -
36   - var engine = engineFactory.GetEngine();
37   - var minified = engine.TransformToCss(bundled, files.First().FullName);
38   -
39   - //build file
40   - fileSystem.File.WriteAllText(outputPath, minified);
41   - }
42   -
43   - public string ProcessCss(FileInfo file)
44   - {
45   - var content = fileSystem.File.ReadAllText(file.FullName);
46   -
47   - //todo resolve imports
48   - //_fileSystem.Directory.SetCurrentDirectory("");
49   - var engineFactory = new EngineFactory();
50   - engineFactory.Configuration.MinifyOutput = true;
51   - var engine = engineFactory.GetEngine();
52   -
53   - return engine.TransformToCss(content, file.FullName);
54   - }
55   - }
56   -}
  4 +using System.IO;
  5 +using dotless.Core;
  6 +using System.IO.Abstractions;
  7 +
  8 +namespace Pretzel.Logic.Minification
  9 +{
  10 + public class CssMinifier
  11 + {
  12 + private readonly IFileSystem fileSystem;
  13 + private readonly IEnumerable<FileInfo> files;
  14 + private readonly string outputPath;
  15 + private readonly Func<ILessEngine> getEngine;
  16 +
  17 + public CssMinifier(IFileSystem fileSystem, IEnumerable<FileInfo> files, string outputPath, Func<ILessEngine> getEngine)
  18 + {
  19 + this.files = files;
  20 + this.outputPath = outputPath;
  21 + this.getEngine = getEngine;
  22 + this.fileSystem = fileSystem;
  23 + }
  24 +
  25 + public void Minify()
  26 + {
  27 + var bundled = fileSystem.BundleFiles(files);
  28 + var engine = getEngine();
  29 + var minified = engine.TransformToCss(bundled, files.First().FullName);
  30 + fileSystem.File.WriteAllText(outputPath, minified);
  31 + }
  32 +
  33 + public string ProcessCss(FileInfo file)
  34 + {
  35 + var content = fileSystem.File.ReadAllText(file.FullName);
  36 + var engine = getEngine();
  37 + return engine.TransformToCss(content, file.FullName);
  38 + }
  39 + }
  40 +}
77 src/Pretzel.Tests/Minification/CssMinificationTests.cs
... ... @@ -1,23 +1,16 @@
1   -using System;
2   -using System.Collections.Generic;
3   -using System.Linq;
4   -using System.Text;
5   -using Xunit;
6   -using System.IO.Abstractions.TestingHelpers;
7   -using Pretzel.Logic.Minification;
8   -using System.IO;
9   -
  1 +using System.Collections.Generic;
  2 +using System.IO;
  3 +using System.IO.Abstractions.TestingHelpers;
  4 +using System.Text;
  5 +using Pretzel.Logic.Minification;
  6 +using Xunit;
  7 +
10 8 namespace Pretzel.Tests.Minification
11   -{
  9 +{
12 10 public class CssMinificationTests
13 11 {
14 12 private const string _outputPath = @"c:\css\output.css";
15 13
16   - public CssMinificationTests()
17   - {
18   -
19   - }
20   -
21 14 [Fact]
22 15 public void Should_Minify_Single_File()
23 16 {
@@ -25,11 +18,14 @@ public void Should_Minify_Single_File()
25 18 var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
26 19 {
27 20 { filepath, new MockFileData("a { color: Red; }") }
28   - });
29   -
30   - var files = new List<FileInfo>() { new FileInfo(filepath) };
  21 + });
31 22
32   - var minifier = new CssMinifier(fileSystem, files, _outputPath);
  23 + var files = new List<FileInfo> { new FileInfo(filepath) };
  24 +
  25 + var factory = new TestContainerFactory();
  26 + var engine = factory.GetEngine(fileSystem, @"c:\css");
  27 +
  28 + var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
33 29
34 30 var result = minifier.ProcessCss(new FileInfo(filepath));
35 31
@@ -55,11 +51,14 @@ public void Should_Compile_Single_Less_File()
55 51 var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
56 52 {
57 53 { filepath, new MockFileData(lessContent) }
58   - });
59   -
60   - var files = new List<FileInfo>() { new FileInfo(filepath) };
61   -
62   - var minifier = new CssMinifier(fileSystem, files, _outputPath);
  54 + });
  55 +
  56 + var files = new List<FileInfo> { new FileInfo(filepath) };
  57 +
  58 + var factory = new TestContainerFactory();
  59 + var engine = factory.GetEngine(fileSystem, @"c:\css");
  60 +
  61 + var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
63 62 var minified = minifier.ProcessCss(new FileInfo(filepath));
64 63
65 64 Assert.Equal(lessOutput, minified);
@@ -74,9 +73,12 @@ public void Should_Write_Single_File_To_Output_Path()
74 73 { filepath, new MockFileData("a { color: Red; }") }
75 74 });
76 75
77   - var files = new List<FileInfo>() { new FileInfo(filepath) };
  76 + var files = new List<FileInfo>() { new FileInfo(filepath) };
  77 +
  78 + var factory = new TestContainerFactory();
  79 + var engine = factory.GetEngine(fileSystem, @"c:\css");
78 80
79   - var minifier = new CssMinifier(fileSystem, files, _outputPath);
  81 + var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
80 82 minifier.Minify();
81 83
82 84 var minifiedFile = fileSystem.File.ReadAllText(_outputPath, Encoding.UTF8);
@@ -96,9 +98,12 @@ public void Should_Combine_Files_To_Output_Path()
96 98 { filepath2, new MockFileData("a { color: Blue; }") }
97 99 });
98 100
99   - var files = new List<FileInfo>() { new FileInfo(filepath1), new FileInfo(filepath2) };
  101 + var files = new List<FileInfo> { new FileInfo(filepath1), new FileInfo(filepath2) };
  102 +
  103 + var factory = new TestContainerFactory();
  104 + var engine = factory.GetEngine(fileSystem, @"c:\css");
100 105
101   - var minifier = new CssMinifier(fileSystem, files, _outputPath);
  106 + var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
102 107 minifier.Minify();
103 108
104 109 var expectedOutput = @"a{color:Red}a{color:Blue}";
@@ -129,9 +134,12 @@ public void Should_Compile_Less_To_Css_To_Output_Path()
129 134 { filepath, new MockFileData(lessContent) }
130 135 });
131 136
132   - var files = new List<FileInfo>() { new FileInfo(filepath) };
  137 + var files = new List<FileInfo>() { new FileInfo(filepath) };
  138 +
  139 + var factory = new TestContainerFactory();
  140 + var engine = factory.GetEngine(fileSystem, @"c:\css");
133 141
134   - var minifier = new CssMinifier(fileSystem, files, _outputPath);
  142 + var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
135 143 minifier.Minify();
136 144
137 145 var minifiedFile = fileSystem.File.ReadAllText(_outputPath, Encoding.UTF8);
@@ -151,11 +159,14 @@ public void Should_Process_Less_Imports()
151 159 { filepath2, new MockFileData("@import \"style.less\"; a { color: @brand_color; }") }
152 160 });
153 161
154   - var files = new List<FileInfo>() { new FileInfo(filepath1), new FileInfo(filepath2) };
  162 + var files = new List<FileInfo> { new FileInfo(filepath1), new FileInfo(filepath2) };
  163 +
  164 + var factory = new TestContainerFactory();
  165 + var engine = factory.GetEngine(fileSystem, @"c:\css");
155 166
156   - var minifier = new CssMinifier(fileSystem, files, _outputPath);
  167 + var minifier = new CssMinifier(fileSystem, files, _outputPath, () => engine);
157 168
158   - var expectedOutput = @"a{color:#4D926F}";
  169 + var expectedOutput = @"a{color:#4d926f}";
159 170
160 171 var minifiedFile = minifier.ProcessCss(new FileInfo(filepath2));
161 172
27 src/Pretzel.Tests/Minification/TestContainerFactory.cs
... ... @@ -0,0 +1,27 @@
  1 +using System.IO.Abstractions;
  2 +using dotless.Core;
  3 +using dotless.Core.Importers;
  4 +using dotless.Core.Input;
  5 +using dotless.Core.Loggers;
  6 +using dotless.Core.Parser;
  7 +using dotless.Core.Stylizers;
  8 +
  9 +namespace Pretzel.Tests.Minification
  10 +{
  11 + public class TestContainerFactory
  12 + {
  13 + public ILessEngine GetEngine(IFileSystem fileSystem, string directory)
  14 + {
  15 + IStylizer stylizer = new HtmlStylizer();
  16 +
  17 + IPathResolver pathResolver = new TestPathResolver(directory);
  18 + IFileReader reader = new TestFileReader(fileSystem, pathResolver);
  19 + var importer = new Importer(reader);
  20 + var parser = new Parser(stylizer, importer);
  21 + ILogger logger = new ConsoleLogger(LogLevel.Error);
  22 + var engine = new LessEngine(parser, logger, true);
  23 + engine.Compress = true;
  24 + return engine;
  25 + }
  26 + }
  27 +}
29 src/Pretzel.Tests/Minification/TestFileReader.cs
... ... @@ -0,0 +1,29 @@
  1 +using System.IO.Abstractions;
  2 +using dotless.Core.Input;
  3 +
  4 +namespace Pretzel.Tests.Minification
  5 +{
  6 + public class TestFileReader : IFileReader
  7 + {
  8 + private readonly IFileSystem fileSystem;
  9 + private readonly IPathResolver pathResolver;
  10 +
  11 + public TestFileReader(IFileSystem fileSystem, IPathResolver pathResolver)
  12 + {
  13 + this.fileSystem = fileSystem;
  14 + this.pathResolver = pathResolver;
  15 + }
  16 +
  17 + public string GetFileContents(string fileName)
  18 + {
  19 + var path = pathResolver.GetFullPath(fileName);
  20 + return fileSystem.File.ReadAllText(path);
  21 + }
  22 +
  23 + public bool DoesFileExist(string fileName)
  24 + {
  25 + var path = pathResolver.GetFullPath(fileName);
  26 + return fileSystem.File.Exists(path);
  27 + }
  28 + }
  29 +}
20 src/Pretzel.Tests/Minification/TestPathResolver.cs
... ... @@ -0,0 +1,20 @@
  1 +using System.IO;
  2 +using dotless.Core.Input;
  3 +
  4 +namespace Pretzel.Tests.Minification
  5 +{
  6 + public class TestPathResolver : IPathResolver
  7 + {
  8 + private readonly string directory;
  9 +
  10 + public TestPathResolver(string directory)
  11 + {
  12 + this.directory = directory;
  13 + }
  14 +
  15 + public string GetFullPath(string path)
  16 + {
  17 + return Path.Combine(directory, path);
  18 + }
  19 + }
  20 +}
4 src/Pretzel.Tests/Pretzel.Tests.csproj
@@ -33,6 +33,7 @@
33 33 <WarningLevel>4</WarningLevel>
34 34 </PropertyGroup>
35 35 <ItemGroup>
  36 + <Reference Include="dotless.Core, Version=1.2.2.0, Culture=neutral, PublicKeyToken=96b446c9e63eae34, processorArchitecture=MSIL" />
36 37 <Reference Include="DotLiquid, Version=1.6.1.0, Culture=neutral, PublicKeyToken=82e46016ecf9f07c, processorArchitecture=MSIL" />
37 38 <Reference Include="System" />
38 39 <Reference Include="System.Core" />
@@ -54,6 +55,9 @@
54 55 <ItemGroup>
55 56 <Compile Include="Minification\CssMinificationTests.cs" />
56 57 <Compile Include="Minification\JsMinificationTests.cs" />
  58 + <Compile Include="Minification\TestContainerFactory.cs" />
  59 + <Compile Include="Minification\TestFileReader.cs" />
  60 + <Compile Include="Minification\TestPathResolver.cs" />
57 61 <Compile Include="PretzelContext.cs" />
58 62 <Compile Include="Properties\AssemblyInfo.cs" />
59 63 <Compile Include="Recipe\RecipeTests.cs" />

0 comments on commit 6059665

Please sign in to comment.
Something went wrong with that request. Please try again.