using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Svg.UnitTests
{
///
/// 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
///
[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]*/";
///
/// We encountered issues in the example file that were caused by an empty d tag in some of the elements
///
[TestMethod]
public void Lexer_FileWithEmptyDAttribute_Success()
{
var xml = GetXMLDocFromResource(GetFullResourceString(ResourceStringEmptyDTagFile));
var doc = OpenSvg(xml);
}
///
/// Stylesheet lexer fails if there is an important after a hex value, this tests the 3 and 6 lenght variants in the file
///
[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 ;");
}
///
///Reference test if there is an important after a non-hex value (should never fail) and on hex without an important
///
[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()
{
// valid colors
var doc = GenerateLexerTestFile("fill: #ff0000; stroke: #ffff00");
var path = doc.GetElementById("path1");
Assert.AreEqual(System.Drawing.Color.Red, ((SvgColourServer)path.Fill).Colour);
Assert.AreEqual(System.Drawing.Color.Yellow, ((SvgColourServer)path.Stroke).Colour);
// invalid/valid color combinations - default color is used for each invalid color
doc = GenerateLexerTestFile("fill: #ff00; stroke: #00ff00");
path = doc.GetElementById("path1");
// default fill color is Black
Assert.AreEqual(System.Drawing.Color.Black, ((SvgColourServer)path.Fill).Colour);
Assert.AreEqual(System.Drawing.Color.Lime, ((SvgColourServer)path.Stroke).Colour);
doc = GenerateLexerTestFile("fill: #fff; stroke: 005577");
path = doc.GetElementById("path1");
Assert.AreEqual(System.Drawing.Color.White, ((SvgColourServer)path.Fill).Colour);
// default stroke color is null (transparent)
Assert.IsNull(path.Stroke);
doc = GenerateLexerTestFile("fill:; stroke: #00557;");
path = doc.GetElementById("path1");
Assert.AreEqual(System.Drawing.Color.Black, ((SvgColourServer)path.Fill).Colour);
Assert.IsNull(path.Stroke);
}
///
/// Load the lexer template and replace the token with a test value and try generating the svg
///
/// Content to test in the template
///
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;
}
}
}