Commit 326dae7c authored by Gertjan van Heertum's avatar Gertjan van Heertum Committed by mrbean-bremen
Browse files

Added test cases for lexer issue #399

- added (base) test cases for the lexer issues and 2 test files. The d="" luckily seems to 
  behave as expected (did crash in earlier versions)
- found the exact line in the CSS that failed (border with !important) and isolated the test
- updated names of the tests and files
- added a reference test for the non important line (which should always work).
- fixed small spelling error in the css parser.
- added trailing whitespace test for #399, which also crashes the lexer 
parent 50e5d1f7
...@@ -55,14 +55,14 @@ namespace Svg.ExCSS ...@@ -55,14 +55,14 @@ namespace Svg.ExCSS
} }
} }
internal void AddSeparator(GrammarSegment termSepertor) internal void AddSeparator(GrammarSegment termSeparator)
{ {
if (_items.Count != _separator.Count + 1) if (_items.Count != _separator.Count + 1)
{ {
throw new NotSupportedException("Must call AddTerm AddSeparator in that order"); throw new NotSupportedException("Must call AddTerm AddSeparator in that order");
} }
_separator.Add(termSepertor); _separator.Add(termSeparator);
} }
public int Length public int Length
......
...@@ -556,7 +556,8 @@ namespace Svg.ExCSS ...@@ -556,7 +556,8 @@ namespace Svg.ExCSS
if(HtmlColor.TryFromHex(color, out htmlColor)) if(HtmlColor.TryFromHex(color, out htmlColor))
return AddTerm(htmlColor); return AddTerm(htmlColor);
return false;
return false;
} }
#region Namespace #region Namespace
......
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Svg.UnitTests
{
/// <summary>
/// Some complexer stylesheets seemed to crash the lexer and result in errors (#399)
/// Found several issues in the lexer and parser of the SVG file.
///
/// This test class will test against these issues to prevent the bug from reoccuring
/// </summary>
[TestClass]
public class LexerIssueTests : SvgTestHelper
{
private const string ResourceStringEmptyDTagFile = "Issue399_LexerIssue.EmptyDTag.svg";
private const string ResourceStringLexerTemplate = "Issue399_LexerIssue.LexerTestTemplate.svg";
private const string LexerTemplateReplaceToken = "/*[REPLACE]*/";
/// <summary>
/// We encountered issues in the example file that were caused by an empty d tag in some of the elements
/// </summary>
[TestMethod]
public void Lexer_FileWithEmptyDAttribute_Success()
{
var xml = GetXMLDocFromResource(GetFullResourceString(ResourceStringEmptyDTagFile));
var doc = OpenSvg(xml);
}
/// <summary>
/// Stylesheet lexer fails if there is an important after a hex value, this tests the 3 and 6 lenght variants in the file
/// </summary>
[TestMethod]
public void Lexer_ImportantAfterHex_Success()
{
Assert.Inconclusive("The important after the hex codes is not working at the moment. Re-enable these tests if the issue is resolved (#399)");
//Important should be valid on 3 digit hex
GenerateLexerTestFile("border-top: 1px solid #333 !important;");
//Important should be valid on 6 digit hex
GenerateLexerTestFile("border-top: 1px solid #009c46 !important;");
//Whitespace should not break the parser
GenerateLexerTestFile("border-bottom: 1px solid #009c46 ;");
}
/// <summary>
///Reference test if there is an important after a non-hex value (should never fail) and on hex without an important
/// </summary>
[TestMethod]
public void Lexer_NoImportantAndImportantAfterNonHex_Success()
{
//Hex is working, if no !important suffixing it (#399)
GenerateLexerTestFile("border-top: 1px solid #009c46;");
//Important is not failing on non-hex value
GenerateLexerTestFile("border-top: 1px solid red !important;");
}
[TestMethod]
public void Lexer_FileWithInvalidHex_ColorTagIsIgnored()
{
Assert.Inconclusive("This test fails due to a lexer error");
GenerateLexerTestFile("color: #0046;");
}
/// <summary>
/// Load the lexer template and replace the token with a test value and try generating the svg
/// </summary>
/// <param name="testContent">Content to test in the template</param>
/// <returns></returns>
private SvgDocument GenerateLexerTestFile(string testContent)
{
var str = GetResourceXmlDocAsString(GetFullResourceString(ResourceStringLexerTemplate));
str = str.Replace(LexerTemplateReplaceToken, testContent);
var xml = GetXMLDocFromString(str);
var doc = OpenSvg(xml);
return doc;
}
}
}
<svg xmlns="http://www.w3.org/2000/svg" width="1092" height="453">
<g transform="translate(40,20)" style="opacity: 1;">
<path class="plot-zero" d="M0,0 H1042" style="opacity: 0;"></path>
<path class="plot-now" d="" style="opacity: 0;"></path>
<path class="plot-now2" d="" style="opacity: 0;"></path>
</g>
<clipPath id="clip-view-2">
<rect width="1042" height="408"></rect>
</clipPath>
<g style="opacity: 0;"></g>
<text class="plot-warning" y="9" x="521" style="opacity: 0;">data loading - 75%</text>
<text class="plot-caption" y="14" x="521" style="opacity: 0;"></text>
</svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" width="1092" height="453">
<style type="text/css">
#footer
{
/*[REPLACE]*/
}
</style>
<g transform="translate(40,20)" style="opacity: 1;">
<path class="plot-zero" d="M0,0 H1042" style="opacity: 0;"></path>
<path class="plot-now" d="" style="opacity: 0;"></path>
</g>
<clipPath id="clip-view-2">
<rect width="1042" height="408"></rect>
</clipPath>
<g style="opacity: 0;"></g>
<text class="plot-warning" y="9" x="521" style="opacity: 0;">data loading - 75%</text>
<text class="plot-caption" y="14" x="521" style="opacity: 0;"></text>
</svg>
\ No newline at end of file
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
<Compile Include="BoundsTests.cs" /> <Compile Include="BoundsTests.cs" />
<Compile Include="CssQueryTest.cs" /> <Compile Include="CssQueryTest.cs" />
<Compile Include="LargeEmbeddedImageTest.cs" /> <Compile Include="LargeEmbeddedImageTest.cs" />
<Compile Include="LexerIssueTests.cs" />
<Compile Include="MarkerEndTest.cs" /> <Compile Include="MarkerEndTest.cs" />
<Compile Include="MetafileRenderingTest.cs" /> <Compile Include="MetafileRenderingTest.cs" />
<Compile Include="MultiThreadingTest.cs" /> <Compile Include="MultiThreadingTest.cs" />
...@@ -107,6 +108,10 @@ ...@@ -107,6 +108,10 @@
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Resources\Issue_Threading\TestFile.svg" /> <EmbeddedResource Include="Resources\Issue_Threading\TestFile.svg" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\Issue399_LexerIssue\LexerTestTemplate.svg" />
<EmbeddedResource Include="Resources\Issue399_LexerIssue\EmptyDTag.svg" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- 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. Other similar extension points exist, see Microsoft.Common.targets.
......
...@@ -4,6 +4,7 @@ using System.Diagnostics; ...@@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Text;
using System.Xml; using System.Xml;
namespace Svg.UnitTests namespace Svg.UnitTests
...@@ -15,7 +16,7 @@ namespace Svg.UnitTests ...@@ -15,7 +16,7 @@ namespace Svg.UnitTests
/// Test file path. /// Test file path.
/// </summary> /// </summary>
[Obsolete("Try not to use the file loader, please use the resource loader to ensure working of tests on all systems")] [Obsolete("Try not to use the file loader, please use the resource loader to ensure working of tests on all systems")]
protected virtual string TestFile protected virtual string TestFile
{ {
get get
{ {
...@@ -129,15 +130,33 @@ namespace Svg.UnitTests ...@@ -129,15 +130,33 @@ namespace Svg.UnitTests
Trace.WriteLine("Done XmlDocument loading from resource data."); Trace.WriteLine("Done XmlDocument loading from resource data.");
return xmlDoc; return xmlDoc;
} }
}
/// <summary>
/// Get embedded resource as string from Unit Test resources.
/// </summary>
/// <param name="fullResourceString">Full Unit Test resource string.</param>
/// <returns>Embedded resource data xml as string.</returns>
protected virtual string GetResourceXmlDocAsString(string fullResourceString)
{
using (var s = GetResourceStream(fullResourceString))
{
Trace.WriteLine("Load XmlDocument content from resource data.");
using (var reader = new StreamReader(s, Encoding.UTF8))
{
string value = reader.ReadToEnd();
Trace.WriteLine("Done XmlDocument content loading from resource data.");
return value;
}
}
} }
/// <summary> /// <summary>
/// Get xml document from <see cref="TestFile"/>. /// Get xml document from <see cref="TestFile"/>.
/// </summary> /// </summary>
/// <returns>File data as xml document.</returns> /// <returns>File data as xml document.</returns>
[Obsolete("Try not to use the file loader, please use the resource loader to ensure working of tests on all systems")] [Obsolete("Try not to use the file loader, please use the resource loader to ensure working of tests on all systems")]
protected virtual XmlDocument GetXMLDocFromFile() protected virtual XmlDocument GetXMLDocFromFile()
{ {
return GetXMLDocFromFile(TestFile); return GetXMLDocFromFile(TestFile);
} }
...@@ -148,8 +167,8 @@ namespace Svg.UnitTests ...@@ -148,8 +167,8 @@ namespace Svg.UnitTests
/// </summary> /// </summary>
/// <param name="file">File to load.</param> /// <param name="file">File to load.</param>
/// <returns>File data as xml document.</returns> /// <returns>File data as xml document.</returns>
[Obsolete("Try not to use the file loader, please use the resource loader to ensure working of tests on all systems")] [Obsolete("Try not to use the file loader, please use the resource loader to ensure working of tests on all systems")]
protected virtual XmlDocument GetXMLDocFromFile(string file) protected virtual XmlDocument GetXMLDocFromFile(string file)
{ {
if (!File.Exists(file)) if (!File.Exists(file))
Assert.Fail("Test file missing.", file); Assert.Fail("Test file missing.", file);
...@@ -157,9 +176,23 @@ namespace Svg.UnitTests ...@@ -157,9 +176,23 @@ namespace Svg.UnitTests
var xmlDoc = new XmlDocument(); var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(File.ReadAllText(file)); xmlDoc.LoadXml(File.ReadAllText(file));
return xmlDoc; return xmlDoc;
}
/// <summary>
/// Get the xml document from an input string
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
protected virtual XmlDocument GetXMLDocFromString(string input)
{
Trace.WriteLine("Load XmlDocument from input data.");
var xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = new SvgDtdResolver();
xmlDoc.LoadXml(input);
Trace.WriteLine("Done XmlDocument loading from resource data.");
return xmlDoc;
} }
/// <summary> /// <summary>
/// Get xml document from <see cref="TestResource"/>. /// Get xml document from <see cref="TestResource"/>.
/// </summary> /// </summary>
...@@ -230,9 +263,9 @@ namespace Svg.UnitTests ...@@ -230,9 +263,9 @@ namespace Svg.UnitTests
{ {
using (var ms = new MemoryStream()) using (var ms = new MemoryStream())
{ {
img.Save(ms, ImageFormat.Png); img.Save(ms, ImageFormat.Png);
ms.Flush(); ms.Flush();
Assert.IsTrue(ms.Length >= ExpectedSize, "Svg file does not match expected minimum size."); Assert.IsTrue(ms.Length >= ExpectedSize, $"Svg file size {ms.Length} does not match expected minimum size (expected {ExpectedSize}).");
} }
} }
...@@ -314,8 +347,8 @@ namespace Svg.UnitTests ...@@ -314,8 +347,8 @@ namespace Svg.UnitTests
// Calculate percentage. // Calculate percentage.
int totalPixelCount = img1.Width * img1.Height; int totalPixelCount = img1.Width * img1.Height;
var imgDiffFactor = ((float)diffPixelCount / totalPixelCount); var imgDiffFactor = ((float)diffPixelCount / totalPixelCount);
imgEqualPercentage = imgDiffFactor * 100; imgEqualPercentage = imgDiffFactor * 100;
return (imgDiffFactor == 1f); return (imgDiffFactor == 1f);
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment