Skip to content

Commit

Permalink
Refactor code to be more Object Oriented
Browse files Browse the repository at this point in the history
- Move all the minification to an extension to StreamReader
- Get rid of global var _features
- Extend the Features class with a constructor and add MaxLength
  • Loading branch information
heldersepu committed Apr 18, 2016
1 parent 3a68c6a commit 61c373f
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 308 deletions.
4 changes: 2 additions & 2 deletions HtmlMinifier.Tests/ArgumentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void FindValuesInArgs_WithIgnores_ShouldReturnCorrectly()
argsList.Add("ignorejscomments");

// Act
Features disabledFeatures = Program.FindValuesInArgs(argsList.ToArray());
Features disabledFeatures = new Features(argsList.ToArray());

// Assert
Assert.That(disabledFeatures.IgnoreHtmlComments, Is.True);
Expand All @@ -30,7 +30,7 @@ public void FindValuesInArgs_WithOneIgnore_ShouldReturnCorrectly()
argsList.Add("ignorehtmlcomments");

// Act
Features disabledFeatures = Program.FindValuesInArgs(argsList.ToArray());
Features disabledFeatures = new Features(argsList.ToArray());

// Assert
Assert.That(disabledFeatures.IgnoreHtmlComments, Is.True);
Expand Down
3 changes: 3 additions & 0 deletions HtmlMinifier.Tests/HtmlMinifier.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<Content Include="Data\StandardResult.txt" />
<Content Include="Data\Standard.txt" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
85 changes: 17 additions & 68 deletions HtmlMinifier.Tests/MinificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
public class MinificationTests
{
readonly string _testDataFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\Data");
readonly Features noFeatures = new Features(new List<string>().ToArray());


[Test]
public void ReadHtml_WithStandardText_ShouldReturnCorrectly()
Expand All @@ -19,11 +21,7 @@ public void ReadHtml_WithStandardText_ShouldReturnCorrectly()
string expectedResult = DataHelpers.StandardResult;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.Standard);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, null);

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.Standard, noFeatures);

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -36,11 +34,7 @@ public void MinifyContents_WithComments_ShouldReturnCorrectly()
string expectedResult = DataHelpers.CommentsResult;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.Comments);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, null);

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.Comments, noFeatures);

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -53,11 +47,7 @@ public void MinifyContents_WithModelList_ShouldReturnCorrectly()
string expectedResult = DataHelpers.ModelListResult;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.ModelList);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, null);

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.ModelList, noFeatures);

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -70,11 +60,7 @@ public void MinifyContents_WithLanguageSpecficCharacters_ShouldReturnCorrectly()
string expectedResult = DataHelpers.LanguageSpecificCharactersResult;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.LanguageSpecificCharacters);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, null);

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.LanguageSpecificCharacters, noFeatures);

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -88,11 +74,7 @@ public void GithubIssue10__ShouldReturnCorrectly()
string expectedResult = DataHelpers.GithubIssue10Result;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.GithubIssue10);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, null);

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.GithubIssue10, noFeatures);

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -105,11 +87,7 @@ public void GithubIssue13__ShouldReturnCorrectly()
string expectedResult = DataHelpers.GithubIssue13Result;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.GithubIssue13);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, null);

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.GithubIssue13, noFeatures);

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -124,11 +102,7 @@ public void SixtyFiveKCharacters__ShouldBreakToNextLine()
string expectedResult = DataHelpers.SixtyFiveThousandCharactersResult;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.SixtyFiveThousandCharacters);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, args.ToArray());

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.SixtyFiveThousandCharacters, new Features(args.ToArray()));

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -143,11 +117,7 @@ public void SixtyFiveKCharacters__WithoutArgs_ShouldMakeNoChange()
string expectedResult = DataHelpers.SixtyFiveThousandCharactersNoBreakResult;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.SixtyFiveThousandCharacters);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, args.ToArray());

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.SixtyFiveThousandCharacters, new Features(args.ToArray()));

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -160,7 +130,7 @@ public void RemoveJavaScriptComments_WithStandardText_ShouldReturnCorrectly()
string expectedResult = DataHelpers.JavaScriptCommentsResult;

