コード例 #1
0
		protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
		{
			expectedOutput = NormalizeNewlines(expectedOutput);

			IDocument doc = GetResult(policy, input, mode, options);
			if (expectedOutput != doc.Text) {
				Console.WriteLine ("expected:");
				Console.WriteLine (expectedOutput);
				Console.WriteLine ("got:");
				Console.WriteLine (doc.Text);
				for (int i = 0; i < expectedOutput.Length && i < doc.TextLength; i++) {
					if (expectedOutput [i] != doc.GetCharAt(i)) {
						Console.WriteLine (
							"i:"+i+" differ:"+ 
							expectedOutput[i].ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t") +
							" !=" + 
							doc.GetCharAt(i).ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t")
						);
						Console.WriteLine(">"+expectedOutput.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t"));
						Console.WriteLine(">"+doc.Text.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t"));
						break;
					}
				}
			}
			Assert.AreEqual (expectedOutput, doc.Text);
			return doc;
		}
コード例 #2
0
        private void Serialize(StringBuilder sb, object value, FormattingMode mode)
        {
            if (value == null)
            {
                sb.Append("null");
                return;
            }

            switch (mode)
            {
            case FormattingMode.Default:
                this.SerializeDefault(sb, value);
                break;

            case FormattingMode.Members:
                this.SerializeMembers(sb, value);
                break;

            case FormattingMode.Items:
                this.SerializeItems(sb, value, false);
                break;

            case FormattingMode.ItemsMembers:
                this.SerializeItems(sb, value, true);
                break;

            case FormattingMode.DoNotLog:
                sb.Append("-");
                break;

            default:
                throw new ArgumentOutOfRangeException("mode", string.Format(CultureInfo.InvariantCulture, "Unexpected formatting mode '{0}'.", mode));
            }
        }
コード例 #3
0
		protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
		{
			StringBuilderDocument document;
			var changes = GetChanges(policy, input, out document, mode, options);
			
			changes.ApplyChanges();
			return document;
		}
コード例 #4
0
		protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive)
		{
			expectedOutput = NormalizeNewlines(expectedOutput);
			IDocument doc = GetResult(policy, input, mode);
			if (expectedOutput != doc.Text) {
				Console.WriteLine (doc.Text);
			}
			Assert.AreEqual (expectedOutput, doc.Text);
			return doc;
		}
コード例 #5
0
		protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, out StringBuilderDocument document, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
		{
			options = GetActualOptions(options);
			input = NormalizeNewlines(input);

			document = new StringBuilderDocument(input);
			var visitor = new CSharpFormatter(policy, options);
			visitor.FormattingMode = mode;
			var syntaxTree = new CSharpParser().Parse(document, "test.cs");

			return visitor.AnalyzeFormatting(document, syntaxTree);
		}
コード例 #6
0
		/*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
		{
			changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset));
			StringBuilder b = new StringBuilder(text);
			foreach (var change in changes) {
				//Console.WriteLine ("---- apply:" + change);
//				Console.WriteLine (adapter.Text);
				if (change.Offset > b.Length)
					continue;
				b.Remove(change.Offset, change.RemovedChars);
				b.Insert(change.Offset, change.InsertedText);
			}
//			Console.WriteLine ("---result:");
//			Console.WriteLine (adapter.Text);
			return b.ToString();
		}*/
		
		protected static IDocument GetResult (CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly)
		{
			input = NormalizeNewlines (input);
			var document = new StringBuilderDocument (input);
			var options = new TextEditorOptions ();
			options.EolMarker = "\n";
			var visitor = new AstFormattingVisitor (policy, document, options);
			visitor.FormattingMode = mode;
			var compilationUnit = new CSharpParser ().Parse (document, "test.cs");
			compilationUnit.AcceptVisitor (visitor);
			visitor.ApplyChanges();
			return document;
		}
コード例 #7
0
 protected static void Continue(CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly)
 {
     expectedOutput = NormalizeNewlines (expectedOutput);
     var options = new TextEditorOptions ();
     options.EolMarker = "\n";
     var formatter = new CSharpFormatter (policy, options);
     formatter.FormattingMode = formattingMode;
     string newText = formatter.Format (document);
     if (expectedOutput != newText) {
         Console.WriteLine (newText);
     }
     Assert.AreEqual (expectedOutput, newText);
 }
コード例 #8
0
 /*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
 {
     changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset));
     StringBuilder b = new StringBuilder(text);
     foreach (var change in changes) {
         //Console.WriteLine ("---- apply:" + change);
 //				Console.WriteLine (adapter.Text);
         if (change.Offset > b.Length)
             continue;
         b.Remove(change.Offset, change.RemovedChars);
         b.Insert(change.Offset, change.InsertedText);
     }
 //			Console.WriteLine ("---result:");
 //			Console.WriteLine (adapter.Text);
     return b.ToString();
 }*/
 protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive)
 {
     input = NormalizeNewlines(input);
     var document = new StringBuilderDocument(input);
     var options = new TextEditorOptions();
     options.EolMarker = "\n";
     options.WrapLineLength = 80;
     var visitor = new CSharpFormatter (policy, options);
     visitor.FormattingMode = mode;
     var syntaxTree = new CSharpParser ().Parse (document, "test.cs");
     var changes = visitor.AnalyzeFormatting(document, syntaxTree);
     changes.ApplyChanges();
     return document;
 }
