Exemplo n.º 1
0
        public void LineEndingModeInequalityTest()
        {
            LineEndingMode modeDefault = LineEndingMode.Default;
            LineEndingMode modeNone    = LineEndingMode.None;
            LineEndingMode modeLF      = LineEndingMode.LF;
            LineEndingMode modeCRLF    = LineEndingMode.CRLF;

            Assert.True(LineEndingMode.Default != modeNone);
            Assert.True(LineEndingMode.None != modeLF);
            Assert.True(LineEndingMode.None != modeCRLF);
            Assert.False(LineEndingMode.Default != modeDefault);
            Assert.False(LineEndingMode.None != modeNone);
            Assert.False(LineEndingMode.LF != modeLF);
            Assert.False(LineEndingMode.CRLF != modeCRLF);

            Assert.True(null != LineEndingMode.LF);
            Assert.True(null != modeLF);
            Assert.True(LineEndingMode.LF != null);
            Assert.True(modeLF != null);
            Assert.True(null != LineEndingMode.CRLF);
            Assert.True(null != modeCRLF);
            Assert.True(LineEndingMode.CRLF != null);
            Assert.True(modeCRLF != null);

            // Handle running tests on different operating systems
            if (modeCRLF.NewLineCharacters == Environment.NewLine)
            {
                Assert.True(LineEndingMode.LF != modeDefault);
            }
            else
            {
                Assert.True(LineEndingMode.CRLF != modeDefault);
            }
        }
Exemplo n.º 2
0
        private static bool TryTypeConverterConversion(Type type, string value, out object newValue)
        {
#if !SILVERLIGHT
            var converter = TypeDescriptor.GetConverter(type);
            if (converter.CanConvertFrom(typeof(string)))
            {
                newValue = converter.ConvertFromInvariantString(value);
                return(true);
            }
#else
            if (type == typeof(LineEndingMode))
            {
                newValue = LineEndingMode.FromString(value);
                return(true);
            }
            else if (type == typeof(Uri))
            {
                newValue = new Uri(value);
                return(true);
            }
#endif

            newValue = null;
            return(false);
        }
Exemplo n.º 3
0
        private void SetScriptLineEndings(string scriptPath,
                                          LineEndingMode mode)
        {
            try
            {
                var scriptContents  = File.ReadAllText(scriptPath);
                var unifiedContents = Regex.Replace(scriptContents,
                                                    @"\r\n|\n\r|\n|\r", lineEndingsMap[mode]);

                if (scriptContents.Equals(unifiedContents,
                                          StringComparison.CurrentCultureIgnoreCase))
                {
                    Info.HasIssues = false;
                    Info.Success   = true;
                }
                else
                {
                    Info.HasIssues = true;
                    File.WriteAllText(scriptPath, unifiedContents);
                    Info.Success = true;
                }
            }
            catch (Exception ex)
            {
                Info.Success   = false;
                Info.Exception = ex;
            }
        }
Exemplo n.º 4
0
        public LineEndingsFixer(LineEndingMode target)
        {
            lineEndingsMap[LineEndingMode.Windows] = "\r\n";
            lineEndingsMap[LineEndingMode.Mac]     = "\r";
            lineEndingsMap[LineEndingMode.Unix]    = "\n"; lineEndingMode = target;

            Info      = new FixerInfo();
            Info.Name = "Line Endings Fixer";
        }
Exemplo n.º 5
0
        public void FiFiRunner_FixLineEndings_Returns_Config_Correctly
            (LineEndingMode expected)
        {
            var runner = FiFiRunner.New();
            var config = runner
                         .FixLineEndings(expected)
                         .GetConfig();

            Assert.Equal(expected, config.LineEnding);
        }
Exemplo n.º 6
0
        public void LineEndingModeEqualityTest()
        {
            LineEndingMode modeDefault = LineEndingMode.Default;
            LineEndingMode modeNone    = LineEndingMode.None;
            LineEndingMode modeLF      = LineEndingMode.LF;

            Assert.True(LineEndingMode.Default == modeDefault);
            Assert.True(LineEndingMode.None == modeNone);
            Assert.True(LineEndingMode.LF == modeLF);
            Assert.False(LineEndingMode.Default == modeNone);
            Assert.False(LineEndingMode.None == modeLF);
            Assert.False(LineEndingMode.LF == modeDefault);
        }
