public void ZeroStepResultsInInfiniteLoopWhenAscending() { var source = @" Dim i: For i = 1 To 5 Step 0 Next " ; var expected = new[] { "for (_outer.i = (Int16)1; _.StrictLTE(_outer.i, 5);)", "{", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void SimpleDoWhile() { var source = @" DO WHILE i > 10 LOOP " ; var expected = new[] { "while (_.IF(_.GT(_.NullableNUM(_env.i), (Int16)10)))", "{", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void PropertyGetterThatHasMultipleLinesFollowsStandardFormat() { var source = @" CLASS C1 PUBLIC PROPERTY GET Name WScript.Echo ""get_Name"" Name = ""C1"" END PROPERTY END CLASS" ; var expected = @" [ComVisible(true)] [SourceClassName(""C1"")] public sealed class c1 { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; private readonly EnvironmentReferences _env; private readonly GlobalReferences _outer; public c1(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env, GlobalReferences outer) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); if (env == null) throw new ArgumentNullException(""env""); if (outer == null) throw new ArgumentNullException(""outer""); _ = compatLayer; _env = env; _outer = outer; } [TranslatedProperty(""Name"")] public object name() { object retVal1 = null; _.CALL(this, _env.wscript, ""Echo"", _.ARGS.Val(""get_Name"")); retVal1 = ""C1""; return retVal1; } }" ; Assert.Equal( expected.Replace(Environment.NewLine, "\n").Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void AscendingLoopThatRollsOverLoopVariableIntoLongType() { var source = @" Dim i: For i = 1 To 32768 Next " ; var expected = new[] { "var loopStart1 = _.NUM((Int16)1, 32768);", "for (_outer.i = loopStart1; _.StrictLTE(_outer.i, 32768); _outer.i = _.ADD(_outer.i, (Int16)1))", "{", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void SingleErrorTrappedStatement() { var source = @" On Error Resume Next WScript.Echo ""Test1"" " ; var expected = @" var errOn1 = _.GETERRORTRAPPINGTOKEN(); _.STARTERRORTRAPPINGANDCLEARANYERROR(errOn1); _.HANDLEERROR(errOn1, () => { _.CALL(this, _env.wscript, ""Echo"", _.ARGS.Val(""Test1"")); }); _.RELEASEERRORTRAPPINGTOKEN(errOn1);" ; Assert.Equal( SplitOnNewLinesSkipFirstLineAndTrimAll(expected).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void NonPreserveReDimOfFunctionReturnValue() { var source = @" Function F1() ReDim F1(0) End Function"; var expected = @" public object f1() { object retVal1 = null; retVal1 = _.NEWARRAY(new object[] { (Int16)0 }); return retVal1; }"; Assert.Equal( SplitOnNewLinesSkipFirstLineAndTrimAll(expected).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void DescendingLoopWithExplicitFractionalStep() { var source = @" Dim i: For i = 1 To 5 Step 0.1 Next " ; var expected = new[] { "var loopStart1 = _.NUM((Int16)1, (Int16)5, 0.1);", "for (_outer.i = loopStart1; _.StrictLTE(_outer.i, 5); _outer.i = _.ADD(_outer.i, 0.1))", "{", "}" }; Assert.Equal( expected, WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void ByRefFunctionArgumentRequiresSpecialTreatmentIfUsedInConditionsAsByRefArgument_AlsoTheCaseWhenWithinErrorTrapping() { var source = @" Function F1(a) On Error Resume Next If F2(a) Then End If End Function Function F2(ByVal a) End Function " ; var expected = new[] { "public object f1(ref object a)", "{", " object retVal1 = null;", " var errOn2 = _.GETERRORTRAPPINGTOKEN();", " _.STARTERRORTRAPPINGANDCLEARANYERROR(errOn2);", " bool ifResult5;", " object byrefalias4 = a;", " try", " {", " ifResult5 = _.IF(() => _.CALL(this, _outer, \"F2\", _.ARGS.Ref(byrefalias4, v6 => { byrefalias4 = v6; })), errOn2);", " }", " finally { a = byrefalias4; }", " if (ifResult5)", " {", " }", " _.RELEASEERRORTRAPPINGTOKEN(errOn2);", " return retVal1;", "}", "public object f2(object a)", "{", " return null;", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void ByRefArgumentDoesNotRequireByRefArgumentMappingWhenPassedDirectlyToBuiltInFunction() { var source = @" Function F1(x) WScript.Echo TypeName(x) End Function" ; var expected = @" public object f1(ref object x) { object retVal1 = null; _.CALL(this, _env.wscript, ""Echo"", _.ARGS.Val(_.TYPENAME(x))); return retVal1; }" ; Assert.Equal( expected.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void MemberAccessorsInCallStatementsShouldNotBeRenamedAtTranslationTime() { // "Params" is a C# keyword, so we couldn't emit translated code with a method called "Params", but if "a" is an external reference (such as a COM // component) then It may have a methor or property named "Params". As such we mustn't enforce the rewriting of "Params" to something C#-friendly // at compile time (the CALL implementation will have to do some magic) // - The GetTranslatedStatements uses the DefaultTranslator which uses the DefaultRuntimeSupportClassFactory.DefaultNameRewriter which will // ensure that C# keywords are rewritten to something safe var source = @" WScript.Echo a.Params " ; var expected = new[] { "_.CALL(this, _env.wscript, \"Echo\", _.ARGS.Val(_.CALL(this, _env.a, \"Params\")));" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void ValueSettingTargetThatIsByRefFunctionArgumentShouldOnlyBeReadWriteAliasedIfWithinErrorHandling() { var source = @" Function F1(x) Set x = Nothing End Function " ; var expected = @" public object f1(ref object x) { object retVal1 = null; x = VBScriptConstants.Nothing; return retVal1; }" ; Assert.Equal( SplitOnNewLinesSkipFirstLineAndTrimAll(expected).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void PreserveReDimOfUndeclaredVariableInFunctionShouldImplicitlyDeclareTheVariableInLocalScope() { var source = @" Function F1() ReDim Preserve a(0) End Function"; var expected = @" public object f1() { object retVal1 = null; object a = null; a = _.RESIZEARRAY(a, new object[] { (Int16)0 }); return retVal1; }"; Assert.Equal( SplitOnNewLinesSkipFirstLineAndTrimAll(expected).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void IfReturnValueWithBracketsPresentInSubThenRaiseTypeMismatch() { var source = @" PUBLIC SUB F1() F1() = Null END SUB "; var expected = new[] { "public void f1()", "{", " _.SET(VBScriptConstants.Null, this, _.RAISEERROR(new TypeMismatchException(\"'F1'\")));", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void IfTheOnlyExecutableStatementIsSetKnownObjectReturnValueThenTranslateIntoSingleReturnStatement() { var source = @" PUBLIC FUNCTION F1() Set F1 = Nothing END FUNCTION " ; var expected = new[] { "public object f1()", "{", " return VBScriptConstants.Nothing;", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void OutermostScopeFunctionMayNotBeTargetOfCallExpression() { var source = @" Set a = GetSomething.Name Function GetSomething() End Function " ; var expected = new[] { "_env.a = _.OBJ(_.CALL(this, _outer, \"GetSomething\", \"Name\"));", "public object getsomething()", "{", " return null;", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void ConstValuesShouldAlwaysBePassedToFunctionsByVal() { var source = @" Const a = 1 F1 a Function F1(a) End Function " ; var expected = @" _outer.a = (Int16)1; _.CALL(this, _outer, ""F1"", _.ARGS.Val(_outer.a)); public object f1(ref object a) { return null; }" ; Assert.Equal( expected.Replace(Environment.NewLine, "\n").Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void IfTheOnlyExecutableStatementIsSetReturnValueThenTranslateIntoSingleReturnStatement() { var source = @" PUBLIC FUNCTION F1() Set F1 = a END FUNCTION " ; var expected = new[] { "public object f1()", "{", " object a = null; /* Undeclared in source */", " return _.OBJ(a);", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void ReDimFollowingNonDimensionalArrayDimInFunction() { var source = @" Function F1() Dim a() ReDim a(0) End Function"; var expected = @" public object f1() { object retVal1 = null; object a = (object[])null; a = _.NEWARRAY(new object[] { (Int16)0 }); return retVal1; }"; Assert.Equal( SplitOnNewLinesSkipFirstLineAndTrimAll(expected).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void IndexedPropertiesNeedSpecialLoveAndCare() { var source = @" CLASS C1 PUBLIC PROPERTY LET Blah(ByVal i, ByVal j, ByVal value) END PROPERTY END CLASS " ; var expected = @" [ComVisible(true)] [SourceClassName(""C1"")] public sealed class c1 : TranslatedPropertyIReflectImplementation { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; private readonly EnvironmentReferences _env; private readonly GlobalReferences _outer; public c1(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env, GlobalReferences outer) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); if (env == null) throw new ArgumentNullException(""env""); if (outer == null) throw new ArgumentNullException(""outer""); _ = compatLayer; _env = env; _outer = outer; } [TranslatedProperty(""Blah"")] public void blah(object i, object j, object value) { } }" ; Assert.Equal( expected.Replace(Environment.NewLine, "\n").Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void NonGetPropertyCallsSelfIfBracketsAreSpecifiedAroundRecursivePropertyUpdate() { var source = @" CLASS C1 PUBLIC PROPERTY LET Name(value) Name() = ""C1"" END PROPERTY END CLASS" ; var expected = @" [ComVisible(true)] [SourceClassName(""C1"")] public sealed class c1 { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; private readonly EnvironmentReferences _env; private readonly GlobalReferences _outer; public c1(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env, GlobalReferences outer) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); if (env == null) throw new ArgumentNullException(""env""); if (outer == null) throw new ArgumentNullException(""outer""); _ = compatLayer; _env = env; _outer = outer; } [TranslatedProperty(""Name"")] public void name(ref object value) { _.SET(""C1"", this, this, ""Name""); } }" ; Assert.Equal( expected.Replace(Environment.NewLine, "\n").Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void IncludingBracketsWhenSettingTheReturnValueIsTypeMismatch() { var source = @" PUBLIC FUNCTION F1() F1() = Null END FUNCTION " ; var expected = new[] { "public object f1()", "{", " object retVal1 = null;", " _.SET(VBScriptConstants.Null, this, _.RAISEERROR(new TypeMismatchException(\"'F1'\")));", " return retVal1;", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void ByRefFunctionArgumentDoNotRequireSpecialTreatmentIfUsedInConditionsAsByValArgument() { var source = @" Function F1(a) If F2(a) Then End If End Function Function F2(ByVal a) End Function " ; var expected = new[] { "public object f1(ref object a)", "{", " object retVal1 = null;", " bool ifResult4;", " object byrefalias3 = a;", " try", " {", " ifResult4 = _.IF(_.CALL(this, _outer, \"F2\", _.ARGS.Ref(byrefalias3, v5 => { byrefalias3 = v5; })));", " }", " finally { a = byrefalias3; }", " if (ifResult4)", " {", " }", " return retVal1;", "}", "public object f2(object a)", "{", " return null;", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void WithReferenceShouldNotConfuseBracketResolution() { var source = @" Function Render(x) With x .Draw ""Test"" End With End Function" ; var expected = @" public object render(ref object x) { object retVal1 = null; var with2 = _.OBJ(x); _.CALL(this, with2, ""Draw"", _.ARGS.Val(""Test"")); return retVal1; }" ; Assert.Equal( expected.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void DimInsideFunction() { var source = @" Function F1() Dim myVariable End Function " ; var expected = new[] { "public object f1()", "{", "object retVal1 = null;", "object myvariable = null;", "return retVal1;", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void ErrorHidingDoUntilLoopWithErrorThrowingCondition() { var source = @" ON ERROR RESUME NEXT DO UNTIL(1/0) LOOP " ; var expected = new[] { "var errOn1 = _.GETERRORTRAPPINGTOKEN();", "_.STARTERRORTRAPPINGANDCLEARANYERROR(errOn1);", "while (_.IF(() => !_.IF(_.DIV((Int16)1, (Int16)0)), errOn1))", "{", "}", "_.RELEASEERRORTRAPPINGTOKEN(errOn1);" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void IfTheOnlyExecutableStatementIsReturnValueThenTranslateIntoSingleReturnStatement() { var source = @" PUBLIC FUNCTION F1() ' Test simple-return-format functions F1 = CDate(""2007-04-01"") END FUNCTION " ; var expected = new[] { "public object f1()", "{", " // Test simple-return-format functions", " return _.CDATE(\"2007-04-01\");", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void SimpleCaseWithErrorHandlingButWithItDisabledAtRunTimeBeforeTheLoop() { var source = @" On Error Resume Next On Error Goto 0 For Each value In values WScript.Echo value Next " ; var expected = new[] { "var errOn1 = _.GETERRORTRAPPINGTOKEN();", "_.STARTERRORTRAPPINGANDCLEARANYERROR(errOn1);", "_.STOPERRORTRAPPINGANDCLEARANYERROR(errOn1);", "IEnumerator enumerationContent2 = null;", "_.HANDLEERROR(errOn1, () => {", " enumerationContent2 = _.ENUMERABLE(_env.values).GetEnumerator();", "});", "while (true)", "{", " if (enumerationContent2 != null)", " {", " if (!enumerationContent2.MoveNext())", " break;", " _env.value = enumerationContent2.Current;", " }", " _.CALL(this, _env.wscript, \"Echo\", _.ARGS.Ref(_env.value, v3 => { _env.value = v3; }));", " if (enumerationContent2 == null)", " break;", "}", "_.RELEASEERRORTRAPPINGTOKEN(errOn1);" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void IfByValArgumentIsRequiredForLoopConstraintAndIsPassedToAnotherFunctionByRefThenNoByRefMappingIsRequiredAsTheFirstArgumentWasByVal() { var source = @" Function F1(ByVal x) Dim i: For i = 1 To F2(x) Next End Function Function F2(ByRef value) F2 = value End Function" ; var expected = @" public object f1(object x) { object retVal1 = null; object i = null; var loopEnd3 = _.NUM(_.CALL(this, _outer, ""F2"", _.ARGS.Ref(x, v2 => { x = v2; }))); var loopStart4 = _.NUM((Int16)1, loopEnd3); if (_.StrictLTE(loopStart4, loopEnd3)) { for (i = loopStart4; _.StrictLTE(i, loopEnd3); i = _.ADD(i, (Int16)1)) { } } return retVal1; } public object f2(ref object value) { return _.VAL(value); }" ; Assert.Equal( expected.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(s => s.Trim()).Where(s => s != "").ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void NestedIfBlocksDoNotConfuseTheTranslatorScopeAccessInformationParentBlockTracking() { var source = @" If True Then If True Then End If End If " ; var expected = new[] { "if (_.IF(true))", "{", " if (_.IF(true))", " {", " }", "}", }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }
public void ByteLoopStartAndEndValuesWithImplicitStepWillGetAnIntegerStep() { var source = @" Dim i: For i = CByte(1) To CByte(5) Next " ; var expected = new[] { "var loopEnd1 = _.CBYTE(5);", "var loopStart2 = _.NUM(_.CBYTE(1), loopEnd1, (Int16)1);", "if (_.StrictLTE(loopStart2, loopEnd1))", "{", " for (_outer.i = loopStart2; _.StrictLTE(_outer.i, loopEnd1); _outer.i = _.ADD(_outer.i, (Int16)1))", " {", " }", "}" }; Assert.Equal( expected.Select(s => s.Trim()).ToArray(), WithoutScaffoldingTranslator.GetTranslatedStatements(source, WithoutScaffoldingTranslator.DefaultConsoleExternalDependencies) ); }