Пример #1
0
        public void Compile_TestedClassesWithOneTestedMethod_Success()
        {
            Input input = new Input()
            {
                Code = new string[] { @" using System;

                    using Testility.Engine.Attribute;

                    [TestedClasses(Name = ""Account"", Description = ""Account class"")]
                    public class Account
                    {
                        public double Current { get; private set; }

                        [TestedMethod(Name = ""add"",
                            Description =@""add amount to your account, can not by less then 0 and more then 100,
                                            in such cases ArgumentException should be thrown"")]
                        public void add(double amount)
                        {
                            Current += amount;
                        }
                    }" },
                Language = "CSharp",
                SolutionName = "Test",
                ReferencedAssemblies = new string[] { "System.dll" }
            };

            Result result = compiler.Compile(input);

            Assert.AreEqual(0, result.Errors.Count);
            Assert.AreEqual(1, result.Classes.Count);
            Assert.AreEqual("Account", result.Classes.First().Name);
            Assert.AreEqual(1, result.Classes.First().Methods.Count);
            Assert.AreEqual("add", result.Classes.First().Methods.First().Name);
            Assert.AreEqual(0, result.Classes.First().Methods.First().Tests.Count);
        }
Пример #2
0
 private Result compile(Input input)
 {
     Compiler compiler;
     Result r;
     System.AppDomain unitDomain = null;
     try
     {
         Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
         AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
         unitDomain = AppDomain.CreateDomain("uTestDomain", evidence, setup);
         Type type = typeof(Compiler);
         compiler = (Compiler)unitDomain.CreateInstanceFrom(
                 type.Assembly.Location,
                 type.FullName).Unwrap();
         foreach (string s in GetAssemblies(Assembly.GetExecutingAssembly()))
         {
             compiler.LoadAssembly(s);
         }
         r = compiler.Compile(input);
     }
     finally
     {
         if (unitDomain != null) System.AppDomain.Unload(unitDomain);
     }
     return r;
 }
Пример #3
0
        public void Compile_ProperClass_Compiled()
        {
            Input input = new Input()
            {
                Code = new string[] { "using System; public class Test{}" },
                Language = "CSharp",
                SolutionName = "Test",
                ReferencedAssemblies = new string [] { "System.dll" }
            };

            Result result = compiler.Compile(input);

            Assert.AreEqual(0, result.Errors.Count);
        }
Пример #4
0
 private Result invoke(Func<Input, Result> invoke, Input input)
 {
     Result ret = null;
     try
     {
         ret = invoke(input);
     }
     finally
     {
         if (!string.IsNullOrEmpty(ret?.TemporaryFile ?? ""))
         {
             if (File.Exists(ret.TemporaryFile))
             {
                 File.Delete(ret.TemporaryFile);
             }
         }
     }
     return ret;
 }
Пример #5
0
        public Result Compile(Input input)
        {
            Result result = new Result();
            CodeDomProvider provider = null;
            try
            {
                provider = CodeDomProvider.CreateProvider(input.Language);
            }
            catch (ConfigurationException)
            { }
            if (provider != null)
            {
                CompilerParameters compilerparameters = new CompilerParameters();
                CompilerResults compilingResult = null;
                compilerparameters.GenerateExecutable = false;
                compilerparameters.GenerateInMemory = false;
                result.TemporaryFile = compilerparameters.OutputAssembly = string.Format(@"{0}\{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), (string.IsNullOrEmpty(input.SolutionName) ? Guid.NewGuid().ToString() : input.SolutionName) + ".dll");
                compilerparameters.TreatWarningsAsErrors = false;

                var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies().ToList();
                var assemblyLocations = assemblies.Select(a =>
                  Assembly.ReflectionOnlyLoad(a.FullName).Location).ToList();
                assemblyLocations.Add(Assembly.GetExecutingAssembly().Location);

                foreach (string reference in input.ReferencedAssemblies)
                {
                    if (assemblies.FirstOrDefault(a => a.Name == Path.GetFileNameWithoutExtension(reference)) == null)
                    {
                        compilerparameters.ReferencedAssemblies.Add(reference);
                    }
                }
                compilerparameters.ReferencedAssemblies.AddRange(assemblyLocations.ToArray());

                compilingResult = provider.CompileAssemblyFromSource(compilerparameters, input.Code);

                if (compilingResult.Errors.Count == 0)
                {
                    var types = compilingResult.CompiledAssembly.GetTypes().Where(t => t.IsClass);

                    foreach (var t in types)
                    {
                        TestedClassesAttribute attribute = System.Attribute.GetCustomAttributes(t)
                            .Where(a => typeof(TestedClassesAttribute).IsInstanceOfType(a)).FirstOrDefault() as TestedClassesAttribute;
                        Class testedclass = new Class()
                        {
                            Name = attribute?.Name ?? t.Name,
                            Description = attribute?.Description ?? t.Name,
                        };
                        var methods = t.GetMethods().Where(m => m.IsPublic
                                        && !m.IsConstructor && m.DeclaringType == t
                                        && !m.IsSpecialName);
                        foreach (var m in methods)
                        {
                            TestedMethodAttribute methodAttribiute = System.Attribute.GetCustomAttributes(m)
                                .Where(a => typeof(TestedMethodAttribute).IsInstanceOfType(a)).FirstOrDefault() as TestedMethodAttribute;
                            Method testedmethod = new Method()
                            {
                                Name = methodAttribiute?.Name ?? m.Name,
                                Description = methodAttribiute?.Description ?? m.Name
                            };
                            testedclass.Methods.Add(testedmethod);
                            var tests = System.Attribute.GetCustomAttributes(m)
                                .Where(a => typeof(Attribute.TestAttribute).IsInstanceOfType(a))
                                .Select(a => a as Attribute.TestAttribute);
                            foreach (Attribute.TestAttribute a in tests)
                            {
                                testedmethod.Tests.Add(new Model.Test()
                                {
                                    Name = a.Name,
                                    Description = a.Description,
                                    Fail = a.Fail
                                });
                            }
                        }
                        if (testedclass.Methods.Count() > 0)
                        {
                            result.Classes.Add(testedclass);
                        }
                    }
                }
                else
                {
                    foreach (CompilerError error in compilingResult.Errors)
                    {
                        result.Errors.Add(new Error()
                        {
                                ErrorText = error.ErrorText,
                                Column = error.Column,
                                ErrorNumber = error.ErrorNumber,
                                IsWarning = error.IsWarning,
                                Line = error.Line
                        });
                    }
                }
            }
            else
            {
                throw new ArgumentException("Language cannot be recognised");
            }
            return result;
        }
Пример #6
0
        public void Compile_TestedClassesWithThreeTests_Success()
        {
            Input input = new Input()
            {
                Code = new string[] { @" using System;

                    using Testility.Engine.Attribute;

                    [TestedClasses(Name = ""Account"", Description = ""Account class"")]
                    public class Account
                    {
                        public double Current { get; private set; }

                        [TestedMethod(Name = ""add"",
                            Description =@""add amount to your account, can not by less then 0 and more then 100,
                                            in such cases ArgumentException should be thrown"")]
                        [Test(Name = ""Less_Then_Zero"",
                                Description = @""Should check if amount is les then 0, ArgumentException is thrown"",
                                Fail = true)]
                        [Test(Name = ""More_Then_Hundred"",
                                Description = @""Should check if amount is more then 100, ArgumentException is thrown"",
                                Fail = true)]
                        [Test(Name = ""Add_To_Account"",
                                Description = @""Should check if amount between 0 to 100 is proper added"",
                                Fail = false)]
                        public void add(double amount)
                        {
                            Current += amount;
                        }
                    }" },
                Language = "CSharp",
                SolutionName = "Test",
                ReferencedAssemblies = new string[] { "System.dll" }
            };

            Result result = compiler.Compile(input);

            Assert.AreEqual(0, result.Errors.Count);
            Assert.AreEqual(1, result.Classes.Count);
            Assert.AreEqual("Account", result.Classes.First().Name);
            Assert.AreEqual(1, result.Classes.First().Methods.Count);
            Assert.AreEqual("add", result.Classes.First().Methods.First().Name);
            Assert.AreEqual(3, result.Classes.First().Methods.First().Tests.Count);
            Assert.AreEqual("Less_Then_Zero", result.Classes.First().Methods.First().Tests.First().Name);
            Assert.AreEqual("More_Then_Hundred", result.Classes.First().Methods.First().Tests.Skip(1).Take(1).First().Name);
            Assert.AreEqual("Add_To_Account", result.Classes.First().Methods.First().Tests.Skip(2).Take(1).First().Name);
        }
Пример #7
0
 public void Compile_WithUnknowLanguage_ArgumentException()
 {
     Input input = new Input()
     {
         Code = new string[] { "public class Test{}" },
         Language = "Pascal"
     };
     compiler.Compile(input);
 }
Пример #8
0
 public void Compile_WithOutSourceCode_ArgumentException()
 {
     Input input = new Input();
     compiler.Compile(input);
 }
Пример #9
0
 private Result runTests(Input i, Result r)
 {
     if (r != null)
     {
         if (r.Errors.Count() == 0 && !string.IsNullOrEmpty(r.TemporaryFile))
         {
             System.AppDomain unitDomain = null;
             TestRuner runner;
             try
             {
                 Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
                 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
                 unitDomain = AppDomain.CreateDomain("uTestDomain", evidence, setup);
                 Type type = typeof(TestRuner);
                 runner = (TestRuner)unitDomain.CreateInstanceFrom(
                         type.Assembly.Location,
                         type.FullName).Unwrap();
                 foreach (string s in GetAssemblies(Assembly.GetExecutingAssembly()))
                 {
                     runner.LoadAssembly(s);
                 }
                 r = runner.Run(r.TemporaryFile, r);
             }
             finally
             {
                 if (unitDomain != null) System.AppDomain.Unload(unitDomain);
             }
         }
     }
     return r;
 }
Пример #10
0
 public Result RunTests(Input input)
 {
     Func<Result, Input, Result> testRuner = (x, y) => runTests(y, x);
     Func<Input, Result> func = testRuner.Curry()(compile(input));
     return invoke(func, input);
 }
Пример #11
0
 public Result Compile(Input input)
 {
     return invoke(compile, input);
 }
Пример #12
0
 public Result RunTests(Input input)
 {
     Contract.Requires<ArgumentNullException>(input.Code != null && input.Code.Length > 0);
     return null;
 }