public void When_InvalidCodeSupplied_Then_CompilationFails()
    {
        string code = @"usi ng System;";

        CSharpCompilers.Compilation compilation = compiler.Compile(code);
        Assert.NotEmpty(compilation.GetErrors());
    }
    public void When_NamespaceNull_Then_RunFails()
    {
        CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);
        var e = Assert.Throws <RunError>(() => compilation.Run(new Compilation.RunOptions()));

        Assert.Equal(e.Message, "The namespace must not be blank");
    }
    public void When_VoidMethod_And_NoArgs_Then_RunSucceeds()
    {
        CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);
        var runOpts = TestUtils.builRunOpts().WithMethod(TestUtils.VOID_METHOD_NO_ARGS);
        var result  = compilation.Run(runOpts);

        Assert.Null(result);
    }
    public void When_NonVoidMethod_And_NoArgs_Then_RunSucceeds()
    {
        CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);
        var runOpts = TestUtils.builRunOpts().WithMethod(TestUtils.NON_VOID_METHOD_NO_ARGS);
        var result  = compilation.Run(runOpts);

        Assert.Equal(result, TestUtils.METHOD_RETURN_VALUE);
    }
    public void When_FieldWithSameNameExists_ButNotMethod_Then_RunFails()
    {
        CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);
        var runOpts = TestUtils.builRunOpts().WithMethod(TestUtils.FIELD);
        var e       = Assert.Throws <RunError>(() => compilation.Run(runOpts));

        Assert.Equal(e.Message,
                     $"Could not find method '{TestUtils.FIELD}' in class '{runOpts.GetClass()}' and namespace '{runOpts.GetNamespace()}'");
    }
    public void When_MethodDoesNotExist_Then_RunFails()
    {
        CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);
        var runOpts = TestUtils.builRunOpts().WithMethod("NonExistentMethod");
        var e       = Assert.Throws <RunError>(() => compilation.Run(runOpts));

        Assert.Equal(e.Message,
                     $"Could not find method 'NonExistentMethod' in class '{runOpts.GetClass()}' and namespace '{runOpts.GetNamespace()}'");
    }
    public void When_ClassEmpty_Then_RunFails()
    {
        CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);
        var e = Assert.Throws <RunError>(() => compilation.Run(
                                             new Compilation.RunOptions()
                                             .WithNamespace(TestUtils.NAMESPACE)
                                             .WithClass(string.Empty)));

        Assert.Equal(e.Message, "The class must not be blank");
    }
    public void When_CompilationErrors_And_RunCalled_Then_ExceptionThrown()
    {
        string code = @"usi ng System;";

        CSharpCompilers.Compilation compilation = compiler.Compile(code);
        var e = Assert.Throws <CompilationError>(() => compilation.Run(
                                                     new Compilation.RunOptions().WithNamespace("A").WithClass("B").WithMethod("C")));

        Assert.Equal(e.Message, String.Concat(compilation.GetErrors()));
    }
    public void When_MethodNull_Then_RunFails()
    {
        CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);
        var e = Assert.Throws <RunError>(() => compilation.Run(
                                             new Compilation.RunOptions()
                                             .WithNamespace(TestUtils.NAMESPACE)
                                             .WithClass(TestUtils.CLASS)));

        Assert.Equal(e.Message, "The method must not be blank");
    }
    public void When_InvalidCodeSupplied_Then_CompilationErrorsAreAccurate()
    {
        string code = @"usi ng System;";

        CSharpCompilers.Compilation compilation = compiler.Compile(code);
        Assert.Equal(4, compilation.GetErrors().ToArray().Length);
        Assert.True(compilation.GetErrors().Exists(e => e.Id == "CS1002"));
        Assert.True(compilation.GetErrors().Exists(e => e.Id == "CS0116"));
        Assert.True(compilation.GetErrors().Exists(e => e.Id == "CS1022"));
        Assert.True(compilation.GetErrors().Exists(e => e.Id == "CS0246"));
    }
    public void When_MethodHasArgs_Then_RunSucceeds()
    {
        CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);

        string firstArg  = "abcd";
        string secondArg = "-1234";

        var runOpts = TestUtils.builRunOpts()
                      .WithMethod(TestUtils.METHOD_WITH_ARGS)
                      .WithArgs(new string[] { firstArg, secondArg });
        var result = compilation.Run(runOpts);

        Assert.Equal(result, firstArg + secondArg);
    }
    public void TestCompile()
    {
        string   code     = File.ReadAllText(@"C:\Users\Ovidiu\Desktop\EventsToSpadNext\src\demo-scripts.cs");
        Compiler compiler = new SPADNextCompiler(@"E:\SPAD.neXt\SPAD.Interfaces.dll");

        CSharpCompilers.Compilation compilation = compiler.Compile(code);
        IEnumerable <Diagnostic>    errors      = compilation.GetErrors();

        if (errors.Count() > 0)
        {
            foreach (Diagnostic diagnostic in errors)
            {
                LOG.WriteLine($"\t{diagnostic.Id}: {diagnostic.GetMessage()}");
            }
            Assert.True(false, "Code compilation failed. See the test output logs for details.");
        }
    }
 public void When_ValidCodeSupplied_Then_CompilationSucceeds()
 {
     CSharpCompilers.Compilation compilation = compiler.Compile(TestUtils.CODE);
     Assert.Empty(compilation.GetErrors());
 }
 public void When_EmptyCodeSupplied_Then_CompilationSucceeds()
 {
     CSharpCompilers.Compilation compilation = compiler.Compile(string.Empty);
     Assert.Empty(compilation.GetErrors());
 }