⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vbtocsharpconvertertest.cs

📁 根据cs源码解析为codedom
💻 CS
📖 第 1 页 / 共 2 页
字号:
		[Test]
		public void VBConstants()
		{
			TestStatement("a = vbYesNo",
			              "a = Constants.vbYesNo;");
		}
		
		[Test]
		public void ForNextLoop()
		{
			TestStatement("For i = 0 To 10\n" +
			              "Next",
			              "for (i = 0; i <= 10; i++) {\n" +
			              "}");
			TestStatement("For l As Long = 0 To 10 Step 2\n" +
			              "Next",
			              "for (long l = 0; l <= 10; l += 2) {\n" +
			              "}");
			TestStatement("For l As Long = 10 To 0 Step -1\n" +
			              "Next",
			              "for (long l = 10; l >= 0; l += -1) {\n" +
			              "}");
		}
		
		[Test]
		public void DoLoop()
		{
			TestStatement("Do \n Loop",
			              "do {\n" +
			              "}\n" +
			              "while (true);");
			TestStatement("Do \n Loop Until i = 10000",
			              "do {\n" +
			              "}\n" +
			              "while (!(i == 10000));");
		}
		
		[Test]
		public void UsingStatement()
		{
			TestStatement("Using r1 As New StreamReader(file1), r2 As New StreamReader(file2)\n" +
			              "End Using",
			              "using (StreamReader r1 = new StreamReader(file1)) {\n" +
			              "\tusing (StreamReader r2 = new StreamReader(file2)) {\n" +
			              "\t}\n" +
			              "}");
		}
		
		[Test]
		public void SwitchStatement()
		{
			TestStatement(@"Select Case i
                  Case 0 To 5
                        i = 10
                  Case 11
                        i = 0
                  Case Else
                        i = 9
            End Select",
			              "switch (i) {\n" +
			              "\tcase 0:\n" +
			              "\tcase 1:\n" +
			              "\tcase 2:\n" +
			              "\tcase 3:\n" +
			              "\tcase 4:\n" +
			              "\tcase 5:\n" +
			              "\t\ti = 10;\n" +
			              "\t\tbreak;\n" +
			              "\tcase 11:\n" +
			              "\t\ti = 0;\n" +
			              "\t\tbreak;\n" +
			              "\tdefault:\n" +
			              "\t\ti = 9;\n" +
			              "\t\tbreak;\n" +
			              "}");
		}
		
		[Test]
		public void FunctionWithoutImplicitReturn()
		{
			TestMember("Public Function run(i As Integer) As Integer\n" +
			           " Return 0\n" +
			           "End Function",
			           "public int run(int i)\n" +
			           "{\n" +
			           "\treturn 0;\n" +
			           "}");
		}
		
		[Test]
		public void FunctionWithImplicitReturn()
		{
			TestMember("Public Function run(i As Integer) As Integer\n" +
			           " run = 0\n" +
			           "End Function",
			           "public int run(int i)\n" +
			           "{\n" +
			           "\treturn 0;\n" +
			           "}");
		}
		
		[Test]
		public void FunctionWithImplicitReturn2()
		{
			TestMember("Public Function run(i As Integer) As Integer\n" +
			           " While something\n" +
			           "   run += i\n" +
			           " End While\n" +
			           "End Function",
			           "public int run(int i)\n" +
			           "{\n" +
			           "\tint " + VBNetConstructsConvertVisitor.FunctionReturnValueName + " = 0;\n" +
			           "\twhile (something) {\n" +
			           "\t\t" + VBNetConstructsConvertVisitor.FunctionReturnValueName + " += i;\n" +
			           "\t}\n" +
			           "\treturn " + VBNetConstructsConvertVisitor.FunctionReturnValueName + ";\n" +
			           "}");
		}
		
		[Test]
		public void FunctionWithImplicitReturn2b()
		{
			const string ReturnValueName = VBNetConstructsConvertVisitor.FunctionReturnValueName;
			TestMember("Public Function run(i As Integer) As Integer\n" +
			           " While something\n" +
			           "   run = run + run(i - 1)\n" +
			           " End While\n" +
			           "End Function",
			           "public int run(int i)\n" +
			           "{\n" +
			           "\tint " + ReturnValueName + " = 0;\n" +
			           "\twhile (something) {\n" +
			           "\t\t" + ReturnValueName + " = " + ReturnValueName + " + run(i - 1);\n" +
			           "\t}\n" +
			           "\treturn " + ReturnValueName + ";\n" +
			           "}");
		}
		
		[Test]
		public void FunctionWithImplicitReturn3()
		{
			TestMember("Public Function run(i As Integer) As CustomType\n" +
			           " While something\n" +
			           "   run = New CustomType()\n" +
			           " End While\n" +
			           "End Function",
			           "public CustomType run(int i)\n" +
			           "{\n" +
			           "\tCustomType " + VBNetConstructsConvertVisitor.FunctionReturnValueName + " = null;\n" +
			           "\twhile (something) {\n" +
			           "\t\t" + VBNetConstructsConvertVisitor.FunctionReturnValueName + " = new CustomType();\n" +
			           "\t}\n" +
			           "\treturn " + VBNetConstructsConvertVisitor.FunctionReturnValueName + ";\n" +
			           "}");
		}
		
		[Test]
		public void Exponentiation()
		{
			TestStatement("i = i ^ 2", "i = Math.Pow(i, 2);");
			TestStatement("i ^= 2", "i = Math.Pow(i, 2);");
		}
		
		[Test]
		public void AddNotParenthesis()
		{
			TestStatement("a = Not b = c", "a = !(b == c);");
		}
		
		[Test]
		public void NotIsNothing()
		{
			TestStatement("a = Not fs Is Nothing",
			              "a = (fs != null);");
		}
		
		[Test]
		public void StaticMethodVariable()
		{
			TestMember(@"Private Sub Test
	Static j As Integer = 0
	j += 1
End Sub",
			           @"private void Test()
{
	static_Test_j += 1;
}
static int static_Test_j = 0;");
		}
		
		[Test]
		public void StaticMethodVariable2()
		{
			TestMember(@"Private Sub Test
	Static j As Integer = 0
	j += 1
End Sub
Private Sub Test2
	Static j As Integer = 0
	j += 2
End Sub",
			           @"private void Test()
{
	static_Test_j += 1;
}
static int static_Test_j = 0;
private void Test2()
{
	static_Test2_j += 2;
}
static int static_Test2_j = 0;");
		}
		
		[Test]
		public void UsingStatementForExistingVariable()
		{
			TestStatement("Using sw\nEnd Using",
			              "using (sw) {\n}");
		}
		
		[Test]
		public void WithStatementTest()
		{
			TestStatement("With Ejes\n" +
			              "\t.AddLine(p1, p2)\n" +
			              "End With",
			              "{\n\tEjes.AddLine(p1, p2);\n}");
		}
		
		[Test]
		public void NestedWithStatements()
		{
			TestStatement(
				"With tb1\n" +
				"  With .Font\n" +
				"    .Italic = True\n" +
				"  End With\n" +
				"End With",
				
				"{\n\t{\n\t\ttb1.Font.Italic = true;\n\t}\n}");
		}
		
		[Test]
		public void NestedWithStatements2()
		{
			TestStatement(
				"With tb1\n" +
				"  With .Something.Font\n" +
				"    .Italic = True\n" +
				"  End With\n" +
				"End With",
				
				"{\n\t{\n\t\ttb1.Something.Font.Italic = true;\n\t}\n}");
		}
		
		[Test]
		public void StructureWithImplicitPublicField()
		{
			TestMember("Public Structure Example \n Dim x As Object \n End Structure",
			           "public struct Example\n{\n\tpublic object x;\n}");
		}
		
		[Test]
		public void InnerClassVisibility()
		{
			TestMember("Class Inner \n End Class",
			           "public class Inner\n{\n}");
		}
		
		[Test]
		public void InnerDelegateVisibility()
		{
			TestMember("Delegate Sub Test()",
			           "public delegate void Test();");
		}
		
		[Test]
		public void InterfaceVisibility()
		{
			TestMember("Public Interface ITest\n" +
			           "\tSub Test()\n" +
			           "\tProperty Name As String\n" +
			           "End Interface",
			           "public interface ITest\n" +
			           "{\n" +
			           "\tvoid Test();\n" +
			           "\tstring Name {\n" +
			           "\t\tget;\n" +
			           "\t\tset;\n" +
			           "\t}\n" +
			           "}");
		}
		
		[Test]
		public void ImportAliasPrimitiveType()
		{
			TestProgram("Imports T = System.Boolean", "using T = System.Boolean;\r\n");
		}
		
		[Test]
		public void GlobalTypeReference()
		{
			TestStatement("Dim a As Global.System.String", "global::System.String a;");
		}
		
		[Test]
		public void FieldReferenceOnCastExpression()
		{
			TestStatement("CType(obj, IDisposable).Dispose()", "((IDisposable)obj).Dispose();");
		}
		
		[Test]
		public void ArrayCreationUpperBound()
		{
			TestStatement("Dim i As String() = New String(1) {}",
			              "string[] i = new string[2];");
			TestStatement("Dim i(1) As String",
			              "string[] i = new string[2];");
			TestStatement("Dim i As String() = New String(1) {\"0\", \"1\"}",
			              "string[] i = new string[2] {\"0\", \"1\"};");
			TestStatement("Dim i As String(,) = New String(5, 5) {}",
			              "string[,] i = new string[6, 6];");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -