diff --git a/RueI/RueI/DefaultParser.cs b/RueI/RueI/DefaultParser.cs deleted file mode 100644 index cfb6338..0000000 --- a/RueI/RueI/DefaultParser.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace RueI; - -using System.Reflection; -using RueI.Parsing; -using RueI.Parsing.Tags; - -/// -/// Provides the default and main for RueI. -/// -public static class DefaultParser -{ - /// - /// Gets the default . - /// - public static Parser Parser { get; } = GetParser(typeof(DefaultParser).Assembly); - - /// - /// Gets a new from an assembly by getting all of the classes. - /// - /// A new . - /// This method is used for unit testing. - private static Parser GetParser(Assembly assembly) - { - ParserBuilder builder = new(); - - MethodInfo addTag = typeof(ParserBuilder).GetMethod(nameof(ParserBuilder.AddTag)); - - foreach (Type type in assembly.GetTypes()) - { - if (type.GetCustomAttributes(typeof(RichTextTagAttribute), true).Any() && type.IsSubclassOf(typeof(RichTextTag))) - { - MethodInfo generic = addTag.MakeGenericMethod(type); - generic.Invoke(builder, Array.Empty()); - } - } - - return builder.Build(); - } -} \ No newline at end of file diff --git a/RueI/RueI/Elements.cs b/RueI/RueI/Elements.cs index 893fc10..02a80e5 100644 --- a/RueI/RueI/Elements.cs +++ b/RueI/RueI/Elements.cs @@ -38,7 +38,7 @@ public DynamicElement(GetContent contentGetter, float position, int zIndex = 0) public int ZIndex { get; set; } /// - public Parser Parser { get; set; } = DefaultParser.Parser; + public Parser Parser { get; set; } = Parser.DefaultParser; /// public ParsedData ParsedData => Parser.Parse(ContentGetter()); @@ -75,7 +75,7 @@ public SetElement(float position, string content = "") public int ZIndex { get; set; } = 0; /// - public Parser Parser { get; set; } = DefaultParser.Parser; + public Parser Parser { get; set; } = Parser.DefaultParser; /// /// Sets the content of this element. @@ -116,6 +116,6 @@ public interface IElement /// /// Gets or sets the currently in use by this . /// - /// Implementations should default this to . + /// Implementations should default this to . public Parser Parser { get; set; } } \ No newline at end of file diff --git a/RueI/RueI/Parsing/Parser.cs b/RueI/RueI/Parsing/Parser.cs index c40def7..1e6020c 100644 --- a/RueI/RueI/Parsing/Parser.cs +++ b/RueI/RueI/Parsing/Parser.cs @@ -2,11 +2,13 @@ using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Reflection; using System.Text; using NorthwoodLib.Pools; using RueI.Enums; using RueI.Parsing; +using RueI.Parsing.Tags; using RueI.Records; /// @@ -44,6 +46,11 @@ internal Parser(IEnumerable tags) Tags = new(dictionary); } + /// + /// Gets the default . + /// + public static Parser DefaultParser { get; } = new ParserBuilder().AddFromAssembly(typeof(Parser).Assembly).Build(); + /// /// Gets the tags that will be searched for when parsing. /// diff --git a/RueI/RueI/Parsing/ParserBuilder.cs b/RueI/RueI/Parsing/ParserBuilder.cs index 1065dde..f16aaca 100644 --- a/RueI/RueI/Parsing/ParserBuilder.cs +++ b/RueI/RueI/Parsing/ParserBuilder.cs @@ -3,6 +3,7 @@ using NorthwoodLib.Pools; using RueI.Parsing; using RueI.Parsing.Tags; +using System.Reflection; /// /// Builds s. @@ -11,16 +12,37 @@ public sealed class ParserBuilder { private readonly List currentTags = ListPool.Shared.Rent(10); + /// + /// Initializes a new instance of the class. + /// + public ParserBuilder() + { + } + /// /// Gets the number of tags within this . /// - public int TagsCount=> currentTags.Count; + public int TagsCount => currentTags.Count; /// - /// Initializes a new instance of the class. + /// Adds new s from an assembly by getting all of the classes. /// - public ParserBuilder() + /// The to get the classes from. + /// A reference to this . + public ParserBuilder AddFromAssembly(Assembly assembly) { + MethodInfo addTag = typeof(ParserBuilder).GetMethod(nameof(AddTag)); + + foreach (Type type in assembly.GetTypes()) + { + if (type.GetCustomAttributes(typeof(RichTextTagAttribute), true).Any() && type.IsSubclassOf(typeof(RichTextTag))) + { + MethodInfo generic = addTag.MakeGenericMethod(type); + generic.Invoke(this, Array.Empty()); + } + } + + return this; } /// diff --git a/RueI/RueI/Parsing/Tags/RichTextTagAttribute.cs b/RueI/RueI/Parsing/Tags/RichTextTagAttribute.cs index 812ec2d..9fbd7e0 100644 --- a/RueI/RueI/Parsing/Tags/RichTextTagAttribute.cs +++ b/RueI/RueI/Parsing/Tags/RichTextTagAttribute.cs @@ -4,6 +4,6 @@ /// Defines a for RueI. /// [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] -internal class RichTextTagAttribute : Attribute +public class RichTextTagAttribute : Attribute { } \ No newline at end of file diff --git a/RueITests/TestTags.cs b/RueITests/TestTags.cs index 668727f..33b93b7 100644 --- a/RueITests/TestTags.cs +++ b/RueITests/TestTags.cs @@ -1,24 +1,57 @@ +using RueI; +using RueI.Parsing; using RueI.Parsing.Tags; +using System.Linq; -namespace RueITest +namespace RueITest; + +[RichTextTag] +public class NotTag +{ +} + +[RichTextTag] +public class RealTag : NoParamsTag { - [TestClass] - public class TestTags + public override string[] Names { get; } = { "hello" }; + + public override bool HandleTag(ParserContext context) => throw new NotImplementedException(); +} + +[TestClass] +public class TestTags +{ + [TestMethod] + public void TestTagBuilding() { - [TestMethod] - public void TestUtility() - { - string? shouldExist = TagHelpers.ExtractFromQuotations("hello world"); - string? shouldExist2 = TagHelpers.ExtractFromQuotations("\"hello world - - - again\""); + ParserBuilder builder = new(); - string? shouldNull = TagHelpers.ExtractFromQuotations("\"hello !!! \nworld"); - string? shouldNull2 = TagHelpers.ExtractFromQuotations("hello world again\""); + builder.AddFromAssembly(typeof(TestTags).Assembly); + Parser parser = builder.Build(); - Assert.IsNotNull(shouldExist); - Assert.IsNotNull(shouldExist2); + Assert.AreEqual(1, parser.Tags.Count); + Assert.IsTrue(parser.Tags.Values.Any(x => x.Contains(SharedTag.Singleton))); + } + + [TestMethod] + [DataRow("\"hello !!! \nworld")] + [DataRow("hello world again\"")] + public void TestQuoteFailures(string input) + { + string? shouldNull = TagHelpers.ExtractFromQuotations(input); + + Assert.IsNull(shouldNull); + } + + [TestMethod] + [DataRow("\"hello world - - - again\"")] + [DataRow("hello \n\n\nworld")] + [DataRow("\"y")] + public void TestQuoteSuccess(string input) + { + string? shouldExist = TagHelpers.ExtractFromQuotations(input); - Assert.IsNull(shouldNull); - Assert.IsNull(shouldNull2); - } + Assert.IsNotNull(shouldExist); } -} \ No newline at end of file +} +///"\"hello world - - - again\"" \ No newline at end of file