Ejemplo n.º 1
0
        public void WhenInitializedWithClassThatHasBaseClass_BaseTypeIsSet()
        {
            var target = new TsClass(typeof(Employee));

            Assert.NotNull(target.BaseType);
            Assert.Equal(typeof(Person), target.BaseType.Type);
        }
Ejemplo n.º 2
0
        public void WhenInitializedWithClassThatHasBaseClass_OnlyPropertiesDefinedInDerivedClassAreCreated()
        {
            var target = new TsClass(typeof(Employee));

            Assert.Single(target.Properties.Where(o => o.MemberInfo == typeof(Employee).GetProperty("Salary")));

            Assert.Empty(target.Properties.Where(o => o.MemberInfo == typeof(Employee).GetProperty("Street")));
            Assert.Empty(target.Properties.Where(o => o.MemberInfo == typeof(Employee).GetProperty("Street")));
        }
Ejemplo n.º 3
0
        public void WhenInitializedWithClassWithEnum_PropertiesCreated()
        {
            var target = new TsClass(typeof(Item));

            Assert.Single(target.Properties.Where(o => o.MemberInfo == typeof(Item).GetProperty("Type")));
            Assert.Single(target.Properties.Where(o => o.MemberInfo == typeof(Item).GetProperty("Id")));
            Assert.Single(target.Properties.Where(o => o.MemberInfo == typeof(Item).GetProperty("Name")));

            Assert.Null(target.BaseType);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Resolves references in the class.
        /// </summary>
        /// <param name="classModel"></param>
        public override void VisitClass(TsClass classModel)
        {
            if (classModel.Module != null) {
                classModel.Module = this.ResolveModule(classModel.Module.Name);
            }

            if (classModel.BaseType != null && classModel.BaseType != TsType.Any) {
                classModel.BaseType = this.ResolveType(classModel.BaseType, false);
            }

            for (int i = 0; i < classModel.Interfaces.Count; i++) {
                classModel.Interfaces[i] = this.ResolveType(classModel.Interfaces[i], false);
            }
        }
Ejemplo n.º 5
0
        public void WhenInitializedWithClassWithEnum_EnumPropertyCreated()
        {
            var target = new TsClass(typeof(Item));

            Assert.Single(target.Properties.Where(o => o.MemberInfo == typeof(Item).GetProperty("Type")));
            var property = target.Properties.Single(o => o.MemberInfo == typeof(Item).GetProperty("Type"));
            Assert.True(property.PropertyType.GetType() == typeof(TsEnum));
            var enumtype = property.PropertyType as TsEnum;
            Assert.NotNull(enumtype);
            Assert.True(enumtype.Values.Any());
            Assert.True(enumtype.Values.Any(a => a.Name == "Book" && a.Value == ((int)ItemType.Book).ToString()));
            Assert.True(enumtype.Values.Any(a => a.Name == "Music" && a.Value == ((int)ItemType.Music).ToString()));
            Assert.True(enumtype.Values.Any(a => a.Name == "Clothing" && a.Value == ((int)ItemType.Clothing).ToString()));
            Assert.Null(target.BaseType);
        }
        protected override void AppendClassDefinition(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            string typeName = this.GetTypeName(classModel);
            string visibility = this.GetTypeVisibility(classModel, typeName) ? "export " : "";
            sb.AppendFormatIndented(
                "{0}class {1} extends {2}",
                visibility,
                typeName,
                //all bottom-level classes must extend Backbone.Model.
                classModel.BaseType != null ? this.GetFullyQualifiedTypeName(classModel.BaseType) : "Backbone.Model");

            sb.AppendLine(" {");

            var members = new List<TsProperty>();
            if ((generatorOutput & TsGeneratorOutput.Properties) == TsGeneratorOutput.Properties)
            {
                members.AddRange(classModel.Properties);
            }
            if ((generatorOutput & TsGeneratorOutput.Fields) == TsGeneratorOutput.Fields)
            {
                members.AddRange(classModel.Fields);
            }
            using (sb.IncreaseIndentation())
            {
                foreach (var property in members)
                {
                    if (property.IsIgnored)
                    {
                        continue;
                    }

                    sb.AppendLineIndented(string.Format(
                        "get {0}(): {1} {{ return this.get(\"{0}\"); }}",
                        this.GetPropertyName(property), this.GetPropertyType(property)));

                    sb.AppendLineIndented(string.Format(
                        "set {0}(v: {1}) {{ this.set(\"{0}\", v); }}",
                        this.GetPropertyName(property), this.GetPropertyType(property)));
                }
            }

            sb.AppendLineIndented("}");

            _generatedClasses.Add(classModel);
        }
Ejemplo n.º 7
0
        public void WhenModuleIsSet_ClassIsAddedToModule()
        {
            var module = new TsModule("Tests");
            var target = new TsClass(typeof(Address));

            target.Module = module;

            Assert.Contains(target, module.Classes);
        }
Ejemplo n.º 8
0
        public void WhenInitializedWithInnerClass_ModuleIsSetToNamespaceAndOuterClass()
        {
            var target = new TsClass(typeof(TypeLite.Tests.TestModels.Outer.Inner));

            Assert.NotNull(target.Module);
            Assert.Equal(typeof(TypeLite.Tests.TestModels.Outer.Inner).Namespace + ".Outer", target.Module.Name);
        }
Ejemplo n.º 9
0
        public void WhenInitialized_FieldsAreCreated()
        {
            var target = new TsClass(typeof(Address));

            Assert.Single(target.Fields.Where(o => o.MemberInfo == typeof(Address).GetField("PostalCode")));
        }
Ejemplo n.º 10
0
        public void WhenInitializedWithClassWithBaseTypeObject_BaseTypeIsSetToNull()
        {
            var target = new TsClass(typeof(Address));

            Assert.Null(target.BaseType);
        }
Ejemplo n.º 11
0
        public void WhenInitialized_IsIgnoredIsFalse()
        {
            var target = new TsClass(typeof(Person));

            Assert.False(target.IsIgnored);
        }
Ejemplo n.º 12
0
        public void WhenInitialized_ConstantsHaveCorrectValues()
        {
            var target = new TsClass(typeof(Person));

            var maxAddresses = target.Constants.Single(o => o.MemberInfo == typeof(Person).GetField("MaxAddresses"));
            Assert.Equal(Person.MaxAddresses, maxAddresses.ConstantValue);
        }
Ejemplo n.º 13
0
 public void AppendClassDoc(ScriptBuilder sb, TsClass classModel, string className)
 {
     AppendModelDoc(sb, classModel.Type);
 }
Ejemplo n.º 14
0
        public void WhenModuleIsSetToOtherModule_ClassIsRemovedFromOriginalModule()
        {
            var originalModule = new TsModule("Tests.Original");
            var module = new TsModule("Tests");
            var target = new TsClass(typeof(Address));

            target.Module = originalModule;
            target.Module = module;

            Assert.DoesNotContain(target, originalModule.Classes);
        }
Ejemplo n.º 15
0
        public void WhenInitialized_PropertiesAreCreated()
        {
            var target = new TsClass(typeof(Address));

            Assert.Single(target.Properties.Where(o => o.MemberInfo == typeof(Address).GetProperty("Street")));
            Assert.Single(target.Properties.Where(o => o.MemberInfo == typeof(Address).GetProperty("Town")));
        }
Ejemplo n.º 16
0
        public void WhenInitialized_NameIsSet()
        {
            var target = new TsClass(typeof(Person));

            Assert.Equal("Person", target.Name);
        }
Ejemplo n.º 17
0
        public void WhenInitialized_ModuleIsSetToNamespaceModule()
        {
            var target = new TsClass(typeof(Address));

            Assert.NotNull(target.Module);
            Assert.Equal(typeof(Address).Namespace, target.Module.Name);
        }
Ejemplo n.º 18
0
 /// <summary>
 /// When overridden in a derived class, it can examine or modify the class model.
 /// </summary>
 /// <param name="classModel">The model class being visited.</param>
 public virtual void VisitClass(TsClass classModel)
 {
 }
Ejemplo n.º 19
0
        public void WhenInitializedAndClassHasCustomNameInAttribute_CustomNameIsUsed()
        {
            var target = new TsClass(typeof(CustomClassName));

            Assert.Equal("MyClass", target.Name);
        }
Ejemplo n.º 20
0
 public void AppendClassDoc(ScriptBuilder sb, TsClass classModel, string className)
 {
 }
Ejemplo n.º 21
0
        public void WhenInitialized_ConstantsAreCreated()
        {
            var target = new TsClass(typeof(Person));

            Assert.Single(target.Constants.Where(o => o.MemberInfo == typeof(Person).GetField("MaxAddresses")));
        }