diff --git a/src/ManiaTemplates/Components/MtComponent.cs b/src/ManiaTemplates/Components/MtComponent.cs index 88ec815..66e2b84 100644 --- a/src/ManiaTemplates/Components/MtComponent.cs +++ b/src/ManiaTemplates/Components/MtComponent.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; +using System.Diagnostics; using System.Xml; +using ManiaTemplates.Exceptions; using ManiaTemplates.Lib; namespace ManiaTemplates.Components; @@ -101,7 +99,7 @@ private static MtComponentImport ParseImportNode(ManiaTemplateEngine engine, Xml if (resource == null) { - throw new Exception($"Missing required attribute 'component' for element '{node.OuterXml}'."); + throw new MissingAttributeException($"Missing required attribute 'component' for element '{node.OuterXml}'."); } tag ??= resource.Split('.')[^2]; @@ -122,7 +120,7 @@ private static string ParseComponentUsingStatement(XmlNode node) if (nameSpace == null) { - throw new Exception($"Missing attribute 'namespace' for element '{node.OuterXml}'."); + throw new MissingAttributeException($"Missing attribute 'namespace' for element '{node.OuterXml}'."); } return nameSpace; @@ -162,12 +160,12 @@ private static MtComponentProperty ParseComponentProperty(XmlNode node) if (name == null) { - throw new Exception($"Missing attribute 'name' for element '{node.OuterXml}'."); + throw new MissingAttributeException($"Missing attribute 'name' for element '{node.OuterXml}'."); } if (type == null) { - throw new Exception($"Missing attribute 'type' for element '{node.OuterXml}'."); + throw new MissingAttributeException($"Missing attribute 'type' for element '{node.OuterXml}'."); } var property = new MtComponentProperty diff --git a/src/ManiaTemplates/Components/MtComponentScript.cs b/src/ManiaTemplates/Components/MtComponentScript.cs index 6adb17e..7a4ab44 100644 --- a/src/ManiaTemplates/Components/MtComponentScript.cs +++ b/src/ManiaTemplates/Components/MtComponentScript.cs @@ -1,4 +1,5 @@ using System.Xml; +using ManiaTemplates.Exceptions; namespace ManiaTemplates.Components; @@ -44,8 +45,8 @@ public static MtComponentScript FromNode(ManiaTemplateEngine engine, XmlNode nod if (content == null) { - throw new Exception( - "Failed to get ManiaScript contents. Script tags need to either specify a body or resource-attribute."); + throw new ManiaScriptSourceMissingException( + "Script tags need to either specify a body or resource-attribute."); } return new MtComponentScript @@ -60,4 +61,4 @@ public int ContentHash() { return Content.GetHashCode(); } -} +} \ No newline at end of file diff --git a/src/ManiaTemplates/Exceptions/ManiaScriptSourceMissingException.cs b/src/ManiaTemplates/Exceptions/ManiaScriptSourceMissingException.cs new file mode 100644 index 0000000..99e45c1 --- /dev/null +++ b/src/ManiaTemplates/Exceptions/ManiaScriptSourceMissingException.cs @@ -0,0 +1,18 @@ +namespace ManiaTemplates.Exceptions; + +public class ManiaScriptSourceMissingException : Exception +{ + public ManiaScriptSourceMissingException() + { + } + + public ManiaScriptSourceMissingException(string message) + : base(message) + { + } + + public ManiaScriptSourceMissingException(string message, Exception inner) + : base(message, inner) + { + } +} \ No newline at end of file diff --git a/src/ManiaTemplates/Exceptions/MissingAttributeException.cs b/src/ManiaTemplates/Exceptions/MissingAttributeException.cs new file mode 100644 index 0000000..a48729e --- /dev/null +++ b/src/ManiaTemplates/Exceptions/MissingAttributeException.cs @@ -0,0 +1,18 @@ +namespace ManiaTemplates.Exceptions; + +public class MissingAttributeException : Exception +{ + public MissingAttributeException() + { + } + + public MissingAttributeException(string message) + : base(message) + { + } + + public MissingAttributeException(string message, Exception inner) + : base(message, inner) + { + } +} \ No newline at end of file diff --git a/tests/ManiaTemplates.Tests/Components/MtComponentMapTest.cs b/tests/ManiaTemplates.Tests/Components/MtComponentMapTest.cs index deb6db7..cdd1de6 100644 --- a/tests/ManiaTemplates.Tests/Components/MtComponentMapTest.cs +++ b/tests/ManiaTemplates.Tests/Components/MtComponentMapTest.cs @@ -6,7 +6,8 @@ public class MtComponentMapTest { private readonly MtComponentMap _mtComponentMap = new(); - public MtComponentMapTest() + [Fact] + public void Should_Contain_Single_Component_Import() { _mtComponentMap["test"] = new MtComponentImport { Tag = "preTest", TemplateKey = "preTest" }; Assert.Single(_mtComponentMap); diff --git a/tests/ManiaTemplates.Tests/Components/MtComponentScriptTest.cs b/tests/ManiaTemplates.Tests/Components/MtComponentScriptTest.cs index 38aec8d..c7220e7 100644 --- a/tests/ManiaTemplates.Tests/Components/MtComponentScriptTest.cs +++ b/tests/ManiaTemplates.Tests/Components/MtComponentScriptTest.cs @@ -1,5 +1,6 @@ using System.Xml; using ManiaTemplates.Components; +using ManiaTemplates.Exceptions; namespace ManiaTemplates.Tests.Components; @@ -40,8 +41,8 @@ public void Should_Fail_To_Load_Empty_ManiaScript(string input) var document = new XmlDocument(); document.LoadXml(input); - var exception = Assert.Throws(() => MtComponentScript.FromNode(_templateEngine, document.DocumentElement!)); - Assert.Equal("Failed to get ManiaScript contents. Script tags need to either specify a body or resource-attribute.", exception.Message); + Assert.Throws(() => + MtComponentScript.FromNode(_templateEngine, document.DocumentElement!)); } [Fact] @@ -51,13 +52,15 @@ public void Should_Load_ManiaScript_With_Different_Hash_Codes() var document1 = new XmlDocument(); document1.LoadXml(firstInput); var script1 = MtComponentScript.FromNode(_templateEngine, document1.DocumentElement!); - + var script1Hash = script1.ContentHash(); + const string secondInput = "