-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an example from discussion 155 on the mustache spec
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
compiler/src/test/java/com/github/mustachejava/SpecDisc155Test.java
This file contains 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,64 @@ | ||
package com.github.mustachejava; | ||
|
||
import com.github.mustachejavabenchmarks.NullWriter; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class SpecDisc155Test { | ||
|
||
static class Friend { | ||
String name = "Sam"; | ||
} | ||
|
||
static class I18N { | ||
String translationKey; | ||
Function<String, String> key = content -> { | ||
translationKey = content; | ||
return ""; | ||
}; | ||
String defaultText; | ||
TemplateFunction t = content -> { | ||
defaultText = content; | ||
return ""; | ||
}; | ||
|
||
Map<String, String> translations; | ||
|
||
{ | ||
translations = new HashMap<>(); | ||
translations.put("goodmorning", "좋은 아침이에요 {{name}}"); | ||
} | ||
|
||
MustacheFactory mf = new DefaultMustacheFactory(); | ||
|
||
TemplateFunction i18n = content -> { | ||
try { | ||
mf.compile(new StringReader(content), "i18n").execute(new NullWriter(), this).flush(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
if (translations.containsKey(translationKey)) { | ||
return translations.get(translationKey); | ||
} else { | ||
return defaultText; | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void testDisc155() throws IOException { | ||
MustacheFactory mf = new DefaultMustacheFactory(); | ||
Mustache m = mf.compile(new StringReader("{{#friend}}{{#i18n}}{{#key}}goodmorning{{/key}}{{#t}}Good morning, {{name}}{{/t}}{{/i18n}}{{/friend}}"), "test"); | ||
StringWriter sw = new StringWriter(); | ||
m.execute(sw, new Object[] {new I18N(), new Object() { public Friend friend = new Friend();}}).flush(); | ||
assertEquals("좋은 아침이에요 Sam", sw.toString()); | ||
} | ||
} |