Пример #1
0
 public void GenerateSuper_WithSimpleClass_ShouldGenerateEmptyClassInheritingFromBase()
 {
     var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") }).GenerateSuper(typeof(TestModel));
     var expected = @"namespace Southpaw.Output.Tests
     {
     public class TestModelViewModel : TestModelViewModelBase
     {
     }
     }";
     Console.Write(actual);
     Assert.AreEqual(expected, actual);
 }
Пример #2
0
 public void GenerateValidateMethod_WithClassWithNoAttributes_ShouldNotGenerateAnything()
 {
     var generator = new ViewModelGenerator(new ViewModelGeneratorOptions());
     var outputWriter = new OutputWriter();
     generator.GenerateBaseValidateMethod(typeof(ModelWithArrayProperty), outputWriter);
     Assert.AreEqual("", outputWriter.ToString());
 }
Пример #3
0
        public void GenerateBase_WithSimpleClass_ShouldWork()
        {
            var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") }).GenerateBase(typeof(TestModel));
            var expected = @"namespace Southpaw.Output.Tests
            {
            public class TestModelViewModelBase : ViewModel
            {
            public string Name
            {
            get { return (string)GetProperty(""Name""); }
            set { SetProperty(""Name"", value); }
            }

            public string Email
            {
            get { return (string)GetProperty(""Email""); }
            set { SetProperty(""Email"", value); }
            }

            public int Id
            {
            get { return (int)GetProperty(""Id""); }
            set { SetProperty(""Id"", value); }
            }

            public bool SetFromJSON(Dictionary<string, object> json, ViewSetOptions options)
            {
            if (json == null)
                return true;
            return base.Set(json, options);
            }
            public void SetNameFromString(string value)
            {
            SetPropertyFromString(""Name"", value, typeof(string), false);
            }

            public void SetEmailFromString(string value)
            {
            SetPropertyFromString(""Email"", value, typeof(string), false);
            }

            public void SetIdFromString(string value)
            {
            SetPropertyFromString(""Id"", value, typeof(int), false);
            }

            }
            }";
            Console.Write(actual);
            Assert.AreEqual(expected, actual);
        }
Пример #4
0
 public void GenerateEnum_WithExplicitValues_ShouldWork()
 {
     var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
         .GenerateEnum(typeof(TestOtherEnum));
     var expected = @"namespace Southpaw.Output.Tests
     {
     public enum TestOtherEnum
     {
     Hello = 10,
     There = 20
     }
     }";
     Console.Write(actual);
     Assert.AreEqual(expected, actual);
 }
Пример #5
0
 public void GenerateBase_ClassInheritingFromAnother_ShouldRecordNestedClassForGeneration()
 {
     var generator = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") });
     generator.GenerateBase(typeof(ModelInheritingFromAnother));
     Assert.Contains(typeof(ModelWithDateTimeProperty), generator._nestedPropertyTypes);
 }
