예제 #1
0
        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]);
            }
        }
예제 #2
0
        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"));
        }
예제 #3
0
 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"));
 }
예제 #4
0
        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));
        }
예제 #5
0
        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));
        }
예제 #6
0
        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 TestBasicArgumentsRequired()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

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

            result.CreateException("TestException");
        }
예제 #8
0
        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"));
        }
예제 #9
0
        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"));
        }
예제 #10
0
        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));
        }
예제 #11
0
        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 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 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 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);
        }
예제 #15
0
        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 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.Generators.Test.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);
        }
예제 #20
0
        public void TestInvalidFormatString()
        {
            TextWriter serr = Console.Error;

            try
            {
                Console.SetError(TextWriter.Null);
                TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");
                builder.Add("TestString(string value)", "Test-{0:n2} {}", "(int value)");
                builder.Compile();
            }
            finally
            {
                Console.SetError(serr);
            }
        }
        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;
            }
        }
예제 #22
0
        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);
        }
예제 #24
0
        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]);
            }
        }
예제 #25
0
 public void TestInvalidFormatString()
 {
     TextWriter serr = Console.Error;
     try
     {
         Console.SetError(TextWriter.Null);
         TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");
         builder.Add("TestString(string value)", "Test-{0:n2} {}", "(int value)");
         builder.Compile();
     }
     finally
     {
         Console.SetError(serr);
     }
 }
예제 #26
0
        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)));
        }
예제 #27
0
        public void TestFormatResxOptions()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            //the next message id to use when autoLog is enabled.
            builder.Add(".NextMessageId", 5);

            //the format string for the full hresult to a uri.
            builder.Add(".HelpLink", new Uri("http://mydomain/errorcodes.aspx?id={0:x8}"));

            //Trailing message text for event log, can include {0} formats. which are specified before the message-specific values
            builder.Add(".EventMessageFormat", "Trailing message text for event log, can include {0} formats.");

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

            //The fully-qualified method used to write the log to the event log
            builder.Add(".AutoLogMethod", String.Format("{0}.TestCustomEventWriter", GetType().FullName));

            //The name of the event source to register, can be qualified with log: "Log-Name/Event-Source"
            builder.Add(".EventSource", "Application/YourAppName");

            //OPTIONAL (default=0): The category id and name for the category of the message, should be unique for this ResX file
            builder.Add(".EventCategory", 0x0F, "MyCategory");

            //OPTIONAL (default=0): The facility code (256-2047) and name to define for these messages
            builder.Add(".Facility", 258, "MyFacility");

            builder.Add("TestFormatting(int value)", "TestFormatting-{0:x8}", "#MessageId=3");
            builder.Add("TestWarning", "TestWarning", "(int unprinted) #MessageId=251, Severity=Warn");
            builder.Add("TestException(string s, int i)", "TestException-{0}-{1}", ":System.Runtime.InteropServices.COMException");

            string             code, resx;
            TestResourceResult result;
            StringWriter       captureErr = new StringWriter();
            TextWriter         stderr     = Console.Error;

            try
            {
                Console.SetError(captureErr);
                result = builder.Compile("-reference:" + GetType().Assembly.Location, out code, out resx);
            }
            finally { Console.SetError(stderr); }

            _lastEvent = null;
            Assert.AreEqual("TestFormatting-000e1234", result.GetValue("TestFormatting", 0x0e1234));
            Assert.IsNotNull(_lastEvent);
            Assert.AreEqual("Application", _lastLog.Log);
            Assert.AreEqual("YourAppName", _lastLog.Source);
            Assert.AreEqual(0x0F, _lastEvent.CategoryId);
            Assert.AreEqual(EventLogEntryType.Information, _lastEvent.EntryType);
            Assert.AreEqual(0x41020003L, _lastEvent.InstanceId);
            Assert.AreEqual(1, _lastArgs.Length);
            Assert.AreEqual("000e1234", _lastArgs[0]);

            _lastEvent = null;
            Assert.AreEqual("TestWarning", result.GetValue("TestWarning", 3579));
            Assert.IsNotNull(_lastEvent);
            Assert.AreEqual("Application", _lastLog.Log);
            Assert.AreEqual("YourAppName", _lastLog.Source);
            Assert.AreEqual(0x0F, _lastEvent.CategoryId);
            Assert.AreEqual(EventLogEntryType.Warning, _lastEvent.EntryType);
            Assert.AreEqual(0x810200fbL, _lastEvent.InstanceId);
            Assert.AreEqual(1, _lastArgs.Length);
            Assert.AreEqual("3579", _lastArgs[0]);

            _lastEvent = null;
            COMException error = (COMException)result.CreateException("TestException", "error", 1234);

            Assert.AreEqual("TestException-error-1234", error.Message);
            Assert.AreEqual(unchecked ((int)0xe1020005), error.ErrorCode);//5 was auto-assigned by NextMessageId
            Assert.AreEqual("http://mydomain/errorcodes.aspx?id=e1020005", error.HelpLink);

            Assert.IsNotNull(_lastEvent);
            Assert.AreEqual("Application", _lastLog.Log);
            Assert.AreEqual("YourAppName", _lastLog.Source);
            Assert.AreEqual(0x0F, _lastEvent.CategoryId);
            Assert.AreEqual(EventLogEntryType.Error, _lastEvent.EntryType);
            Assert.AreEqual(0xC1020005L, _lastEvent.InstanceId);
            Assert.AreEqual(2, _lastArgs.Length);
            Assert.AreEqual("error", _lastArgs[0]);
            Assert.AreEqual("1234", _lastArgs[1]);
        }
