public void WithString_Translate_ReturnsString() { var translator = new DefaultTranslator <string>(); var value = translator.Translate("hello"); value.ShouldBe("hello"); }
public void WithInt_Translate_ReturnsInt() { var translator = new DefaultTranslator <int>(); var value = translator.Translate("100"); value.ShouldBe(100); }
public void WithBool_Translate_ReturnsBool(string arg) { var translator = new DefaultTranslator <bool>(); var value = translator.Translate(arg); value.ShouldBeTrue(); }
public void RepeatedReDimInOutermostScope() { var source = @" ReDim a(0) ReDim a(1) ReDim a(2)"; var trimmedTranslatedStatements = DefaultTranslator.Translate(source, new string[0], OuterScopeBlockTranslator.OutputTypeOptions.Executable) .Select(s => s.Content.Trim()) .ToArray(); Assert.Equal(1, trimmedTranslatedStatements.Count(s => s == "a = null;")); Assert.Equal(1, trimmedTranslatedStatements.Count(s => s == "public object a { get; set; }")); }
/// <summary> /// This will never return null or an array containing any nulls, blank values or values with leading or trailing whitespace or values containing line /// returns (this format makes the Assert.Equals easier, where it can make array comparisons easily but not any IEnumerable implementation) /// </summary> public static string[] GetTranslatedStatements(string content, NonNullImmutableList <string> externalDependencies) { if (content == null) { throw new ArgumentNullException("content"); } if (externalDependencies == null) { throw new ArgumentNullException("externalDependencies"); } return(DefaultTranslator .Translate( content, externalDependencies, OuterScopeBlockTranslator.OutputTypeOptions.WithoutScaffolding, renderCommentsAboutUndeclaredVariables: false ) .Select(s => s.Content) .Where(s => s != "") .ToArray()); }
public void UndeclaredLoopVariablesMustBeAddedToTheEnvironmentReferences() { var source = @" For Each value In Array(1, 2) Next " ; var expected = @" using System; using System.Collections; using System.Runtime.InteropServices; using VBScriptTranslator.RuntimeSupport; using VBScriptTranslator.RuntimeSupport.Attributes; using VBScriptTranslator.RuntimeSupport.Exceptions; using VBScriptTranslator.RuntimeSupport.Compat; namespace TranslatedProgram { public class Runner { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; public Runner(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); _ = compatLayer; } public GlobalReferences Go() { return Go(new EnvironmentReferences()); } public GlobalReferences Go(EnvironmentReferences env) { if (env == null) throw new ArgumentNullException(""env""); var _env = env; var _outer = new GlobalReferences(_, _env); var enumerationContent1 = _.ENUMERABLE(_.CALL(this, _, ""ARRAY"", _.ARGS.Val((Int16)1).Val((Int16)2))).GetEnumerator(); while (true) { if (!enumerationContent1.MoveNext()) break; _env.value = enumerationContent1.Current; } return _outer; } public class GlobalReferences { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; private readonly GlobalReferences _outer; private readonly EnvironmentReferences _env; public GlobalReferences(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); if (env == null) throw new ArgumentNullException(""env""); _ = compatLayer; _env = env; _outer = this; } } public class EnvironmentReferences { public object value { get; set; } } } }" ; var output = DefaultTranslator .Translate(source, new string[0], renderCommentsAboutUndeclaredVariables: false) .Select(s => s.Content) .Where(s => s != "") .ToArray(); Assert.Equal( expected.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(s => s.Trim()).Where(s => s != "").ToArray(), output.Select(s => s.Trim()).Where(s => s != "").ToArray() ); }
public void RuntimeDateLiteralPresent() { var source = "If (a = #29 May 2015#) Then\nEnd If"; var expected = @" using System; using System.Collections; using System.Collections.ObjectModel; using System.Runtime.InteropServices; using VBScriptTranslator.RuntimeSupport; using VBScriptTranslator.RuntimeSupport.Attributes; using VBScriptTranslator.RuntimeSupport.Exceptions; using VBScriptTranslator.RuntimeSupport.Compat; namespace TranslatedProgram { public class Runner { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; public Runner(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); _ = compatLayer; } public GlobalReferences Go() { return Go(new EnvironmentReferences()); } public GlobalReferences Go(EnvironmentReferences env) { if (env == null) throw new ArgumentNullException(""env""); var _env = env; var _outer = new GlobalReferences(_, _env); RuntimeDateLiteralValidator.ValidateAgainstCurrentCulture(_); if (_.IF(_.EQ(_.NullableDATE(_env.a), _.DateLiteralParser.Parse(""29 May 2015"")))) { } return _outer; } private static class RuntimeDateLiteralValidator { private static readonly ReadOnlyCollection<Tuple<string, int[]>> _literalsToValidate = new ReadOnlyCollection<Tuple<string, int[]>>(new[] { Tuple.Create(""29 May 2015"", new[] { 1 }) }); public static void ValidateAgainstCurrentCulture(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); foreach (var dateLiteralValueAndLineNumbers in _literalsToValidate) { try { compatLayer.DateLiteralParser.Parse(dateLiteralValueAndLineNumbers.Item1); } catch { throw new SyntaxError(string.Format( ""Invalid date literal #{0}# on line{1} {2}"", dateLiteralValueAndLineNumbers.Item1, (dateLiteralValueAndLineNumbers.Item2.Length == 1) ? """" : ""s"", string.Join<int>("", "", dateLiteralValueAndLineNumbers.Item2) )); } } } } public class GlobalReferences { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; private readonly GlobalReferences _outer; private readonly EnvironmentReferences _env; public GlobalReferences(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); if (env == null) throw new ArgumentNullException(""env""); _ = compatLayer; _env = env; _outer = this; } } public class EnvironmentReferences { public object a { get; set; } } } }" ; Assert.Equal( expected.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(s => s.Trim()).Where(s => s != "").ToArray(), DefaultTranslator.Translate(source, new string[0], OuterScopeBlockTranslator.OutputTypeOptions.Executable).Select(s => s.Content.Trim()).Where(s => s != "").ToArray() ); }
public void NoRuntimeDateLiteralPresent() { var source = "If (a = #29 5 2015#) Then\nEnd If"; var expected = @" using System; using System.Collections; using System.Runtime.InteropServices; using VBScriptTranslator.RuntimeSupport; using VBScriptTranslator.RuntimeSupport.Attributes; using VBScriptTranslator.RuntimeSupport.Exceptions; using VBScriptTranslator.RuntimeSupport.Compat; namespace TranslatedProgram { public class Runner { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; public Runner(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); _ = compatLayer; } public GlobalReferences Go() { return Go(new EnvironmentReferences()); } public GlobalReferences Go(EnvironmentReferences env) { if (env == null) throw new ArgumentNullException(""env""); var _env = env; var _outer = new GlobalReferences(_, _env); if (_.IF(_.EQ(_.NullableDATE(_env.a), _.DateLiteralParser.Parse(""29 5 2015"")))) { } return _outer; } public class GlobalReferences { private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _; private readonly GlobalReferences _outer; private readonly EnvironmentReferences _env; public GlobalReferences(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env) { if (compatLayer == null) throw new ArgumentNullException(""compatLayer""); if (env == null) throw new ArgumentNullException(""env""); _ = compatLayer; _env = env; _outer = this; } } public class EnvironmentReferences { public object a { get; set; } } } }" ; Assert.Equal( expected.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(s => s.Trim()).Where(s => s != "").ToArray(), DefaultTranslator.Translate(source, new string[0], OuterScopeBlockTranslator.OutputTypeOptions.Executable).Select(s => s.Content.Trim()).Where(s => s != "").ToArray() ); }