Skip to content

Commit

Permalink
Adds ExportTypes support.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekspro committed Aug 17, 2018
1 parent bf8f70a commit 9033c62
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 5 deletions.
40 changes: 40 additions & 0 deletions src/NJsonSchema.CodeGeneration.Tests/EnumGenerationTests.cs
Expand Up @@ -50,6 +50,46 @@ public async Task When_string_and_integer_enum_used_then_two_enums_are_generated
Assert.Equal(3, code.Split(new[] { "export enum " }, StringSplitOptions.None).Count()); // two found
}

[Fact]
public async Task When_export_types_is_true_add_export_before_enum_in_typescript()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<StringAndIntegerEnumTestClass>(new JsonSchemaGeneratorSettings());
var data = schema.ToJson();

TypeScriptGeneratorSettings typeScriptGeneratorSettings = new TypeScriptGeneratorSettings()
{
ExportTypes = true
};

//// Act
var generator = new TypeScriptGenerator(schema, typeScriptGeneratorSettings);
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("export enum", code);
}

[Fact]
public async Task When_add_export_keyword_is_false_dont_add_export_before_enum_in_typescript()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<StringAndIntegerEnumTestClass>(new JsonSchemaGeneratorSettings());
var data = schema.ToJson();

TypeScriptGeneratorSettings typeScriptGeneratorSettings = new TypeScriptGeneratorSettings()
{
ExportTypes = false
};

//// Act
var generator = new TypeScriptGenerator(schema, typeScriptGeneratorSettings);
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.DoesNotContain("export enum", code);
}

[Fact]
public async Task When_string_and_integer_enum_used_then_one_enum_is_generated_in_CSharp()
{
Expand Down
Expand Up @@ -227,6 +227,46 @@ public void When_object_property_is_required_or_not_then_the_code_has_correct_in
Assert.Contains("this.b = data[\"B\"] ? B.fromJS(data[\"B\"]) : <any>undefined;", code);
}

[Fact]
public async Task When_export_types_is_true_add_export_before_class_and_interface()
{
var code = await PrepareAsync(new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, ExportTypes = true });

//// Assert
Assert.Contains("export class Student extends Person implements IStudent {", code);
Assert.Contains("export interface IStudent extends IPerson {", code);
Assert.Contains("export interface IPerson {", code);
}

[Fact]
public async Task When_export_types_is_false_dont_add_export_before_class_and_interface()
{
var code = await PrepareAsync(new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, ExportTypes = false });

//// Assert
Assert.DoesNotContain("export class Student extends Person implements IStudent {", code);
Assert.DoesNotContain("export interface IStudent extends IPerson {", code);
Assert.DoesNotContain("export interface IPerson {", code);
}

[Fact]
public async Task When_add_export_keyword_is_true_with_knockout_class_add_export_before_class()
{
var code = await PrepareAsync(new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.KnockoutClass, ExportTypes = true });

//// Assert
Assert.Contains("export class Student extends Person {", code);
}

[Fact]
public async Task When_add_export_keyword_is_false_with_knockout_class_dont_add_export_before_class()
{
var code = await PrepareAsync(new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.KnockoutClass, ExportTypes = false });

//// Assert
Assert.DoesNotContain("export class Student extends Person {", code);
}

[Fact]
public async Task When_class_is_generated_then_constructor_interfaces_are_correctly_generated()
{
Expand Down
Expand Up @@ -128,6 +128,9 @@ public string IndexerPropertyValueType
/// <summary>Gets a value indicating whether </summary>
public bool RequiresStrictPropertyInitialization => _settings.TypeScriptVersion >= 2.7m;

/// <summary>Gets a value indicating whether the export keyword should be added to all classes.</summary>
public bool ExportTypes => _settings.ExportTypes;

/// <summary>Gets the inherited schema.</summary>
private JsonSchema4 InheritedSchema => _schema.InheritedSchema?.ActualSchema;
}
Expand Down
Expand Up @@ -38,6 +38,9 @@ public EnumTemplateModel(string typeName, JsonSchema4 schema, TypeScriptGenerato
/// <summary>Gets the description.</summary>
public string Description => ConversionUtilities.RemoveLineBreaks(_schema.Description);

/// <summary>Gets a value indicating whether the export keyword should be added to all enums.</summary>
public bool ExportTypes => _settings.ExportTypes;

/// <summary>Gets the enum values.</summary>
public List<EnumerationItemModel> Enums
{
Expand Down
@@ -1,7 +1,7 @@
{% if HasDescription -%}
/** {{ Description }} */
{% endif -%}
export {% if IsAbstract %}abstract {% endif %}class {{ ClassName }}{{ Inheritance }} {
{% if ExportTypes -%}export {% endif -%}{% if IsAbstract %}abstract {% endif %}class {{ ClassName }}{{ Inheritance }} {
{% for property in Properties -%}
{% if property.HasDescription -%}
/** {{ property.Description }} */
Expand Down Expand Up @@ -168,7 +168,7 @@ export {% if IsAbstract %}abstract {% endif %}class {{ ClassName }}{{ Inheritanc
{% if HasDescription -%}
/** {{ Description }} */
{% endif -%}
export interface I{{ ClassName }}{{ InterfaceInheritance }} {
{% if ExportTypes -%}export {% endif -%}interface I{{ ClassName }}{{ InterfaceInheritance }} {
{% for property in Properties -%}
{% if property.HasDescription -%}
/** {{ property.Description }} */
Expand Down
@@ -1,7 +1,7 @@
{% if HasDescription -%}
/** {{ Description }} */
{% endif -%}
export enum {{ Name }} {
{% if ExportTypes -%}export {% endif -%}enum {{ Name }} {
{% for enumeration in Enums -%}
{{ enumeration.Name }} = {{ enumeration.Value }},
{% endfor -%}
Expand Down
@@ -1,7 +1,7 @@
{% if HasDescription -%}
/** {{ Description }} */
{% endif -%}
export interface {{ ClassName }}{{ Inheritance }} {
{% if ExportTypes -%}export {% endif -%}interface {{ ClassName }}{{ Inheritance }} {
{% for property in Properties -%}
{% if property.HasDescription -%}
/** {{ property.Description }} */
Expand Down
@@ -1,7 +1,7 @@
{% if HasDescription -%}
/** {{ Description }} */
{% endif -%}
export class {{ ClassName }}{{ Inheritance }} {
{% if ExportTypes -%}export {% endif -%}class {{ ClassName }}{{ Inheritance }} {
{% for property in Properties -%}
{% if property.HasDescription -%}
/** {{ property.Description }} */
Expand Down
Expand Up @@ -27,6 +27,7 @@ public TypeScriptGeneratorSettings()
TypeScriptVersion = 1.8m;
GenerateConstructorInterface = true;
ConvertConstructorInterfaceData = false;
ExportTypes = true;

ValueGenerator = new TypeScriptValueGenerator(this);
PropertyNameGenerator = new TypeScriptPropertyNameGenerator();
Expand Down Expand Up @@ -84,6 +85,9 @@ public TypeScriptGeneratorSettings()
/// <summary>Gets or sets a value indicating whether POJO objects in the constructor data are converted to DTO instances (GenerateConstructorInterface must be enabled, default: false).</summary>
public bool ConvertConstructorInterfaceData { get; set; }

/// <summary>Gets or sets a value indicating whether the export keyword should be added to all classes and enums (default: true).</summary>
public bool ExportTypes { get; set; }

internal ITemplate CreateTemplate(string typeName, object model)
{
if (ClassTypes != null && ClassTypes.Contains(typeName))
Expand Down

0 comments on commit 9033c62

Please sign in to comment.