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()); }
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()); }
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()); }
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()); }
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()); }
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()); }
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()); }