Пример #6
0
        public void GenerateBase_ClassWithPropertyTypeInTheSameAssembly_ShouldUseViewModelAsAReference()
        {
            var actual = new ViewModelGenerator(
                new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
                .GenerateBase(typeof(ModelWithDateTimeProperty));
            var expected = @"namespace Southpaw.Output.Tests
            {
            public class ModelWithDateTimePropertyViewModelBase : ViewModel
            {
            public Date Prop
            {
            get { return (Date)GetProperty(""Prop""); }
            set { SetProperty(""Prop"", value); }
            }

            public bool SetFromJSON(Dictionary<string, object> json, ViewSetOptions options)
            {
            if (json == null)
                return true;
            return base.Set(json, options);
            }
            public void SetPropFromString(string value)
            {
            SetPropertyFromString(""Prop"", value, typeof(Date), false);
            }

            }
            }";
            Console.Write(actual);
            Assert.AreEqual(expected.Trim(), actual.Trim());
        }
Пример #7
0
        public void GenerateBase_ClassWithNullableProperty_ShouldSetThePropertyAsNullable()
        {
            var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") }).GenerateBase(typeof(ModelWithNullableProperty));
            var expected = @"namespace Southpaw.Output.Tests
            {
            public class ModelWithNullablePropertyViewModelBase : ViewModel
            {
            public int? Age
            {
            get { return (int?)GetProperty(""Age""); }
            set { SetProperty(""Age"", value); }
            }

            public bool SetFromJSON(Dictionary<string, object> json, ViewSetOptions options)
            {
            if (json == null)
                return true;
            return base.Set(json, options);
            }
            public void SetAgeFromString(string value)
            {
            SetPropertyFromString(""Age"", value, typeof(int), true);
            }

            }
            }";
            Console.Write(actual);
            Assert.AreEqual(expected.Trim(), actual.Trim());
        }
Пример #8
0
 public void ValidateType_ClassWithPropertyTypeInTheSameAssembly_ShouldNotThrow()
 {
     var actual = new ViewModelGenerator(
         new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
         .ValidateType(this.GetType().Assembly, typeof(ModelWithNestedAssemblyPropertyType));
 }
Пример #9
0
        public void GenerateBase_ClassWithListOfModelsProperty_ShouldWork()
        {
            var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
                .GenerateBase(typeof(ModelWithListOfModelsProperty));
            var expected = @"namespace Southpaw.Output.Tests
            {
            public class ModelWithListOfModelsPropertyViewModelBase : ViewModel
            {
            public List<Southpaw.Output.Tests.ModelWithDateTimePropertyViewModel> Prop
            {
            get { return (List<Southpaw.Output.Tests.ModelWithDateTimePropertyViewModel>)GetProperty(""Prop""); }
            set { SetProperty(""Prop"", value); }
            }

            public bool SetFromJSON(JsDictionary<string, object> json, ViewSetOptions options)
            {
            if (json == null)
                return true;
            if (json.ContainsKey(""Prop""))
            {
                List<Southpaw.Output.Tests.ModelWithDateTimePropertyViewModel> l = new List<Southpaw.Output.Tests.ModelWithDateTimePropertyViewModel>();
                if (this.Prop != null)
                    l = this.Prop;

                foreach(JsDictionary<string, object> itemJson in (List<JsDictionary<string, object>>)json[""Prop""])
                {
                    Southpaw.Output.Tests.ModelWithDateTimePropertyViewModel x = new Southpaw.Output.Tests.ModelWithDateTimePropertyViewModel();
                    if (!x.SetFromJSON(itemJson, options))
                        return false;
                    l.Add(x);
                }
                json[""Prop""] = l;
            }
            return base.Set(json, options);
            }
            }
            }";
            Console.Write(actual);
            Assert.AreEqual(expected.Trim(), actual.Trim());
        }
Пример #10
0
        public void GenerateBase_ClassWithListProperty_ShouldWork()
        {
            var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
                .GenerateBase(typeof(ModelWithListProperty));
            var expected = @"namespace Southpaw.Output.Tests
            {
            public class ModelWithListPropertyViewModelBase : ViewModel
            {
            public List<string> Prop
            {
            get { return (List<string>)GetProperty(""Prop""); }
            set { SetProperty(""Prop"", value); }
            }

            public bool SetFromJSON(JsDictionary<string, object> json, ViewSetOptions options)
            {
            if (json == null)
                return true;
            return base.Set(json, options);
            }
            }
            }";
            Console.Write(actual);
            Assert.AreEqual(expected.Trim(), actual.Trim());
        }
Пример #11
0
 public void GenerateBase_ClassWithListOfModelsProperty_ShouldAddModelInListForGeneration()
 {
     var generator = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") });
     generator.GenerateBase(typeof(ModelWithListOfModelsProperty));
     Assert.AreEqual(1, generator._nestedPropertyTypes.Count);
     Assert.AreEqual(typeof(ModelWithDateTimeProperty), generator._nestedPropertyTypes[0]);
 }
Пример #12
0
 public void GenerateBase_ClassWithIgnoredProperty_ShouldNotIncludeTheGeneratedPropertyInTheOutput()
 {
     var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
         .GenerateBase(typeof(ModelWithIgnoredProperty));
     var expected = @"namespace Southpaw.Output.Tests
     {
     public class ModelWithIgnoredPropertyViewModelBase : ViewModel
     {
     public bool SetFromJSON(JsDictionary<string, object> json, ViewSetOptions options)
     {
     if (json == null)
         return true;
     return base.Set(json, options);
     }
     }
     }";
     Console.Write(actual);
     Assert.AreEqual(expected.Trim(), actual.Trim());
 }
Пример #13
0
        public void GenerateBase_ClassInheritingFromAnother_ShouldSetUpInheritanceChainCorrectly()
        {
            var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
                .GenerateBase(typeof(ModelInheritingFromAnother));
            var expected = @"namespace Southpaw.Output.Tests
            {
            public class ModelInheritingFromAnotherViewModelBase : Southpaw.Output.Tests.ModelWithDateTimePropertyViewModel
            {
            public string Prop2
            {
            get { return (string)GetProperty(""Prop2""); }
            set { SetProperty(""Prop2"", value); }
            }

            public bool SetFromJSON(JsDictionary<string, object> json, ViewSetOptions options)
            {
            if (json == null)
                return true;
            return base.Set(json, options);
            }
            public void SetProp2FromString(string value)
            {
            SetPropertyFromString(""Prop2"", value, typeof(string), false);
            }

            }
            }";
            Console.Write(actual);
            Assert.AreEqual(expected.Trim(), actual.Trim());
        }
Пример #14
0
 public void GenerateValidateMethod_WithOneAttributeOneProperty_ShouldGenerateCorrectValidation()
 {
     var generator = new ViewModelGenerator(new ViewModelGeneratorOptions());
     var outputWriter = new OutputWriter();
     generator.GenerateBaseValidateMethod(typeof(ModelWithOneValidationAttribute), outputWriter);
     Assert.AreEqual(@"public override bool Validate(Dictionary<string, object> attributes)
     {
     string res = null;
     res = new Southpaw.Runtime.Clientside.Validation.RequiredValidator().Validate(attributes[""Prop""], new Southpaw.Runtime.Clientside.Validation.RequiredValidatorOptions { Property = ""Prop"", AllowEmptyStrings = false, });
     if (res != null) this.Errors.AddError(""Prop"", res);
     return this.Errors.IsError;
     }
     ", outputWriter.ToString());
 }
Пример #15
0
 public void GenerateBase_ClassWithPropertyTypeInTheSameAssembly_ShouldRecordNestedClassForGeneration()
 {
     var generator = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") });
     generator.GenerateBase(typeof(ModelWithNestedAssemblyPropertyType));
     Assert.Contains(typeof(TestModel), generator._nestedPropertyTypes);
 }
Пример #16
0
 public void ValidateType_ClassInheritingFromAnInvalidType_ShouldThrow()
 {
     var actual = new ViewModelGenerator(
         new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
         .ValidateType(this.GetType().Assembly, typeof(ModelInheritingFromAnInvalidType));
 }
Пример #17
0
        public void GenerateBase_ClassWithPropertyTypeInTheSameAssembly_ShouldUseGeneratedViewModelType()
        {
            var actual = new ViewModelGenerator(new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
                .GenerateBase(typeof(ModelWithNestedAssemblyPropertyType));
            var expected = @"namespace Southpaw.Output.Tests
            {
            public class ModelWithNestedAssemblyPropertyTypeViewModelBase : ViewModel
            {
            public Southpaw.Output.Tests.TestModelViewModel Prop
            {
            get { return (Southpaw.Output.Tests.TestModelViewModel)GetProperty(""Prop""); }
            set { SetProperty(""Prop"", value); }
            }

            public bool SetFromJSON(Dictionary<string, object> json, ViewSetOptions options)
            {
            if (json == null)
                return true;
            if (json.ContainsKey(""Prop""))
            {
                if (this.Prop != null)
                {
                    if (this.Prop.SetFromJSON((Dictionary<string, object>)json[""Prop""], options))
                        json.Remove(""Prop"");
                    else
                        return false;
                }
                else
                {
                    Southpaw.Output.Tests.TestModelViewModel x = new Southpaw.Output.Tests.TestModelViewModel();
                    if (!x.SetFromJSON((Dictionary<string, object>)json[""Prop""], options))
                        return false;
                    json[""Prop""] = x;
                }
            }
            return base.Set(json, options);
            }
            }
            }";
            Console.Write(actual);
            Assert.AreEqual(expected.Trim(), actual.Trim());
        }
Пример #18
0
 public void ValidateType_ModelWithValidNullableArrayProperty_ShouldNotThrow()
 {
     var actual = new ViewModelGenerator(
         new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>("Southpaw.Generator.Tests", "Southpaw.Output.Tests") })
         .ValidateType(this.GetType().Assembly, typeof(ModelWithNullableArrayProperty));
 }
Пример #19
0
        public override bool Execute()
        {
            if (!ValidateInput())
                return false;

            //XNamespace xns = "http://schemas.microsoft.com/developer/msbuild/2003";
            //XDocument xmldoc = XDocument.Load(BusinessObjectProjectFile);
            OutputProjectFile = Path.GetFullPath(OutputProjectFile);
            BusinessObjectAssemblyPath = Path.GetFullPath(BusinessObjectAssemblyPath);
            ControllerAssemblyPath = Path.GetFullPath(ControllerAssemblyPath);

            var currentProjects = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection;
            var outputProject = currentProjects.GetLoadedProjects(OutputProjectFile).FirstOrDefault();
            if (outputProject == null)
            {
                outputProject = new Project(OutputProjectFile) {FullPath = OutputProjectFile};
            }

            var vmg = new ViewModelGenerator(
                new ViewModelGeneratorOptions
                    {
                        NamespaceSubstitution =
                            new Tuple<string, string>(ControllerNamespaceSubstitutionSource,
                                                      ControllerNamespaceSubstitutionDestination)
                    });
            foreach (var ti in ValidatorMappings)
            {
                vmg.AddValidator(ti.GetMetadata("csharpType"), ti.GetMetadata("jsType"));
            }

            if (!AddToProject(
                outputProject,
                BusinessObjectAssemblyPath,
                OutputProjectFile,
                BusinessObjectNamespaceSubstitutionSource,
                BusinessObjectNamespaceSubstitutionDestination,
                () => vmg
                ))
                return false;
            if (!AddToProject(
                outputProject,
                ControllerAssemblyPath,
                OutputProjectFile,
                ControllerNamespaceSubstitutionSource,
                ControllerNamespaceSubstitutionDestination,
                () => new ServiceGenerator(
                    new ServiceGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>(ControllerNamespaceSubstitutionSource, ControllerNamespaceSubstitutionDestination) },
                    new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>(ControllerNamespaceSubstitutionSource, ControllerNamespaceSubstitutionDestination) })
                ))
                return false;

            /*
            // business objects:
            // load assembly
            // -> get assembly name
            var assemblyNameNode = businessLibraryProject.AllEvaluatedProperties.FirstOrDefault(p => p.Name == "AssemblyName");
            //var assemblyNameNode = xmldoc.Descendants(xns + "AssemblyName").FirstOrDefault();
            if (assemblyNameNode == null)
            {
                Log.LogError("No assembly name found in Business Object project (" + BusinessObjectProjectFile + ")");
                return false;
            }
            var businessObjectsAssemblyName = assemblyNameNode.EvaluatedValue;

            // -> get output path
            var outputPathProperty = businessLibraryProject.AllEvaluatedProperties.FirstOrDefault(p => p.Name == "OutputPath");
            if (outputPathProperty == null || outputPathProperty.EvaluatedValue == "")
            {
                Log.LogError("No output path found in Business Object project (" + BusinessObjectProjectFile + ")");
                return false;
            }
            Log.LogMessage("Output path:" + outputPathProperty.EvaluatedValue);
            var businessObjectsOutput = outputPathProperty.EvaluatedValue;
            var businessObjectsFolder = Path.GetDirectoryName(BusinessObjectProjectFile);
            var businessObjectAssemblyFullPath = Path.Combine(businessObjectsFolder, businessObjectsOutput, businessObjectsAssemblyName + ".dll");
            if (!File.Exists(businessObjectAssemblyFullPath))
            {
                Log.LogError("Business Object assembly not found in derived output path '" + businessObjectAssemblyFullPath + "'");
                return false;
            }
            Log.LogMessage("Found business objects assembly " + businessObjectAssemblyFullPath);
            // -> find out which of bin/debug/assemmblyname.dll or bin/release.assemblyname.dll is most recent
            // -> (or perhaps use current build configuration?)
            // -> load assembly from path
            var assembly = Assembly.LoadFrom(businessObjectAssemblyFullPath);
            var viewModelGenerator = new ViewModelGenerator(
                new ViewModelGeneratorOptions { NamespaceSubstitution = new Tuple<string, string>(BusinessObjectNamespaceSubstitutionSource, BusinessObjectNamespaceSubstitutionDestination) });
            var res = viewModelGenerator.Generate(assembly);
            var modelsOutputFolder = Path.GetDirectoryName(OutputProjectFile);
            //var businessObjectsOutputFolder = Path.Combine(Path.GetDirectoryName(OutputProjectFile), "ViewModels");
            foreach (var r in res)
            {
                Log.LogMessage("Found output file " + r.PathRelativeToSourceAssembly);
                var path = Path.Combine(modelsOutputFolder, r.PathRelativeToSourceAssembly);
                if (!File.Exists(path) || r.IsForceOverwrite)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                    File.WriteAllText(path, r.Contents);
                }
                if (!outputProject.GetItems("Compile").Any(x => x.UnevaluatedInclude == r.PathRelativeToSourceAssembly))
                {
                    outputProject.AddItem("Compile", r.PathRelativeToSourceAssembly);
                }
            }
            */
            outputProject.Save();

            // pass assembly, parameters to generator
            // generate
            // for each generated file, figure out which folder to put it in
            // -> OutputRoot + "/Generated/GeneratedModelsBase.cs" for base file
            // -> OutputRoot + (classNamespace.Replace(assemblyName, "").ToFolderStructure() + "\" + className + ".cs"
            // add all generated files to the project if they don't already exist

            // controllers:
            // as above
            return true;
        }