コード例 #9
0
        public void ReadModePropertyInfo_ReturnsTheResultOfTheBaseReader()
        {
            // Arrange
            IFormattingModeReader baseReader = Substitute.For <IFormattingModeReader>();

            baseReader.ReadMode(TestProperty).Returns(FormattingMode.ItemsMembers);

            IFormattingModeReader reader = new CachedFormattingModeReader(baseReader);

            // Act
            FormattingMode mode = reader.ReadMode(TestProperty);

            // Assert
            Assert.AreEqual(FormattingMode.ItemsMembers, mode);
        }
コード例 #10
0
        public void Serialize(StringBuilder sb, object value, ParameterInfo parameter)
        {
            if (sb == null)
            {
                throw new ArgumentNullException("sb");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            FormattingMode mode = this.reader.ReadMode(parameter);

            this.Serialize(sb, value, mode);
        }
コード例 #11
0
		protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly)
		{
			expectedOutput = NormalizeNewlines (expectedOutput);
			var options = new TextEditorOptions ();
			options.EolMarker = "\n";
			var visitior = new AstFormattingVisitor (policy, document, options);
			visitior.FormattingMode = formattingMode;
			var syntaxTree = new CSharpParser ().Parse (document, "test.cs");
			syntaxTree.AcceptVisitor (visitior);
			visitior.ApplyChanges();
			string newText = document.Text;
			if (expectedOutput != newText) {
				Console.WriteLine (newText);
			}
			Assert.AreEqual (expectedOutput, newText);
		}
コード例 #12
0
        private static void TestFormatter(object value, FormattingMode mode, string expectedOutput, IFormatProvider formatProvider = null, ICustomValueFormatter customValueFormatter = null)
        {
            // Arrange
            IFormattingModeReader reader = Substitute.For <IFormattingModeReader>();

            reader.ReadMode(TestParameter).Returns(mode);

            IParameterFormatter formatter = new ParameterFormatter(reader, formatProvider ?? CultureInfo.InvariantCulture, customValueFormatter);

            StringBuilder sb = new StringBuilder();

            // Act
            formatter.Serialize(sb, value, TestParameter);

            // Assert
            Assert.AreEqual(expectedOutput, sb.ToString());
        }
コード例 #13
0
        private void SerializeMembers(StringBuilder sb, object value)
        {
            List <PropertyInfo> properties = value.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.GetIndexParameters().Length == 0).ToList();

            sb.Append("{ ");
            for (int i = 0; i < properties.Count; i++)
            {
                if (i != 0)
                {
                    sb.Append(", ");
                }

                sb.Append(properties[i].Name);
                sb.Append(": ");

                object         memberValue = properties[i].GetValue(value, null);
                FormattingMode mode        = this.reader.ReadMode(properties[i]);
                this.Serialize(sb, memberValue, mode);
            }

            sb.Append(" }");
        }
