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 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 ShouldRejectExpressionSelectionsIncludingAssignment()
        {
            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("x = ((yacht * ant) - (bucket + car))"));

            manipulator.ExtractVariable();
        }
        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 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 ShouldExtractVariableAndInsertVariableOnLineBeforeThatContainingTheExpressionEvenWhenMultipleExpressionsAreOnTheSameLine()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("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();

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


            Assert.AreEqual("            var expression = a - b + c;", testBuffer.CurrentSnapshot.Lines.ElementAt(8).GetText());
            Assert.AreEqual("            int w = 1; int x = expression * d / e % 3444;", testBuffer.CurrentSnapshot.Lines.ElementAt(9).GetText());
        }
        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 ShouldExtractConstantAndInsertConstantOnLastLineOfClassDeclarationsCopingWithLineComments()
        {
            StringTextBuffer testBuffer = new StringTextBuffer(ExpressionInContext("int x = 500;", "// comment with bracket )"));

            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(9).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 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 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 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 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 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 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 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 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 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();
        }
        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();
        }