public void TestBasicAssertPasses()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestException", "(Test Exception:{0})", "(string message) : ArgumentException");
            TestResourceResult result = builder.Compile();

            result.Assert("TestException", true, "Message");
        }
        public void TestBasicArgumentsRequired()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestException", "(Test Exception:{0})");
            TestResourceResult result = builder.Compile();

            result.CreateException("TestException");
        }
        public void TestFormatStringTypedArgName()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestDouble(double value)", "Test-{0:n2}", "");
            TestResourceResult result = builder.Compile();

            Assert.AreEqual("Test-42.24", result.GetValue("TestDouble", 42.24));
        }
        public void TestFormatStringTypedArgComments()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestInt32", "Test-{0}", "(int value)");
            TestResourceResult result = builder.Compile();

            Assert.AreEqual("Test-42", result.GetValue("TestInt32", 42));
        }
        public void TestFormatStringAnonymousArg()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestString", "Test{0}");
            TestResourceResult result = builder.Compile();

            Assert.AreEqual("TestX123X", result.GetValue("TestString", "X123X"));
        }
        public void TestNonFormatString()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestString", "Test{Value}");
            TestResourceResult result = builder.Compile();

            Assert.AreEqual("Test{Value}", result.GetValue("TestString"));
        }
        public void TestBasicException()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestException", "TestValue");
            TestResourceResult result = builder.Compile();
            Exception          e      = result.CreateException("TestException");

            Assert.AreEqual("TestValue", e.Message);
        }
        public void TestFormatStringTypedArgConflictWinner()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            //if both name and comments specify arguments, the name wins
            builder.Add("TestString(string value)", "Test-{0:n2}", "(int value)");
            TestResourceResult result = builder.Compile();

            Assert.AreEqual("Test-Value", result.GetValue("TestString", "Value"));
        }
        public void TestBasicTypeArgumentsInName()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestException(string message)", "(Test:{0})");
            TestResourceResult result = builder.Compile();
            Exception          e      = result.CreateException("TestException", "Message");

            Assert.AreEqual("(Test:Message)", e.Message);
        }
        public void TestMemberNotExposed()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestException(int extraData)", "(Test:{0})");
            TestResourceResult result = builder.Compile();
            Exception          e      = result.CreateException("TestException", 42);

            Assert.IsNull(e.GetType().GetProperty("extradata", BindingFlags.Public | BindingFlags.GetProperty |
                                                  BindingFlags.Instance | BindingFlags.IgnoreCase));
        }
        public void TestWithInnerException()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestException()", "(Test)");
            TestResourceResult result = builder.Compile();

            Exception inner = new Exception("My inner exception");
            Exception e     = result.CreateException("TestException", inner);

            Assert.AreEqual(inner, e.InnerException);
        }
        public void TestDerivedException()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("BaseException", "{0}");
            builder.Add("TestException(string message)", "(Test:{0})", ": BaseException");
            TestResourceResult result = builder.Compile();

            Exception e = result.CreateException("TestException", "Message");

            Assert.AreEqual("TestNs.BaseException", e.GetType().BaseType.FullName);
            Assert.AreEqual("(Test:Message)", e.Message);
        }
        public void TestDerivedExceptionWithInterface()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            //The parameter starting with a capital 'E'xtraData denotes that this should be stored and a property accessor exposted.
            builder.Add("TestException(int ExtraData)", "(Test:{0})", ": ApplicationException, CSharpTest.Net.GeneratorsTest.IHaveExtraData<int>");
            TestResourceResult result = builder.Compile();

            Exception e = result.CreateException("TestException", 42);

            Assert.AreEqual(typeof(ApplicationException), e.GetType().BaseType);
            Assert.IsTrue(e is IHaveExtraData <int>, "Interface not found: IHaveExtraData<int>");
            Assert.AreEqual("(Test:42)", e.Message);
            Assert.AreEqual(42, ((IHaveExtraData <int>)e).ExtraData);
        }
        public void TestBasicAssertFails()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestException", "(Test Exception:{0})", "(string message) : ArgumentException");
            TestResourceResult result = builder.Compile();

            try
            {
                result.Assert("TestException", false, "Message");
            }
            catch (ArgumentException ae)
            {
                Assert.AreEqual("TestNs.TestException", ae.GetType().FullName);
                throw;
            }
        }
        public void TestFormatStringTypedArgOverloads()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("Test(string value)", "Test-{0}");
            builder.Add("Test(int value)", "Test-{0}");
            builder.Add("Test(double value)", "Test-{0:n3}");
            builder.Add("Test(System.Version value)", "Test-{0}");
            builder.Add("Test(System.Uri value)", "Test-{0}");

            TestResourceResult result = builder.Compile();

            Assert.AreEqual("Test-Value", result.GetValue("Test", "Value"));
            Assert.AreEqual("Test-123", result.GetValue("Test", 123));
            Assert.AreEqual("Test-123.321", result.GetValue("Test", 123.321));
            Assert.AreEqual("Test-1.2.3.4", result.GetValue("Test", new Version(1, 2, 3, 4)));
            Assert.AreEqual("Test-http://csharptest.net/blog", result.GetValue("Test", new Uri("http://csharptest.net/blog", UriKind.Absolute)));
        }
        public void TestBasicTypeArgumentOverloads()
        {
            Exception           e;
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            builder.Add("TestException", "(Test)");
            builder.Add("TestException(string message)", "(Test:{0})");
            builder.Add("TestException(double value)", "(Test:{0:n2})");
            TestResourceResult result = builder.Compile();

            e = result.CreateException("TestException");
            Assert.AreEqual("(Test)", e.Message);

            e = result.CreateException("TestException", "Message");
            Assert.AreEqual("(Test:Message)", e.Message);

            e = result.CreateException("TestException", 1.2);
            Assert.AreEqual("(Test:1.20)", e.Message);
        }
        public void TestSimpleResXLogEvent()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            string messageText = String.Format("NUnit {0} {1}", GetType(), Guid.NewGuid());

            //Enables the LOG feature to automagically generate log calls on all exception constructors.
            builder.Add(".AutoLog", true);
            builder.Add(".EventSource", "CSharpTest - NUnit");

            //If .NextMessageId is not defined you must specify one for logging to enable on that item.
            builder.Add("SimpleLog(string text)", "{0}", "#MessageId=1");

            TestResourceResult result = builder.Compile();

            Assert.AreEqual(messageText, result.GetValue("SimpleLog", messageText));

            using (EventLog applog = new EventLog("Application"))
            {
                EventLogEntry           found   = null;
                EventLogEntryCollection entries = applog.Entries;
                int stop = Math.Max(0, entries.Count - 50);
                for (int i = entries.Count - 1; i >= stop; i--)
                {
                    if (entries[i].Message.Contains(messageText))
                    {
                        found = entries[i];
                        break;
                    }
                }
                Assert.IsNotNull(found);
                Assert.AreEqual("CSharpTest - NUnit", found.Source);
                Assert.AreEqual(1, found.ReplacementStrings.Length);
                Assert.AreEqual(messageText, found.ReplacementStrings[0]);
            }
        }