예제 #28
0
        public void TestFormatResxOptions()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNs", "ResXClass");

            //the next message id to use when autoLog is enabled.
            builder.Add(".NextMessageId", 5);

            //the format string for the full hresult to a uri.
            builder.Add(".HelpLink", new Uri("http://mydomain/errorcodes.aspx?id={0:x8}"));

            //Trailing message text for event log, can include {0} formats. which are specified before the message-specific values
            builder.Add(".EventMessageFormat", "Trailing message text for event log, can include {0} formats.");

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

            //The fully-qualified method used to write the log to the event log
            builder.Add(".AutoLogMethod", String.Format("{0}.TestCustomEventWriter", GetType().FullName));

            //The name of the event source to register, can be qualified with log: "Log-Name/Event-Source"
            builder.Add(".EventSource", "Application/YourAppName");

            //OPTIONAL (default=0): The category id and name for the category of the message, should be unique for this ResX file 
            builder.Add(".EventCategory", 0x0F, "MyCategory");

            //OPTIONAL (default=0): The facility code (256-2047) and name to define for these messages
            builder.Add(".Facility", 258, "MyFacility");

            builder.Add("TestFormatting(int value)", "TestFormatting-{0:x8}", "#MessageId=3");
            builder.Add("TestWarning", "TestWarning", "(int unprinted) #MessageId=251, Severity=Warn");
            builder.Add("TestException(string s, int i)", "TestException-{0}-{1}", ":System.Runtime.InteropServices.COMException");

            string code, resx;
            TestResourceResult result;
            StringWriter captureErr = new StringWriter();
            TextWriter stderr = Console.Error;
            try
            {
                Console.SetError(captureErr);
                result = builder.Compile("-reference:" + GetType().Assembly.Location, out code, out resx);
            }
            finally { Console.SetError(stderr); }

            _lastEvent = null;
            Assert.AreEqual("TestFormatting-000e1234", result.GetValue("TestFormatting", 0x0e1234));
            Assert.IsNotNull(_lastEvent);
            Assert.AreEqual("Application", _lastLog.Log);
            Assert.AreEqual("YourAppName", _lastLog.Source);
            Assert.AreEqual(0x0F, _lastEvent.CategoryId);
            Assert.AreEqual(EventLogEntryType.Information, _lastEvent.EntryType);
            Assert.AreEqual(0x41020003L, _lastEvent.InstanceId);
            Assert.AreEqual(1, _lastArgs.Length);
            Assert.AreEqual("000e1234", _lastArgs[0]);

            _lastEvent = null;
            Assert.AreEqual("TestWarning", result.GetValue("TestWarning", 3579));
            Assert.IsNotNull(_lastEvent);
            Assert.AreEqual("Application", _lastLog.Log);
            Assert.AreEqual("YourAppName", _lastLog.Source);
            Assert.AreEqual(0x0F, _lastEvent.CategoryId);
            Assert.AreEqual(EventLogEntryType.Warning, _lastEvent.EntryType);
            Assert.AreEqual(0x810200fbL, _lastEvent.InstanceId);
            Assert.AreEqual(1, _lastArgs.Length);
            Assert.AreEqual("3579", _lastArgs[0]);

            _lastEvent = null;
            COMException error = (COMException)result.CreateException("TestException", "error", 1234);
            Assert.AreEqual("TestException-error-1234", error.Message);
            Assert.AreEqual(unchecked((int)0xe1020005), error.ErrorCode);//5 was auto-assigned by NextMessageId
            Assert.AreEqual("http://mydomain/errorcodes.aspx?id=e1020005", error.HelpLink);

            Assert.IsNotNull(_lastEvent);
            Assert.AreEqual("Application", _lastLog.Log);
            Assert.AreEqual("YourAppName", _lastLog.Source);
            Assert.AreEqual(0x0F, _lastEvent.CategoryId);
            Assert.AreEqual(EventLogEntryType.Error, _lastEvent.EntryType);
            Assert.AreEqual(0xC1020005L, _lastEvent.InstanceId);
            Assert.AreEqual(2, _lastArgs.Length);
            Assert.AreEqual("error", _lastArgs[0]);
            Assert.AreEqual("1234", _lastArgs[1]);
        }