コード例 #14
0
        protected static IDocument Test(CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
        {
            expectedOutput = NormalizeNewlines(expectedOutput);

            IDocument doc = GetResult(policy, input, mode, options);

            if (expectedOutput != doc.Text)
            {
                Console.WriteLine("expected:");
                Console.WriteLine(expectedOutput);
                Console.WriteLine("got:");
                Console.WriteLine(doc.Text);
                for (int i = 0; i < expectedOutput.Length && i < doc.TextLength; i++)
                {
                    if (expectedOutput [i] != doc.GetCharAt(i))
                    {
                        Console.WriteLine(
                            "i:" + i + " differ:" +
                            expectedOutput[i].ToString().Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t") +
                            " !=" +
                            doc.GetCharAt(i).ToString().Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t")
                            );
                        Console.WriteLine(">" + expectedOutput.Substring(i).Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t"));
                        Console.WriteLine(">" + doc.Text.Substring(i).Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t"));
                        break;
                    }
                }
            }
            Assert.AreEqual(expectedOutput, doc.Text);
            return(doc);
        }
コード例 #15
0
		protected static void TestNoUnnecessaryChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
		{
			var formatted = GetResult(policy, input, mode, options).Text;
			var changes = GetChanges(policy, formatted, mode, options);
			Assert.AreEqual(0, changes.Count, "Wrong amount of changes");
		}
コード例 #16
0
		protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
		{
			StringBuilderDocument document;
			return GetChanges(policy, input, out document, mode, options);
		}
コード例 #17
0
        /*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
         * {
         *      changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset));
         *      StringBuilder b = new StringBuilder(text);
         *      foreach (var change in changes) {
         *              //Console.WriteLine ("---- apply:" + change);
         * //				Console.WriteLine (adapter.Text);
         *              if (change.Offset > b.Length)
         *                      continue;
         *              b.Remove(change.Offset, change.RemovedChars);
         *              b.Insert(change.Offset, change.InsertedText);
         *      }
         * //			Console.WriteLine ("---result:");
         * //			Console.WriteLine (adapter.Text);
         *      return b.ToString();
         * }*/

        protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly)
        {
            input = NormalizeNewlines(input);
            var document = new StringBuilderDocument(input);
            var options  = new TextEditorOptions();

            options.EolMarker = "\n";
            var visitor = new AstFormattingVisitor(policy, document, options);

            visitor.FormattingMode = mode;
            var compilationUnit = new CSharpParser().Parse(document, "test.cs");

            compilationUnit.AcceptVisitor(visitor);
            visitor.ApplyChanges();
            return(document);
        }
コード例 #18
0
        /*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
         * {
         *      changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset));
         *      StringBuilder b = new StringBuilder(text);
         *      foreach (var change in changes) {
         *              //Console.WriteLine ("---- apply:" + change);
         * //				Console.WriteLine (adapter.Text);
         *              if (change.Offset > b.Length)
         *                      continue;
         *              b.Remove(change.Offset, change.RemovedChars);
         *              b.Insert(change.Offset, change.InsertedText);
         *      }
         * //			Console.WriteLine ("---result:");
         * //			Console.WriteLine (adapter.Text);
         *      return b.ToString();
         * }*/

        protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
        {
            if (options == null)
            {
                options                = new TextEditorOptions();
                options.EolMarker      = "\n";
                options.WrapLineLength = 80;
            }


            input = NormalizeNewlines(input);
            var document = new StringBuilderDocument(input);
            var visitor  = new CSharpFormatter(policy, options);

            visitor.FormattingMode = mode;
            var syntaxTree = new CSharpParser().Parse(document, "test.cs");
            var changes    = visitor.AnalyzeFormatting(document, syntaxTree);

            changes.ApplyChanges();
            return(document);
        }
コード例 #19
0
        protected static void Continue(CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly)
        {
            expectedOutput = NormalizeNewlines(expectedOutput);
            var options = new TextEditorOptions();

            options.EolMarker = "\n";
            var formatter = new CSharpFormatter(policy, options);

            formatter.FormattingMode = formattingMode;
            string newText = formatter.Format(document);

            if (expectedOutput != newText)
            {
                Console.WriteLine("expected:");
                Console.WriteLine(expectedOutput);
                Console.WriteLine("got:");
                Console.WriteLine(newText);
            }
            Assert.AreEqual(expectedOutput, newText);
        }
コード例 #20
0
        protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, out StringBuilderDocument document, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
        {
            options = GetActualOptions(options);
            input   = NormalizeNewlines(input);

            document = new StringBuilderDocument(input);
            var visitor = new CSharpFormatter(policy, options);

            visitor.FormattingMode = mode;
            var syntaxTree = new CSharpParser().Parse(document, "test.cs");

            return(visitor.AnalyzeFormatting(document, syntaxTree));
        }
コード例 #21
0
        protected static void Continue(CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly)
        {
            expectedOutput = NormalizeNewlines(expectedOutput);
            var options = new TextEditorOptions();

            options.EolMarker = "\n";
            var visitior = new AstFormattingVisitor(policy, document, options);

            visitior.FormattingMode = formattingMode;
            var syntaxTree = new CSharpParser().Parse(document, "test.cs");

            syntaxTree.AcceptVisitor(visitior);
            visitior.ApplyChanges();
            string newText = document.Text;

            if (expectedOutput != newText)
            {
                Console.WriteLine(newText);
            }
            Assert.AreEqual(expectedOutput, newText);
        }
コード例 #22
0
        protected static IDocument Test(CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive)
        {
            expectedOutput = NormalizeNewlines(expectedOutput);
            IDocument doc = GetResult(policy, input, mode);

            if (expectedOutput != doc.Text)
            {
                Console.WriteLine("expected:");
                Console.WriteLine(expectedOutput);
                Console.WriteLine("got:");
                Console.WriteLine(doc.Text);
            }
            Assert.AreEqual(expectedOutput, doc.Text);
            return(doc);
        }
コード例 #23
0
        /*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
         * {
         *      changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset));
         *      StringBuilder b = new StringBuilder(text);
         *      foreach (var change in changes) {
         *              //Console.WriteLine ("---- apply:" + change);
         * //				Console.WriteLine (adapter.Text);
         *              if (change.Offset > b.Length)
         *                      continue;
         *              b.Remove(change.Offset, change.RemovedChars);
         *              b.Insert(change.Offset, change.InsertedText);
         *      }
         * //			Console.WriteLine ("---result:");
         * //			Console.WriteLine (adapter.Text);
         *      return b.ToString();
         * }*/

        protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive)
        {
            input = NormalizeNewlines(input);
            var document = new StringBuilderDocument(input);
            var options  = new TextEditorOptions();

            options.EolMarker      = "\n";
            options.WrapLineLength = 80;
            var visitor = new AstFormattingVisitor(policy, document, options);

            visitor.FormattingMode = mode;
            var syntaxTree = new CSharpParser().Parse(document, "test.cs");

            syntaxTree.AcceptVisitor(visitor);
            visitor.ApplyChanges();
            return(document);
        }
コード例 #24
0
        protected static void TestNoUnnecessaryChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
        {
            var formatted = GetResult(policy, input, mode, options).Text;
            var changes   = GetChanges(policy, formatted, mode, options);

            Assert.AreEqual(0, changes.Count, "Wrong amount of changes");
        }
コード例 #25
0
        protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
        {
            StringBuilderDocument document;

            return(GetChanges(policy, input, out document, mode, options));
        }
コード例 #26
0
        protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
        {
            StringBuilderDocument document;
            var changes = GetChanges(policy, input, out document, mode, options);

            changes.ApplyChanges();
            return(document);
        }