public void ShouldConvertGenericVariableToFieldAndInsertFieldDeclarationOnLastLineOfClassDeclarations()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("List<int> fred = new List<int();"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Caret.Position).Returns(testBuffer.GetPositionAtStartOf("fred"));

            manipulator.ConvertVariableToField();
            Console.Out.Write(testBuffer.CurrentSnapshot.GetText());

            Assert.AreEqual("        private List<int> fred;", testBuffer.CurrentSnapshot.Lines.ElementAt(5).GetText());
            Assert.AreEqual("            fred = new List<int();", testBuffer.CurrentSnapshot.Lines.ElementAt(9).GetText());
        }
        public void ShouldAssignParameterToFieldAndInsertFieldDeclarationOnLastLineOfClassDeclarationsPlusAssignmentInMethod()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("int x = a - b + c * d / e % 3444;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Caret.Position).Returns(testBuffer.GetPositionAtStartOf("arg1,"));

            manipulator.AssignParameterToField();

            Console.Out.Write(testBuffer.CurrentSnapshot.GetText());

            Assert.AreEqual("        private string[] arg1;", testBuffer.CurrentSnapshot.Lines.ElementAt(5).GetText());
            Assert.AreEqual("            this.arg1 = arg1;", testBuffer.CurrentSnapshot.Lines.ElementAt(10).GetText());
        }
 internal TestSnapshotLine(StringTextBuffer testTextBuffer, TestSnapshot parent, int linenumber, int start, string linetext)
 {
     this.start = start;
     this.line = linetext;
     this.end = this.start + linetext.Length;
     this.testTextBuffer = testTextBuffer;
     this.parent = parent;
     this.lineNumber = linenumber;
 }
 public TestSnapshot(StringTextBuffer testTextBuffer, string text)
 {
     this.testTextBuffer = testTextBuffer;
     this.text = text;
 }
        public void ShouldThrowExceptionExtractingVariableAndWhenNoWhereToPutTheDeclaration()
        {
            StringTextBuffer testBuffer = new StringTextBuffer("int w = 1; int x = a - b + c * d / e % 3444;");

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("a - b + c"));
            mockNameProvider.Setup(m => m.GetName()).Returns("expression");

            manipulator.ExtractVariable();
        }
        public void ShouldConvertVariableToFieldAndInsertFieldDeclarationOnLastLineOfClassDeclarationsRemovingSimpleOriginalWhenItDoesntHaveAnAssignmentAndOtherAssignmentsOnTheSameLine()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("string fred=\"fred\"; int x; double jim;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Caret.Position).Returns(testBuffer.GetPositionAtStartOf("x;"));

            manipulator.ConvertVariableToField();
            Console.Out.Write(testBuffer.CurrentSnapshot.GetText());

            Assert.AreEqual("        private int x;", testBuffer.CurrentSnapshot.Lines.ElementAt(5).GetText());
            Assert.AreEqual("            string fred=\"fred\";  double jim;", testBuffer.CurrentSnapshot.Lines.ElementAt(9).GetText());
        }
        public void ShouldThrowExceptionExtractingConstantWhenNoWhereToPutTheDeclaration()
        {
            StringTextBuffer testBuffer = new StringTextBuffer("int x = a - b + c * d / e % 3444;");

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("3444"));
            mockNameProvider.Setup(m => m.GetConstantName()).Returns("constant");

            manipulator.ExtractConstant();
        }
        public void ShouldRejectExpressionSelectionsContainingMismatchedBrackets()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("int x = ((y * a) - (b + c)) * d / e % 3444;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("(y * a) - (b + c))"));

            manipulator.ExtractVariable();
        }
        public void ShouldRejectExpressionSelectionsContainingMismatchedQuotes()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("int x = \" george\".Length * a - b + c * d / e % 3444;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("george\".Length * a"));

            manipulator.ExtractVariable();
        }
        public void ShouldExtractConstantIncludingUnaryOperatorAndInsertConstantOnLastLineOfClassDeclarations()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("int x = -1;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("-1"));
            mockNameProvider.Setup(m => m.GetConstantName()).Returns("constant");

            manipulator.ExtractConstant();

            Console.Out.Write(testBuffer.CurrentSnapshot.GetText());

            Assert.AreEqual("        private const int constant = -1;", testBuffer.CurrentSnapshot.Lines.ElementAt(5).GetText());
            Assert.AreEqual("            int x = constant;", testBuffer.CurrentSnapshot.Lines.ElementAt(9).GetText());
        }
        public void ShouldExtractVariableAndInsertVariableOnLineBeforeThatContainingTheExpressionEvenWhenThatLineContainsABrace()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("int x = new string[] {}.Length + a - b + c * d / e % 3444;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("a - b + c"));
            mockNameProvider.Setup(m => m.GetName()).Returns("expression");

            manipulator.ExtractVariable();

            Console.Out.Write(testBuffer.CurrentSnapshot.GetText());

            Assert.AreEqual("            var expression = a - b + c;", testBuffer.CurrentSnapshot.Lines.ElementAt(8).GetText());
            Assert.AreEqual("            int x = new string[] {}.Length + expression * d / e % 3444;", testBuffer.CurrentSnapshot.Lines.ElementAt(9).GetText());
        }
        public void ShouldExtractConstantFromInsideIfStatementAndInsertConstantOnLastLineOfClassDeclarations()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("if (y < 219) \n{\n                int x = a - b + c * d / e % 3444;\n            }"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("3444"));
            mockNameProvider.Setup(m => m.GetConstantName()).Returns("constant");

            manipulator.ExtractConstant();

            Console.Out.Write(testBuffer.CurrentSnapshot.GetText());

            Assert.AreEqual("        private const int constant = 3444;", testBuffer.CurrentSnapshot.Lines.ElementAt(5).GetText());
            Assert.AreEqual("                int x = a - b + c * d / e % constant;", testBuffer.CurrentSnapshot.Lines.ElementAt(11).GetText());
        }
        public void ShouldExtractConstantAndInsertConstantOnLastLineOfClassDeclarationsCopingWithQuotedExpressionsWithEscapedQuotes()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("char quote= '\\'';\n            int x = 500;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("500"));
            mockNameProvider.Setup(m => m.GetConstantName()).Returns("constant");

            manipulator.ExtractConstant();

            Console.Out.Write(testBuffer.CurrentSnapshot.GetText());

            Assert.AreEqual("        private const int constant = 500;", testBuffer.CurrentSnapshot.Lines.ElementAt(5).GetText());
            Assert.AreEqual("            int x = constant;", testBuffer.CurrentSnapshot.Lines.ElementAt(10).GetText());
        }
        public void ShouldExtractBooleanConstantFromIfConditionAndInsertConstantOnLastLineOfClassDeclarations()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("if (y == true) int x = a - b + c * d / e % 3444;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("true"));
            mockNameProvider.Setup(m => m.GetConstantName()).Returns("constant");

            manipulator.ExtractConstant();

            Console.Out.Write(testBuffer.CurrentSnapshot.GetText());

            Assert.AreEqual("        private const bool constant = true;", testBuffer.CurrentSnapshot.Lines.ElementAt(5).GetText());
            Assert.AreEqual("            if (y == constant) int x = a - b + c * d / e % 3444;", testBuffer.CurrentSnapshot.Lines.ElementAt(9).GetText());
        }
 public TestSelection(StringTextBuffer testTextBuffer, string text, int start, int length)
 {
     this.testTextBuffer = testTextBuffer;
     this.start = start;
     this.length = length;
     this.selectionSpan = new SnapshotSpan(testTextBuffer.CurrentSnapshot, new Span(start, length));
 }
        public void ShouldRejectUnfinishedExpressionSelections()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("int x = ((yacht * ant) - (bucket + car)) * d / e % 3444;"));

            mockView.Setup(m => m.TextBuffer).Returns(testBuffer);
            mockView.Setup(m => m.Selection).Returns(testBuffer.Select("bucket + ca"));

            manipulator.ExtractVariable();
        }
 internal TestMappingPoint(StringTextBuffer textBuffer, TestSnapshot snapshot, SnapshotPoint point)
 {
     this.snapshot = snapshot;
     this.textBuffer = textBuffer;
     this.point = point;
 }
 public void SetupViewWithSnapshot()
 {
     buffer = new StringTextBuffer(sampleContext);
     snapshot = buffer.CurrentSnapshot;
 }