LexerIssueTests.cs 4.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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()
        {
            //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()
        {
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
            // valid colors
            var doc = GenerateLexerTestFile("fill: #ff0000; stroke: #ffff00");
            var path = doc.GetElementById<SvgPath>("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<SvgPath>("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<SvgPath>("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<SvgPath>("path1");
            Assert.AreEqual(System.Drawing.Color.Black, ((SvgColourServer)path.Fill).Colour);
            Assert.IsNull(path.Stroke);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        }

        /// <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;
        }

    }
}