Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in camelcase lambda, add tests for pascalcase #12639

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CamelCaseLambda escapeAsParamName(final Boolean escape) {

@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String text = camelize(fragment.execute(), lowercaseFirstLetter);
String text = camelize(fragment.execute().replace(" ", "_"), lowercaseFirstLetter);
if (generator != null) {
text = generator.sanitizeName(text);
if (generator.reservedWords().contains(text)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public void camelCaseTest() {
test("inputText", "{{#camelcase}}Input-text{{/camelcase}}", ctx);
}

@Test
public void camelCaseSpaceTest() {
// Given
Map<String, Object> ctx = context("camelcase", new CamelCaseLambda());

// When & Then
test("inputTextApi", "{{#camelcase}}Input text api{{/camelcase}}", ctx);
}

@Test
public void camelCaseReservedWordTest() {
// Given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.openapitools.codegen.templating.mustache;

import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openapitools.codegen.CodegenConfig;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class PascalCaseLambdaTest extends LambdaTest {

@Mock
CodegenConfig generator;

@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}

@Test
public void pascalCaseTest() {
// Given
Map<String, Object> ctx = context("pascalcase", new CamelCaseLambda(false));

// When & Then
test("InputText", "{{#pascalcase}}Input-text{{/pascalcase}}", ctx);
test("InputText", "{{#pascalcase}}Input_text{{/pascalcase}}", ctx);
test("", "{{#pascalcase}}{{/pascalcase}}", ctx);

}

@Test
public void pascalCaseSpaceTest() {
// Given
Map<String, Object> ctx = context("pascalcase", new CamelCaseLambda(false));

// When & Then
test("InputTextApi", "{{#pascalcase}}Input text api{{/pascalcase}}", ctx);
}

@Test
public void pascalCaseReservedWordTest() {
// Given
Map<String, Object> ctx = context("pascalcase", new CamelCaseLambda(false).generator(generator));

when(generator.sanitizeName(anyString())).then(returnsFirstArg());
when(generator.reservedWords()).thenReturn(new HashSet<String>(Arrays.asList("ReservedWord")));
when(generator.escapeReservedWord("ReservedWord")).thenReturn("escapedReservedWord");

// When & Then
test("escapedReservedWord", "{{#pascalcase}}reserved-word{{/pascalcase}}", ctx);
}

@Test
public void pascalCaseEscapeParamTest() {
// Given
Map<String, Object> ctx = context("pascalcase", new CamelCaseLambda(false)
.generator(generator).escapeAsParamName(true));

when(generator.sanitizeName(anyString())).then(returnsFirstArg());
when(generator.reservedWords()).thenReturn(new HashSet<String>());
when(generator.toParamName("InputText")).thenReturn("inputTextAsParam");

// When & Then
test("inputTextAsParam", "{{#pascalcase}}Input_text{{/pascalcase}}", ctx);
}

}