Exemplo n.º 7
0
    /// <summary>
    /// Loads the script at the specified path, replaces all the line endings
    /// then if the contents of the file have changed, write the file back out.
    /// </summary>
    /// <param name="scriptPath">The path to the script to unify.</param>
    /// <param name="mode">The line ending mode to use.</param>
    private static void SetScriptLineEndings(string scriptPath, LineEndingMode mode)
    {
        var scriptContents  = File.ReadAllText(scriptPath);
        var unifiedContents = Regex.Replace(scriptContents, @"\r\n|\n\r|\n|\r", s_lineEndingsMap[mode]);

        if (scriptContents.Equals(unifiedContents, StringComparison.CurrentCultureIgnoreCase))
        {
            return;
        }

        Debug.Log("Unifyed script line endings: " + scriptPath);
        File.WriteAllText(scriptPath, unifiedContents);
    }
Exemplo n.º 8
0
        public void LineEndingModeNullComparison()
        {
            LineEndingMode mode1 = LineEndingMode.LF;

            Assert.False(mode1 == null);
            Assert.True(mode1 != null);
            Assert.False(null == mode1);
            Assert.True(null != mode1);

            LineEndingMode mode2 = null;

            Assert.True(mode2 == null);
            Assert.False(mode2 != null);
            Assert.True(null == mode2);
            Assert.False(null != mode2);
        }
#pragma warning restore S1144 // Unused private types or members should be removed

        private static Dictionary <Type, Func <string, ConfigurationItemFactory, object> > BuildPropertyConversionMapper()
        {
            return(new Dictionary <Type, Func <string, ConfigurationItemFactory, object> >()
            {
                { typeof(Layout), TryParseLayoutValue },
                { typeof(SimpleLayout), TryParseLayoutValue },
                { typeof(ConditionExpression), TryParseConditionValue },
                { typeof(Encoding), TryParseEncodingValue },
                { typeof(string), (stringvalue, factory) => stringvalue },
                { typeof(int), (stringvalue, factory) => Convert.ChangeType(stringvalue.Trim(), TypeCode.Int32, CultureInfo.InvariantCulture) },
                { typeof(bool), (stringvalue, factory) => Convert.ChangeType(stringvalue.Trim(), TypeCode.Boolean, CultureInfo.InvariantCulture) },
                { typeof(CultureInfo), (stringvalue, factory) => new CultureInfo(stringvalue.Trim()) },
                { typeof(Type), (stringvalue, factory) => Type.GetType(stringvalue.Trim(), true) },
                { typeof(LineEndingMode), (stringvalue, factory) => LineEndingMode.FromString(stringvalue.Trim()) },
                { typeof(Uri), (stringvalue, factory) => new Uri(stringvalue.Trim()) }
            });
        }
Exemplo n.º 10
0
        public void LineEndingModeInequalityTest()
        {
            LineEndingMode modeDefault = LineEndingMode.Default;
            LineEndingMode modeNone    = LineEndingMode.None;
            LineEndingMode modeLF      = LineEndingMode.LF;

            Assert.True(LineEndingMode.Default != modeNone);
            Assert.True(LineEndingMode.None != modeLF);
            Assert.True(LineEndingMode.LF != modeDefault);
            Assert.False(LineEndingMode.Default != modeDefault);
            Assert.False(LineEndingMode.None != modeNone);
            Assert.False(LineEndingMode.LF != modeLF);

            Assert.True(null != LineEndingMode.LF);
            Assert.True(null != modeLF);
            Assert.True(LineEndingMode.LF != null);
            Assert.True(modeLF != null);
        }
