Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LolaLollipop committed Nov 22, 2023
1 parent 9db1089 commit 7b006a0
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 62 deletions.
39 changes: 0 additions & 39 deletions RueI/RueI/DefaultParser.cs

This file was deleted.

6 changes: 3 additions & 3 deletions RueI/RueI/Elements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DynamicElement(GetContent contentGetter, float position, int zIndex = 0)
public int ZIndex { get; set; }

/// <inheritdoc/>
public Parser Parser { get; set; } = DefaultParser.Parser;
public Parser Parser { get; set; } = Parser.DefaultParser;

/// <inheritdoc/>
public ParsedData ParsedData => Parser.Parse(ContentGetter());
Expand Down Expand Up @@ -75,7 +75,7 @@ public SetElement(float position, string content = "")
public int ZIndex { get; set; } = 0;

/// <inheritdoc/>
public Parser Parser { get; set; } = DefaultParser.Parser;
public Parser Parser { get; set; } = Parser.DefaultParser;

/// <summary>
/// Sets the content of this element.
Expand Down Expand Up @@ -116,6 +116,6 @@ public interface IElement
/// <summary>
/// Gets or sets the <see cref="Parser"/> currently in use by this <see cref="IElement"/>.
/// </summary>
/// <remarks>Implementations should default this to <see cref="DefaultParser.Parser"/>.</remarks>
/// <remarks>Implementations should default this to <see cref="Parser.DefaultParser"/>.</remarks>
public Parser Parser { get; set; }
}
7 changes: 7 additions & 0 deletions RueI/RueI/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -44,6 +46,11 @@ internal Parser(IEnumerable<RichTextTag> tags)
Tags = new(dictionary);
}

/// <summary>
/// Gets the default <see cref="RueI.Parser"/>.
/// </summary>
public static Parser DefaultParser { get; } = new ParserBuilder().AddFromAssembly(typeof(Parser).Assembly).Build();

/// <summary>
/// Gets the tags that will be searched for when parsing.
/// </summary>
Expand Down
28 changes: 25 additions & 3 deletions RueI/RueI/Parsing/ParserBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NorthwoodLib.Pools;
using RueI.Parsing;
using RueI.Parsing.Tags;
using System.Reflection;

/// <summary>
/// Builds <see cref="Parser"/>s.
Expand All @@ -11,16 +12,37 @@ public sealed class ParserBuilder
{
private readonly List<RichTextTag> currentTags = ListPool<RichTextTag>.Shared.Rent(10);

/// <summary>
/// Initializes a new instance of the <see cref="ParserBuilder"/> class.
/// </summary>
public ParserBuilder()
{
}

/// <summary>
/// Gets the number of tags within this <see cref="ParserBuilder"/>.
/// </summary>
public int TagsCount=> currentTags.Count;
public int TagsCount => currentTags.Count;

/// <summary>
/// Initializes a new instance of the <see cref="ParserBuilder"/> class.
/// Adds new <see cref="RichTextTag"/>s from an assembly by getting all of the <see cref="RichTextTagAttribute"/> classes.
/// </summary>
public ParserBuilder()
/// <param name="assembly">The <see cref="Assembly"/> to get the classes from.</param>
/// <returns>A reference to this <see cref="ParserBuilder"/>.</returns>
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<object>());
}
}

return this;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion RueI/RueI/Parsing/Tags/RichTextTagAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
/// Defines a <see cref="RichTextTag"/> for RueI.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
internal class RichTextTagAttribute : Attribute
public class RichTextTagAttribute : Attribute
{
}
65 changes: 49 additions & 16 deletions RueITests/TestTags.cs
Original file line number Diff line number Diff line change
@@ -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<RealTag>.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);
}
}
}
///"\"hello world - - - again\""

0 comments on commit 7b006a0

Please sign in to comment.