// Copyright (c) Wojciech Figat. All rights reserved. #if FLAX_TESTS using System.Collections.Generic; using FlaxEngine.Utilities; using NUnit.Framework; namespace FlaxEngine.Tests { [TestFixture] public class TestHtmlParser { [TestCase("")] [TestCase("a")] [TestCase("a\na")] [TestCase("a\ra")] [TestCase("")] [TestCase("")] [TestCase("")] [TestCase("b")] [TestCase("")] public void TestValid(string html) { var parser = new HtmlParser(html); while (parser.ParseNext(out var tag)) { Assert.IsNotNull(tag.Name); Assert.IsNotNull(tag.Attributes); } } [TestCase("bb", ExpectedResult = new[] { "a" })] [TestCase("bb", ExpectedResult = new[] { "a" })] [TestCase("bb", ExpectedResult = new[] { "a" })] [TestCase("b", ExpectedResult = new[] { "a", "a" })] [TestCase("b", ExpectedResult = new[] { "a", "a" })] [TestCase("b", ExpectedResult = new[] { "a", "a" })] public string[] TestTags(string html) { var tags = new List(); var parser = new HtmlParser(html); while (parser.ParseNext(out var tag)) { tags.Add(tag.Name); } return tags.ToArray(); } [TestCase("bb", ExpectedResult = "size=50")] [TestCase("bb", ExpectedResult = "size=50,len=60")] [TestCase("bb", ExpectedResult = "size=50")] [TestCase("bb", ExpectedResult = "size=5 0")] [TestCase("bb", ExpectedResult = "size=50%")] [TestCase("bb", ExpectedResult = "size=#FF")] [TestCase("bb", ExpectedResult = "=value")] public string TestAttributes(string html) { var result = string.Empty; var parser = new HtmlParser(html); while (parser.ParseNext(out var tag)) { foreach (var e in tag.Attributes) { if (result.Length != 0) result += ','; result += e.Key + "=" + e.Value; } } return result; } [TestCase("b", ExpectedResult = false)] [TestCase("b", ExpectedResult = true)] [TestCase("b", ExpectedResult = true)] [TestCase("b", ExpectedResult = true)] [TestCase("b", ExpectedResult = false)] [TestCase("b", ExpectedResult = true)] [TestCase("b", ExpectedResult = true)] public bool TestEnding(string html) { var parser = new HtmlParser(html); while (parser.ParseNext(out var tag)) { return tag.IsSlash; } throw new System.Exception(); } [TestCase("b", ExpectedResult = 1)] [TestCase("sd b ", ExpectedResult = 5)] [TestCase("sd b ", ExpectedResult = 5)] [TestCase("sd b ", ExpectedResult = 5)] public int TestStartPosition(string html) { var parser = new HtmlParser(html); while (parser.ParseNext(out var tag)) { return tag.StartPosition; } return -1; } [TestCase("b", ExpectedResult = 4)] [TestCase("b", ExpectedResult = 5)] [TestCase("b", ExpectedResult = 6)] [TestCase("b", ExpectedResult = 7)] [TestCase("b", ExpectedResult = 12)] [TestCase("b", ExpectedResult = 14)] public int TestEndPosition(string html) { var parser = new HtmlParser(html); while (parser.ParseNext(out var tag)) { return tag.EndPosition; } return -1; } } } #endif