// Act
string removedComments = Program.RemoveJavaScriptComments(DataHelpers.JavaScriptComments);
string removedComments = StreamReaderExtension.RemoveJavaScriptComments(DataHelpers.JavaScriptComments);

// Assert
Assert.That(removedComments, Is.EqualTo(expectedResult));
Expand All @@ -173,7 +143,7 @@ public void RemoveMultipleJavaScriptComments_WithStandardText_ShouldReturnCorrec
string expectedResult = DataHelpers.MultipleJavaScriptCommentsResult;

// Act
string removedComments = Program.RemoveJavaScriptComments(DataHelpers.MultipleJavaScriptComments);
string removedComments = StreamReaderExtension.RemoveJavaScriptComments(DataHelpers.MultipleJavaScriptComments);

// Assert
Assert.That(removedComments, Is.EqualTo(expectedResult));
Expand All @@ -186,11 +156,7 @@ public void GithubIssue19Inherits__ShouldReturnCorrectly()
string expectedResult = DataHelpers.GithubIssue19InheritsResult;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.GithubIssue19Inherits);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, null);

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.GithubIssue19Inherits, noFeatures);

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -203,11 +169,7 @@ public void GithubIssue19Multiple__ShouldReturnCorrectly()
string expectedResult = DataHelpers.GithubIssue19MultipleResult;

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.GithubIssue19Multiple);

minifiedHtml = Program.EnsureMaxLength(minifiedHtml, null);

minifiedHtml = Program.ReArrangeDeclarations(minifiedHtml);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.GithubIssue19Multiple, noFeatures);

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
Expand All @@ -218,28 +180,15 @@ public void GithubIssue23__ShouldReturnCorrectly()
{
// A fix for a Github issue - https://github.com/deanhume/html-minifier/issues/23
string expectedResult = DataHelpers.GithubIssue23Result;
Program._features.IgnoreHtmlComments = true;
// test IgnoreHtmlComments
List<string> args = new List<string> { "ignorehtmlcomments" };

// Act
string minifiedHtml = Program.MinifyHtml(DataHelpers.GithubIssue23);
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.GithubIssue23, new Features(args.ToArray()));

// Assert
Assert.That(minifiedHtml, Is.EqualTo(expectedResult));
}

#region Helpers

public string ReadFileContents(string filePath)
{
string result;
using (var reader = new StreamReader(filePath))
{
result = reader.ReadToEnd();
}

return result;
}

#endregion
}
}
39 changes: 36 additions & 3 deletions ViewMinifier/Features.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
namespace HtmlMinifier
using System.Linq;

namespace HtmlMinifier
{
public class Features
{
/// <summary>
/// Check the arguments passed in to determine if we should enable or disable any features.
/// </summary>
/// <param name="args">The arguments passed in.</param>
public Features(string[] args)
{
if (args.Contains("ignorehtmlcomments"))
{
IgnoreHtmlComments = true;
}

if (args.Contains("ignorejscomments"))
{
IgnoreJsComments = true;
}

int maxLength = 0;
// This is a check to see if the args contain an optional parameter for the max line length
if (args != null && args.Length > 1)
{
// Try and parse the value sent through
int.TryParse(args[1], out maxLength);
}
MaxLength = maxLength;
}

/// <summary>
/// Should we ignore the JavaScript comments and not minify?
/// </summary>
public bool IgnoreJsComments { get; set; }
public bool IgnoreJsComments { get; private set; }

/// <summary>
/// Should we ignore the html comments and not minify?
/// </summary>
public bool IgnoreHtmlComments { get; set; }
public bool IgnoreHtmlComments { get; private set; }

/// <summary>
/// Property for the max character count
/// </summary>
public int MaxLength { get; private set; }
}
}
1 change: 1 addition & 0 deletions ViewMinifier/HtmlMinifier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Features.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StreamReaderExtension.cs" />
<Compile Include="StringExtension.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 61c373f

Please sign in to comment.