public void Construct_Default()
 {
     using (var listener = new PrettyTextWriterTraceListener())
     {
         listener.Writer.Should().BeSameAs(Console.Out);
     }
 }
 public void Construct_InvalidPath()
 {
     using (var listener = new PrettyTextWriterTraceListener(InvalidPath))
     {
         ShouldBeDisabled(listener);
     }
 }
        public void Construct_IOException_Once()
        {
            using (var writer = new StreamWriter(FilePath, true, new UTF8Encoding(false)))
            {
                var shim = new Mock <PrettyTextWriterTraceListener.IShim>();

                shim.Setup(s => s.CreateStreamWriter(FilePath, It.IsNotNull <Encoding>()))
                .Throws <IOException>()
                .Verifiable();

                shim.Setup(s => s.CreateStreamWriter(It.Is <string>(p => p != FilePath), It.IsNotNull <Encoding>()))
                .Returns(writer)
                .Verifiable();

                using (var listener = new PrettyTextWriterTraceListener(FilePath, shim.Object))
                {
                    listener.Writer.Should().BeSameAs(writer);
                    listener.Writer.Write("a");
                }

                FileContent.Should().Be("a");

                shim.Verify();
            }
        }
        private void Test(Action <PrettyTextWriterTraceListener> action, string expected)
        {
            using (var listener = new PrettyTextWriterTraceListener(FilePath))
                action(listener);

            var content = File.ReadAllText(FilePath, Encoding.UTF8);

            content.Should().Be(expected);
        }
        public void Construct_AbsolutePath()
        {
            using (var listener = new PrettyTextWriterTraceListener(FilePath))
            {
                listener.Writer.Should().NotBeNull().And.NotBeSameAs(Console.Out);
                listener.Writer.Write("a");
            }

            FileContent.Should().Be("a");
        }
        public void Construct_InvalidPath_ErrorNotifying()
        {
            var shim = new Mock <PrettyTextWriterTraceListener.IShim>();

            shim.Setup(s => s.NotifyCriticalError(It.IsAny <string>()))
            .Throws <UnauthorizedAccessException>()
            .Verifiable();

            using (var listener = new PrettyTextWriterTraceListener(InvalidPath, shim.Object))
            {
                ShouldBeDisabled(listener);
            }

            shim.Verify();
        }
        public void Construct_RelativePath()
        {
            File.Delete(FilePath); // won't be used

            var name = Path.GetRandomFileName() + ".log";

            FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name);

            using (var listener = new PrettyTextWriterTraceListener(name))
            {
                listener.Writer.Should().NotBeNull().And.NotBeSameAs(Console.Out);
                listener.Writer.Write("a");
            }

            FileContent.Should().Be("a");
        }
        private void ShouldBeDisabled(PrettyTextWriterTraceListener listener)
        {
#if NETFRAMEWORK
            listener.Writer.Should().BeNull();
#else
            listener.Writer.Should().BeSameAs(TextWriter.Null);
#endif
            // These should have no effect
            listener.TraceEvent(Cache, Source, Error, 0);
            listener.TraceEvent(Cache, Source, Error, 0, "a");
            listener.TraceEvent(Cache, Source, Error, 0, "{0}", "a");
            listener.TraceData(Cache, Source, Error, 0, "a");
            listener.TraceData(Cache, Source, Error, 0, "a", "b");
            listener.TraceTransfer(Cache, Source, 0, "a", Guid.NewGuid());
            listener.Write("a");
            listener.WriteLine("a");
        }
        public void Construct_OtherError()
        {
            var shim = new Mock <PrettyTextWriterTraceListener.IShim>();

            shim.Setup(s => s.CreateStreamWriter(It.IsNotNull <string>(), It.IsNotNull <Encoding>()))
            .Throws <UnauthorizedAccessException>()
            .Verifiable();

            shim.Setup(s => s.NotifyCriticalError(It.IsAny <string>()))
            .Verifiable();

            using (var listener = new PrettyTextWriterTraceListener(FilePath, shim.Object))
            {
                ShouldBeDisabled(listener);
            }

            shim.Verify();
        }