Пример #1
0
        static int Main(string[] args)
        {
            // print args
            Console.WriteLine("Printing args...");
            foreach (string arg in args)
            {
                Console.WriteLine("Arg: " + arg);
            }
            Console.WriteLine();

            // run basic tests
            Console.WriteLine("Running tests...");
            Log(ClassNesting.RunTest(), "ClassNesting");
            Log(ClassVsStruct.RunTest(), "ClassVsStruct");
            Log(Enums.RunTest(), "Enums");
            Log(RefOutParameters.RunTest(), "RefOutParameters");
            Log(FieldsAndProperties.RunTest(), "FieldsAndProperties");
            Log(FlowControlAndEnumerators.RunTest(), "FlowControlAndEnumerators");
            Log(ExtensionMethods.RunTest(), "ExtensionMethods");
            Log(Destructors.RunTest(), "Destructors");
            Log(TryCatch.RunTest(), "TryCatch");
            Log(Interop.RunTest(), "Interop");
            Log(VirtualMethods.RunTest(), "VirtualMethods");
            Log(Generics.RunTest(), "Generics");
            Log(Delegates.RunTest(), "Delegates");
            Log(Indexers.RunTest(), "Indexers");
            Log(Operators.RunTest(), "Operators");
            Log(StringEncoding.RunTest(), "StringEncoding");
            Log(CoreGenericCollections.RunTest(), "CoreGenericCollections");
            Log(NewOverrides.RunTest(), "NewOverrides");
            Log(NumbersToStrings.RunTest(), "NumbersToStrings");
            Console.WriteLine("TESTS DONE!");

            // return result code
            return(99);
        }
Пример #2
0
  public void RoundtripMixedFieldsAndProperties() {
      var expected = new FieldsAndProperties(123) {
          Name = "bob",
          Email = "*****@*****.**",
          Ref = Guid.NewGuid()
      };
      var data = Serializer.Serialize(expected);
      var actual = Deserializer.Deserialize<FieldsAndProperties>(data);
      Assert.Equal(expected.Id, actual.Id);
      Assert.Equal(expected.Name, actual.Name);
      Assert.Equal(expected.Email, actual.Email);
 }
        public override StringBuilder Generate()
        {
            StringBuilder builder       = new();
            var           currentIndent = 0;

            int importCount      = Imports.Count,
                attributeCount   = ClassAttributes.Count,
                dataCount        = FieldsAndProperties.Count,
                constructorCount = Constructors.Count,
                methodCount      = Methods.Count;

            var allEmpty = dataCount < 1 && constructorCount < 1 && methodCount < 1;

            if (importCount > 0)
            {
                Imports.ForEach(import =>
                {
                    builder.Append($"using {import};{NewLine}");
                });
                builder.Append(NewLine);
            }

            if (!string.IsNullOrWhiteSpace(Namespace))
            {
                builder.Append($"namespace {Namespace}{NewLine}{{{NewLine}");
                currentIndent++;
            }

            if (attributeCount > 0)
            {
                IndentStringBuilder(builder, currentIndent);
                builder.Append($"[{string.Join(", ", ClassAttributes)}]{NewLine}");
            }

            IndentStringBuilder(builder, currentIndent);
            builder.Append($"{ClassAccess} class {FileName}");

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance))
            {
                builder.Append(" :");
            }

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Class))
            {
                builder.Append(
                    $" {Relations.First(r=>r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Class).Target.Name}");
            }

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Interface))
            {
                builder.Append(" " + string.Join(", ", Relations.Where(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Interface).Select(x => x.Target.Name)));
            }

            IndentStringBuilder(builder.Append(NewLine), currentIndent++);
            builder.Append($"{{{NewLine}");

            FieldsAndProperties.ForEach(d =>
            {
                IndentStringBuilder(builder, currentIndent);
            });

            Constructors.ForEach(ctr =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{Helper.ToString(ctr.Access)} {ctr.NameType.Name}({string.Join(", ",ctr.Params.Select(p => $"{p.Type} {p.Name}"))}){{{NewLine}");
                IndentStringBuilder(builder, ++currentIndent);
                builder.Append($"{NewLine}");
                IndentStringBuilder(builder, --currentIndent);
                builder.Append($"}}{NewLine}");
            });
            Methods.ForEach(m =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{Helper.ToString(m.Access)} ");
                if (m.NameType.IsStatic)
                {
                    builder.Append(m.NameType.IsStatic ? "static " : "");
                }
                builder.Append($"{m.NameType.Type} {m.NameType.Name}({string.Join(", ", m.Params.Select(p => $"{p.Type} {p.Name}"))}){NewLine}");

                IndentStringBuilder(builder, currentIndent);
                builder.Append($"{{{NewLine}");
                IndentStringBuilder(builder, ++currentIndent);
                builder.Append($"{NewLine}");
                IndentStringBuilder(builder, --currentIndent);
                builder.Append($"}}{NewLine}");
            });

            if (allEmpty)
            {
                IndentStringBuilder(builder, currentIndent);
                builder.Append(NewLine);
            }
            IndentStringBuilder(builder, --currentIndent);
            builder.Append($"}}{(string.IsNullOrWhiteSpace(Namespace) ? "" : NewLine)}");

            if (string.IsNullOrWhiteSpace(Namespace))
            {
                return(builder);
            }
            builder.Append('}');
            currentIndent--;

            return(builder);
        }
        public override StringBuilder Generate()
        {
            StringBuilder builder       = new();
            var           currentIndent = 0;

            if (!string.IsNullOrWhiteSpace(Namespace))
            {
                builder.Append($"package {Namespace};{NewLine}{NewLine}");
            }

            if (Imports.Count > 0)
            {
                builder.Append("import " + string.Join($";{NewLine}import ", Imports.Where(i => !i.Equals(Namespace))) + $";{NewLine}{NewLine}");
            }

            ClassAttributes.ForEach(attr =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{attr}{NewLine}");
            });

            IndentStringBuilder(builder, currentIndent);
            builder.Append($"{ClassAccess} class {FileName}");

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Class))
            {
                builder.Append(
                    $" extends {Relations.First(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Class).Target.Name}");
            }

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Interface))
            {
                builder.Append(" implements " + string.Join(", ", Relations.Where(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Interface).Select(x => x.Target.Name)));
            }

            IndentStringBuilder(builder.Append(NewLine), currentIndent++);
            builder.Append($"{{{NewLine}");

            FieldsAndProperties.ForEach(d =>
            {
            });

            Constructors.ForEach(ctr =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{Helper.ToString(ctr.Access)} {ctr.NameType.Name}({string.Join(", ", ctr.Params.Select(p => $"{p.Type} {p.Name}"))}){{{NewLine}");
                IndentStringBuilder(builder, ++currentIndent);
                builder.Append($"{NewLine}");
                IndentStringBuilder(builder, --currentIndent);
                builder.Append($"}}{NewLine}");
            });
            Methods.ForEach(m =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{Helper.ToString(m.Access)} {m.NameType.Type} {m.NameType.Name}({string.Join(", ", m.Params.Select(p => $"{p.Type} {p.Name}"))}){NewLine}");
                IndentStringBuilder(builder, currentIndent);
                builder.Append($"{{{NewLine}");
                IndentStringBuilder(builder, ++currentIndent);
                builder.Append($"{NewLine}");
                IndentStringBuilder(builder, --currentIndent);
                builder.Append($"}}{NewLine}");
            });

            //builder.Append(NewLine);
            IndentStringBuilder(builder, currentIndent);
            builder.Append(NewLine);
            IndentStringBuilder(builder, --currentIndent);
            return(builder.Append('}'));
        }