public void Custom_complex_types_are_generated_as_interfaces()
        {
            var mapping = new TypeMapping();

            Assert.AreEqual("SimpleModel", mapping.GetTSType(typeof(SimpleModel)));
            TextAssert.ContainsLine("export interface SimpleModel {", mapping.GetGeneratedTypes()[typeof(SimpleModel)]);
        }
示例#2
0
        public void Should_write_custom_imports_if_api_generation_extensions_provided()
        {
            var mapping          = new TypeMapping();
            var customWriterMock = new CustomApiWriter();
            var generator        = createGenerator(mapping, customWriterMock);

            generator.WriteApiClientClass();

            TextAssert.ContainsLine("import { foo } from 'bar'", generator.GetResult());
        }
示例#3
0
        public void Should_extend_class_body_if_api_generation_extensions_provided()
        {
            var mapping          = new TypeMapping();
            var customWriterMock = new CustomApiWriter();
            var generator        = createGenerator(mapping, customWriterMock);

            generator.WriteApiClientClass();

            TextAssert.ContainsLine("public bar() { return foo.bar(); }", generator.GetResult());
        }
示例#4
0
        public void Types_from_custom_type_converter_are_used_for_nested_properties()
        {
            var customConverter = new CustomTypeConverter((typeof(Guid), "MyCustomGuid"));
            var mapping         = new TypeMapping(customConverter);

            mapping.GetTSType(typeof(ModelWithProp));
            var generatedType = mapping.GetGeneratedTypes()[typeof(ModelWithProp)];

            TextAssert.ContainsLine("identifier: MyCustomGuid;", generatedType);
        }
        public void Aborting_request_is_supported()
        {
            var method    = createMethodDescriptor("/func");
            var sb        = new IndentedStringBuilder();
            var generator = createGenerator(method, sb);

            generator.WriteBody(false, false);

            TextAssert.ContainsLine("return request<void>({ url, getAbortFunc, method, jsonResponseExpected });", sb.ToString());
        }
        public void Query_parameters_are_passed_to_request()
        {
            var method    = createMethodDescriptor("/func", ("id", typeof(int)));
            var sb        = new IndentedStringBuilder();
            var generator = createGenerator(method, sb);

            generator.WriteBody(false, false);

            TextAssert.ContainsLine("const queryStringParams = { id };", sb.ToString());
        }
        public void Url_section_parameters_are_inserted_into_placeholders()
        {
            var method    = createMethodDescriptor("/func/{id}", ("id", typeof(int)));
            var sb        = new IndentedStringBuilder();
            var generator = createGenerator(method, sb);

            generator.WriteBody(false, false);

            TextAssert.ContainsLine("const url = `/func/${id}`;", sb.ToString());
        }
        public void Date_parameters_are_converted_to_ISO_string_in_route_sections()
        {
            var method    = createMethodDescriptor("/func/{startDate}", ("startDate", typeof(DateTime)));
            var sb        = new IndentedStringBuilder();
            var generator = createGenerator(method, sb);

            generator.WriteBody(false, false);

            TextAssert.ContainsLine("const url = `/func/${startDate.toISOString()}`;", sb.ToString());
        }
        public void Get_url_method_includes_query_parameters()
        {
            var method    = createMethodDescriptor("/get", ("id", typeof(int)));
            var sb        = new IndentedStringBuilder();
            var generator = createGenerator(method, sb);

            generator.WriteBody(true, false);

            TextAssert.ContainsLine("const queryStringParams = { id };", sb.ToString());
        }
        public void Generic_types_are_mapped_correctly()
        {
            var mapping = new TypeMapping();

            Assert.AreEqual("GenericModel_SimpleModel", mapping.GetTSType(typeof(GenericModel <SimpleModel>)));

            var generatedType = mapping.GetGeneratedTypes()[typeof(GenericModel <SimpleModel>)];

            TextAssert.ContainsLine("export interface GenericModel_SimpleModel {", generatedType);
            TextAssert.ContainsLine("prop: SimpleModel;", generatedType);
        }
        public void Ignored_properties_are_excluded_from_code_generation()
        {
            var mapping = new TypeMapping();

            mapping.AddType(typeof(SimpleModel));

            var generatedType = mapping.GetGeneratedTypes()[typeof(SimpleModel)];

            TextAssert.ContainsLine("prop1: string;", generatedType);
            TextAssert.DoesNotContainLine("prop2: string;", generatedType);
        }
        public void Http_method_is_respected(string httpMethod)
        {
            var method = new ApiMethod(
                "func", "/func", new HttpMethod(httpMethod), new ApiMethodParam[0],
                typeof(void), false, false);
            var sb        = new IndentedStringBuilder();
            var generator = createGenerator(method, sb);

            generator.WriteBody(false, false);

            TextAssert.ContainsLine($"const method = '{httpMethod}';", sb.ToString());
        }
示例#13
0
        public void Enum_imports_are_generated()
        {
            var mapping = new TypeMapping();

            mapping.AddType(typeof(Enum1));
            mapping.AddType(typeof(Enum2));

            var generator = createGenerator(mapping);

            generator.WriteEnumImports("enums");

            TextAssert.ContainsLine("import { Enum1, Enum2 } from './enums'", generator.GetResult());
        }
        public void Upload_progress_callback_is_supported_when_uploading_files()
        {
            var method = new ApiMethod(
                "func", "/upload", HttpMethod.Post,
                new ApiMethodParam[0],
                typeof(void), true, false);
            var sb        = new IndentedStringBuilder();
            var generator = createGenerator(method, sb);

            generator.WriteBody(false, false);

            TextAssert.ContainsLine("return request<void>({ url, requestBody, getAbortFunc, onUploadProgress, method, jsonResponseExpected });", sb.ToString());
        }
        public void Type_substitutions_on_properties_are_processed_correctly()
        {
            var mapping = new TypeMapping();

            mapping.GetTSType(typeof(ModelWithSubstitutedProps));
            var generatedType = mapping.GetGeneratedTypes()[typeof(ModelWithSubstitutedProps)];

            // this is not a mistake - when TSSubstituteType is applied on a property,
            // inline is ignored and always treated as true cause a single property on some type
            // should not affect property type's definition outside of this parent type
            TextAssert.ContainsLine("typedefSeparate: string | number;", generatedType);

            TextAssert.ContainsLine("typedefInline: string | number;", generatedType);
            TextAssert.ContainsLine("noAttribute: SimpleModel;", generatedType);
            TextAssert.ContainsLine("stringSubst: string;", generatedType);
        }
示例#16
0
        public void All_nested_types_are_written_to_module()
        {
            var mapping = new TypeMapping();

            mapping.AddType(typeof(Model));

            var generator = createGenerator(mapping);

            generator.WriteTypeDefinitions();

            var result = generator.GetResult();

            TextAssert.ContainsLine("export interface Model {", result);
            TextAssert.ContainsLine("export interface NestedModel {", result);

            CollectionAssert.AreEquivalent(
                new[] { typeof(Model), typeof(NestedModel) },
                mapping.GetGeneratedTypes().Keys);
            CollectionAssert.AreEquivalent(
                new[] { typeof(Enum1), typeof(Enum2) },
                mapping.GetEnums());
        }