using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities; using Quadarax.Foundation.Core.Attributes; using Quadarax.Foundation.Core.Exceptions; namespace qdr.fnd.core.test.Exceptions { internal class CodeExceptionTest { const string CS_MESSAGE = "Foo"; const string CS_EN_MESSAGE1 = "Foo message"; const string CS_EN_MESSAGE2 = "Fii message"; const string CS_EN_MESSAGE3 = "Arguments {0},{1}"; const int CS_CODE = 1234; enum TestErrorCodeEnum { [ErrorMessage(CS_EN_MESSAGE1)] Foo = 1, [ErrorMessage(CS_EN_MESSAGE2)] Bar = 2, NotDef = 3, [ErrorMessage(CS_EN_MESSAGE3)] Args = 4 } [Test(TestOf = typeof(CodeException))] public void CheckCodeAndMessageSettingOk() { var ex = new CodeException(CS_CODE, CS_MESSAGE); Assert.That(ex.Code, Is.EqualTo(CS_CODE)); Assert.That(ex.Message, Is.EqualTo(CS_MESSAGE)); Assert.That(ex.InnerException, Is.Null); } [Test(TestOf = typeof(CodeException))] public void CheckCodeAndMessageInnerSettingOk() { var exInner = new Exception("Fee"); var ex = new CodeException(CS_CODE, CS_MESSAGE, exInner); Assert.That(ex.Code, Is.EqualTo(CS_CODE)); Assert.That(ex.Message, Is.EqualTo(CS_MESSAGE)); Assert.That(ex.InnerException, Is.Not.Null); Assert.That(ex.InnerException!.Message, Is.EqualTo(ex.InnerException.Message)); } [Test(TestOf = typeof(CodeException))] public void CheckGenericCodeOk() { var ex = new CodeException(TestErrorCodeEnum.Foo); Assert.That(ex.Code, Is.EqualTo(TestErrorCodeEnum.Foo)); Assert.That(ex.Message, Is.EqualTo(CS_EN_MESSAGE1)); Assert.That(ex.InnerException, Is.Null); } [Test(TestOf = typeof(CodeException))] public void CheckGenericCodeArguments() { var ex = new CodeException(TestErrorCodeEnum.Args,1,"AAA"); Assert.That(ex.Code, Is.EqualTo(TestErrorCodeEnum.Args)); Assert.That(ex.Message, Is.EqualTo(string.Format(CS_EN_MESSAGE3,1,"AAA"))); Assert.That(ex.InnerException, Is.Null); } [Test(TestOf = typeof(CodeException))] public void CheckGenericCodeNotDefinedOk() { var ex = new CodeException(TestErrorCodeEnum.NotDef); Assert.That(ex.Message, Is.EqualTo($"Error message code: {TestErrorCodeEnum.NotDef} [{Convert.ToInt32(TestErrorCodeEnum.NotDef)}]")); } } }