Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Ciocanu committed Dec 16, 2023
1 parent 43c90a0 commit 97ff27b
Show file tree
Hide file tree
Showing 8 changed files with 927 additions and 15 deletions.
51 changes: 46 additions & 5 deletions src/generators/scala/ScalaRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AbstractRenderer } from '../AbstractRenderer';
import { ScalaGenerator, ScalaOptions } from './ScalaGenerator';
import { ConstrainedMetaModel, InputMetaModel, Preset } from '../../models';
import { FormatHelpers } from '../../helpers';
import { ScalaDependencyManager } from './ScalaDependencyManager';

/**
Expand All @@ -10,11 +11,7 @@ import { ScalaDependencyManager } from './ScalaDependencyManager';
*/
export abstract class ScalaRenderer<
RendererModelType extends ConstrainedMetaModel
> extends AbstractRenderer<
ScalaOptions,
ScalaGenerator,
RendererModelType
> {
> extends AbstractRenderer<ScalaOptions, ScalaGenerator, RendererModelType> {
constructor(
options: ScalaOptions,
generator: ScalaGenerator,
Expand All @@ -25,4 +22,48 @@ export abstract class ScalaRenderer<
) {
super(options, generator, presets, model, inputModel);
}

renderComments(lines: string | string[]): string {
lines = FormatHelpers.breakLines(lines);
const newLiteral = lines.map((line) => ` * ${line}`).join('\n');
return `/**
${newLiteral}
*/`;
}

renderAnnotation(
annotationName: string,
value?: any | Record<string, any>
): string {
const name = `@${annotationName}`;

if (value === undefined || value === null) {
return name;
}

if (typeof value !== 'object') {
return `${name}(${value})`;
}

const entries = Object.entries(value || {});

if (entries.length === 0) {
return name;
}

const values = concatenateEntries(entries);
return `${name}(${values})`;
}
}

function concatenateEntries(entries: [string, unknown][] = []): string {
return entries
.map(([paramName, newValue]) => {
if (paramName && newValue !== undefined) {
return `${paramName}=${newValue}`;
}
return newValue;
})
.filter((v) => v !== undefined)
.join(', ');
}
58 changes: 48 additions & 10 deletions src/generators/scala/presets/DescriptionPreset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
import { ScalaRenderer } from '../ScalaRenderer';
import { ScalaPreset } from '../ScalaPreset';
import { FormatHelpers } from '../../../helpers';
import { ConstrainedEnumModel, ConstrainedObjectModel } from '../../../models';
function renderDescription({
renderer,
content,
item
}: {
renderer: ScalaRenderer<any>;
content: string;
item: ConstrainedObjectModel | ConstrainedEnumModel;
}): string {
if (!item.originalInput['description']) {
return content;
}

let comment = `${item.originalInput['description']}`;

if (item instanceof ConstrainedObjectModel) {
const properties = Object.keys(item.properties)
.map((key) => item.properties[`${key}`])
.map((model) => {
const property = `@property ${model.propertyName}`;
const desc = model.property.originalInput['description'];

return desc !== undefined ? `${property} ${desc}` : property;
})
.join('\n');

comment += `\n\n${properties}`;
}

const examples = Array.isArray(item.originalInput['examples'])
? `Examples: \n${FormatHelpers.renderJSONExamples(
item.originalInput['examples']
)}`
: null;

if (examples !== null) {
comment += `\n\n${examples}`;
}

return `${renderer.renderComments(comment)}\n${content}`;
}

/**
* Preset which adds description to rendered model.
Expand All @@ -7,19 +51,13 @@ import { ScalaPreset } from '../ScalaPreset';
*/
export const SCALA_DESCRIPTION_PRESET: ScalaPreset = {
class: {
self({ content }) {
const renderedDesc = 'my description';
return `${renderedDesc}\n${content}`;
},
getter({ content }) {
const renderedDesc = 'my description';
return `${renderedDesc}\n${content}`;
self({ renderer, model, content }) {
return renderDescription({ renderer, content, item: model });
}
},
enum: {
self({ content }) {
const renderedDesc = 'my description';
return `${renderedDesc}\n${content}`;
self({ renderer, model, content }) {
return renderDescription({ renderer, content, item: model });
}
}
};
2 changes: 2 additions & 0 deletions test/TestUtils/TestRenderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { RustRenderer } from '../../src/generators/rust/RustRenderer';
import { PythonRenderer } from '../../src/generators/python/PythonRenderer';
import { KotlinRenderer } from '../../src/generators/kotlin/KotlinRenderer';
import { PhpRenderer } from '../../src/generators/php/PhpRenderer';
import { ScalaRenderer } from '../../src/generators/scala/ScalaRenderer';

export class TestRenderer extends AbstractRenderer {
constructor(presets = []) {
Expand Down Expand Up @@ -43,3 +44,4 @@ export class MockRustRenderer extends RustRenderer<any> {}
export class MockPythonRenderer extends PythonRenderer<any> {}
export class MockKotlinRenderer extends KotlinRenderer<any> {}
export class MockPhpRenderer extends PhpRenderer<any> {}
export class MockScalaRenderer extends ScalaRenderer<any> {}
12 changes: 12 additions & 0 deletions test/generators/scala/Constants.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { isReservedScalaKeyword } from '../../../src/generators/scala/Constants';

describe('Reserved keywords', () => {
it('should return true if the word is a reserved keyword', () => {
expect(isReservedScalaKeyword('abstract')).toBe(true);
expect(isReservedScalaKeyword('type')).toBe(true);
});

it('should return false if the word is not a reserved keyword', () => {
expect(isReservedScalaKeyword('dinosaur')).toBe(false);
});
});
Loading

0 comments on commit 97ff27b

Please sign in to comment.