Exemplo n.º 11
0
        public void LineEndingModeEqualityTest()
        {
            LineEndingMode modeDefault = LineEndingMode.Default;
            LineEndingMode modeNone    = LineEndingMode.None;
            LineEndingMode modeLF      = LineEndingMode.LF;
            LineEndingMode modeCRLF    = LineEndingMode.CRLF;
            LineEndingMode modeNull    = LineEndingMode.Null;

            Assert.True(LineEndingMode.Default == modeDefault);
            Assert.True(LineEndingMode.None == modeNone);
            Assert.True(LineEndingMode.LF == modeLF);
            Assert.True(LineEndingMode.Null == modeNull);
            Assert.False(LineEndingMode.Default == modeNone);
            Assert.False(LineEndingMode.None == modeLF);
            Assert.False(LineEndingMode.None == modeCRLF);
            Assert.False(LineEndingMode.None == modeNull);
            Assert.False(LineEndingMode.None == (object)new { });
            Assert.False(LineEndingMode.None == null);

            Assert.True(LineEndingMode.Default.Equals(modeDefault));
            Assert.True(LineEndingMode.None.Equals(modeNone));
            Assert.True(LineEndingMode.LF.Equals(modeLF));
            Assert.True(LineEndingMode.Null.Equals(modeNull));
            Assert.False(LineEndingMode.Default.Equals(modeNone));
            Assert.False(LineEndingMode.None.Equals(modeLF));
            Assert.False(LineEndingMode.None.Equals(modeCRLF));
            Assert.False(LineEndingMode.None.Equals(modeNull));
            Assert.False(LineEndingMode.None.Equals(new { }));
            Assert.False(LineEndingMode.None.Equals(null));

            // Handle running tests on different operating systems
            if (modeCRLF.NewLineCharacters == Environment.NewLine)
            {
                Assert.False(LineEndingMode.LF == modeDefault);
                Assert.True(LineEndingMode.CRLF == modeDefault);
            }
            else
            {
                Assert.True(LineEndingMode.LF == modeDefault);
                Assert.False(LineEndingMode.CRLF == modeDefault);
            }
        }
Exemplo n.º 12
0
        private static bool TrySpecialConversion(Type type, string value, out object newValue)
        {
            if (type == typeof(Encoding))
            {
                value = value.Trim();
                if (string.Equals(value, nameof(Encoding.UTF8), StringComparison.OrdinalIgnoreCase))
                {
                    value = Encoding.UTF8.WebName;  // Support utf8 without hyphen (And not just Utf-8)
                }
                newValue = Encoding.GetEncoding(value);
                return(true);
            }

            if (type == typeof(CultureInfo))
            {
                value    = value.Trim();
                newValue = new CultureInfo(value);
                return(true);
            }

            if (type == typeof(Type))
            {
                value    = value.Trim();
                newValue = Type.GetType(value, true);
                return(true);
            }

            if (type == typeof(LineEndingMode))
            {
                newValue = LineEndingMode.FromString(value);
                return(true);
            }

            if (type == typeof(Uri))
            {
                newValue = new Uri(value);
                return(true);
            }

            newValue = null;
            return(false);
        }
Exemplo n.º 13
0
 public FiFiRunner FixLineEndings(LineEndingMode lineEnding)
 {
     config.LineEnding = lineEnding;
     return(this);
 }
Exemplo n.º 14
0
        private void HappyPathTest(bool newLine, LineEndingMode lineEnding, params string[] messages)
        {
            var senderFactory = new MySenderFactory();
            var target        = new NetworkTarget();

            target.Address        = "tcp://someaddress/";
            target.SenderFactory  = senderFactory;
            target.Layout         = "${message}";
            target.NewLine        = newLine;
            target.LineEnding     = lineEnding;
            target.KeepConnection = true;
            target.Initialize(null);

            var exceptions = new List <Exception>();
            var mre        = new ManualResetEvent(false);
            int remaining  = 3;
            AsyncContinuation asyncContinuation = ex =>
            {
                lock (exceptions)
                {
                    exceptions.Add(ex);
                    if (--remaining == 0)
                    {
                        mre.Set();
                    }
                }
            };

            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg1").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg2").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg3").WithContinuation(asyncContinuation));

            mre.WaitOne();
            foreach (var ex in exceptions)
            {
                if (ex != null)
                {
                    Assert.True(false, ex.ToString());
                }
            }

            Assert.Single(senderFactory.Senders);

            var sender = senderFactory.Senders[0];

            target.Close();

            // Get the length of all the messages and their line endings
            var eol       = newLine ? lineEnding.NewLineCharacters : string.Empty;
            var eolLength = eol.Length;
            var length    = messages.Sum(m => m.Length) + (eolLength * messages.Length);

            Assert.Equal(length, sender.MemoryStream.Length);
            Assert.Equal(string.Join(eol, messages) + eol, target.Encoding.GetString(sender.MemoryStream.GetBuffer(), 0, (int)sender.MemoryStream.Length));

            // we invoke the sender for each message, each time sending 4 bytes
            var actual = senderFactory.Log.ToString();

            Assert.True(actual.IndexOf("1: connect tcp://someaddress/") != -1);
            foreach (var message in messages)
            {
                Assert.True(actual.IndexOf($"1: send 0 {message.Length + eolLength}") != -1);
            }
            Assert.True(actual.IndexOf("1: close") != -1);
        }