-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Feature/mustache lambda tests #3447
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
Merged
jimschubert
merged 3 commits into
OpenAPITools:master
from
MichalFoksa:feature/mustache-lambda-tests
Aug 11, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...rator/src/test/java/org/openapitools/codegen/templating/mustache/CamelCaseLambdaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| 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 CamelCaseLambdaTest extends LambdaTest { | ||
|
|
||
| @Mock | ||
| CodegenConfig generator; | ||
|
|
||
| @BeforeMethod | ||
| public void setup() { | ||
| MockitoAnnotations.initMocks(this); | ||
| } | ||
|
|
||
| @Test | ||
| public void camelCaseTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("camelcase", new CamelCaseLambda()); | ||
|
|
||
| // When & Then | ||
| test("inputText", "{{#camelcase}}Input-text{{/camelcase}}", ctx); | ||
| } | ||
|
|
||
| @Test | ||
| public void camelCaseReservedWordTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("camelcase", new CamelCaseLambda().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", "{{#camelcase}}reserved-word{{/camelcase}}", ctx); | ||
| } | ||
|
|
||
| @Test | ||
| public void camelCaseEscapeParamTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("camelcase", new CamelCaseLambda() | ||
| .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", "{{#camelcase}}Input_text{{/camelcase}}", ctx); | ||
| } | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
...erator/src/test/java/org/openapitools/codegen/templating/mustache/IndentedLambdaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package org.openapitools.codegen.templating.mustache; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| public class IndentedLambdaTest extends LambdaTest { | ||
|
|
||
| String lineSeparator = System.lineSeparator(); | ||
|
|
||
| @Test | ||
| public void defaultIndentTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("indented", new IndentedLambda()); | ||
| String lineSeparator = System.lineSeparator(); | ||
|
|
||
| // When & Then | ||
| // IndentedLambda applies indentation from second line on of a template. | ||
| test("first line" + lineSeparator + " second line", | ||
| "{{#indented}}first line" + lineSeparator +"second line{{/indented}}", ctx); | ||
| } | ||
|
|
||
| @Test | ||
| public void indentedCountTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("indented", new IndentedLambda(8, " ")); | ||
|
|
||
| // When & Then | ||
| // IndentedLambda applies indentation from second line on of a template. | ||
| test("first line" + lineSeparator + " second line", | ||
| "{{#indented}}first line" + lineSeparator +"second line{{/indented}}", ctx); | ||
| } | ||
|
|
||
|
|
||
| } |
44 changes: 44 additions & 0 deletions
44
...napi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LambdaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package org.openapitools.codegen.templating.mustache; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import com.samskivert.mustache.Mustache; | ||
|
|
||
| /** | ||
| * Simple framework to test Mustache Lambdas. It avoids | ||
| * <pre>compiler->compile->execute->assert</pre> | ||
| * boilerplate code. | ||
| * | ||
| * Inspired by <a href="https://github.com/samskivert/jmustache/blob/master/src/test/java/com/samskivert/mustache/SharedTests.java">Jmustache SharedTests.java</a> | ||
| * | ||
| */ | ||
| public abstract class LambdaTest { | ||
|
|
||
| protected String execute(String template, Object ctx) { | ||
| return execute(Mustache.compiler(), template, ctx); | ||
| } | ||
|
|
||
| protected String execute(Mustache.Compiler compiler, String template, Object ctx) { | ||
| return compiler.compile(template).execute(ctx); | ||
| } | ||
|
|
||
| protected void test(String expected, String template, Map<String, Object> ctx) { | ||
| test(Mustache.compiler(), expected, template, ctx); | ||
| } | ||
|
|
||
| protected void test(Mustache.Compiler compiler, String expected,String template, Map<String, Object> ctx) { | ||
| assertEquals(execute(compiler, template, ctx), expected); | ||
| } | ||
|
|
||
| protected static Map<String, Object> context(Object... data) { | ||
| Map<String, Object> ctx = new HashMap<String, Object>(); | ||
| for (int ii = 0; ii < data.length; ii += 2) { | ||
| ctx.put(data[ii].toString(), data[ii + 1]); | ||
| } | ||
| return ctx; | ||
| } | ||
|
|
||
| } |
49 changes: 49 additions & 0 deletions
49
...rator/src/test/java/org/openapitools/codegen/templating/mustache/LowercaseLambdaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| 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 LowercaseLambdaTest extends LambdaTest { | ||
|
|
||
| @Mock | ||
| CodegenConfig generator; | ||
|
|
||
| @BeforeMethod | ||
| public void setup() { | ||
| MockitoAnnotations.initMocks(this); | ||
| } | ||
|
|
||
| @Test | ||
| public void lowercaseTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("lowercase", new LowercaseLambda()); | ||
|
|
||
| // When & Then | ||
| test("input text", "{{#lowercase}}InPut Text{{/lowercase}}", ctx); | ||
| } | ||
|
|
||
| @Test | ||
| public void lowercaseReservedWordTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("lowercase", new LowercaseLambda().generator(generator)); | ||
|
|
||
| when(generator.sanitizeName(anyString())).then(returnsFirstArg()); | ||
| when(generator.reservedWords()).thenReturn(new HashSet<String>(Arrays.asList("reserved"))); | ||
| when(generator.escapeReservedWord("reserved")).thenReturn("escaped-reserved"); | ||
|
|
||
| // When & Then | ||
| test("escaped-reserved", "{{#lowercase}}rEservEd{{/lowercase}}", ctx); | ||
| } | ||
|
|
||
| } |
42 changes: 42 additions & 0 deletions
42
...erator/src/test/java/org/openapitools/codegen/templating/mustache/OnChangeLambdaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package org.openapitools.codegen.templating.mustache; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.testng.annotations.BeforeMethod; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| public class OnChangeLambdaTest extends LambdaTest { | ||
|
|
||
| private Map<String, Object> ctx; | ||
|
|
||
| // OnChangeLambda holds internal state it is important that context is | ||
| // reinitialize before each test. | ||
| @BeforeMethod | ||
| public void setup() { | ||
| ctx = context("onchange", new OnChangeLambda()); | ||
| } | ||
|
|
||
| @Test | ||
| public void firstValueIsReturnedTest() { | ||
| // Given | ||
|
|
||
| // When & Then | ||
| test("first", "{{#onchange}}first{{/onchange}}", ctx); | ||
| } | ||
|
|
||
| @Test | ||
| public void repeatingValueReturnedOnFirstOccurrenceTest() { | ||
| // Given | ||
|
|
||
| // When & Then | ||
| test("First", "{{#onchange}}First{{/onchange}}", ctx); | ||
| test("", "{{#onchange}}First{{/onchange}}", ctx); | ||
|
|
||
| test("Another", "{{#onchange}}Another{{/onchange}}", ctx); | ||
| test("", "{{#onchange}}Another{{/onchange}}", ctx); | ||
| test("", "{{#onchange}}Another{{/onchange}}", ctx); | ||
|
|
||
| test("First", "{{#onchange}}First{{/onchange}}", ctx); | ||
| } | ||
|
|
||
| } |
28 changes: 28 additions & 0 deletions
28
...rator/src/test/java/org/openapitools/codegen/templating/mustache/TitlecaseLambdaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package org.openapitools.codegen.templating.mustache; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| public class TitlecaseLambdaTest extends LambdaTest { | ||
|
|
||
| @Test | ||
| public void titlecaseTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("titlecase", new TitlecaseLambda()); | ||
|
|
||
| // When & Then | ||
| test("Once Upon A Time", "{{#titlecase}}once upon a time{{/titlecase}}", ctx); | ||
| } | ||
|
|
||
| @Test | ||
| public void titlecaseWithDelimiterTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("titlecase", new TitlecaseLambda("-")); | ||
|
|
||
| // When & Then | ||
| test("Once-Upon-A-Time", "{{#titlecase}}once-upon-a-time{{/titlecase}}", ctx); | ||
| } | ||
|
|
||
|
|
||
| } |
18 changes: 18 additions & 0 deletions
18
...rator/src/test/java/org/openapitools/codegen/templating/mustache/UppercaseLambdaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.openapitools.codegen.templating.mustache; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| public class UppercaseLambdaTest extends LambdaTest { | ||
|
|
||
| @Test | ||
| public void uppercaseTest() { | ||
| // Given | ||
| Map<String, Object> ctx = context("uppercase", new UppercaseLambda()); | ||
|
|
||
| // When & Then | ||
| test("INPUT TEXT", "{{#uppercase}}InPut Text{{/uppercase}}", ctx); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has the detailed message moved elsewhere? If not, we should keep the link to templating docs.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jimschubert
I am sorry, I do not understand the question.
Condition
if (additionalProperties.containsKey("lambda"))should never evaluate to true and if then only because something has changed in DefautlCodegen and that will be caught at first instantiation of DefautlCodegen - in unit tests.I think the construct bellow made sense when it was in a generator derived from DefaultCodegen, but not now.
Makes sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks for the clarification. I incorrectly remembered your earlier change being in DefaultGenerator, and missed that it's actually called in this class constructor.