1 package org.unicode.cldr.test; 2 3 import static org.junit.jupiter.api.Assertions.assertNotNull; 4 import static org.junit.jupiter.api.Assertions.assertTrue; 5 6 import java.util.LinkedList; 7 import java.util.List; 8 9 import org.junit.jupiter.api.Test; 10 import org.unicode.cldr.util.CLDRConfig; 11 import org.unicode.cldr.util.CLDRFile; 12 import org.unicode.cldr.util.SimpleXMLSource; 13 import org.unicode.cldr.util.XMLSource; 14 import org.unicode.cldr.util.XMLSource.ResolvingSource; 15 16 public class TestExampleGenerator { 17 18 @Test testPersonNamesGwen()19 public void testPersonNamesGwen() { 20 final String loc = "es"; 21 final String X_GIVEN = "//ldml/personNames/sampleName[@item=\"givenSurnameOnly\"]/nameField[@type=\"given\"]"; 22 final String X_SURNAME = "//ldml/personNames/sampleName[@item=\"givenSurnameOnly\"]/nameField[@type=\"surname\"]"; 23 final String X_PATTERN = "//ldml/personNames/personName[@order=\"sorting\"][@length=\"long\"][@usage=\"addressing\"][@formality=\"formal\"]/namePattern"; 24 25 final CLDRFile english = CLDRConfig.getInstance().getEnglish(); 26 final XMLSource source = new SimpleXMLSource(loc); 27 // add a bunch of English stuff 28 for (final String x : english.fullIterable() ) { 29 if (!x.startsWith("//ldml/personNames")) continue; 30 source.add(english.getFullXPath(x), english.getStringValue(x)); 31 } 32 source.add(X_PATTERN, "{surname}, {given}"); 33 source.add(X_GIVEN, "Fred"); 34 source.add(X_SURNAME, "Person"); 35 36 final XMLSource root = new SimpleXMLSource("root"); 37 final List<XMLSource> sourceList = new LinkedList<>(); 38 sourceList.add(source); 39 sourceList.add(root); 40 final ResolvingSource rs = new ResolvingSource(sourceList); 41 final CLDRFile afile = new CLDRFile(rs); 42 ExampleGenerator eg = new ExampleGenerator(afile, english, english.getSupplementalDirectory().getAbsolutePath()); 43 assertNotNull(eg); 44 45 46 final String html1 = eg.getExampleHtml(X_PATTERN, source.getValueAtDPath(X_PATTERN)); 47 // html chunk… 48 assertTrue(html1.contains(">Person, Fred<"), () -> "Expected '>Person, Fred<' in the morass of " + html1); 49 source.add(X_GIVEN, "Gwen"); 50 eg.updateCache(X_GIVEN); // Notify the ExampleGenerator that there's a change 51 52 final String html2 = eg.getExampleHtml(X_PATTERN, source.getValueAtDPath(X_PATTERN)); 53 // html chunk… 54 assertTrue(html2.contains(">Person, Gwen<"), () -> "Expected '>Person, Gwen<' in the morass of " + html2); 55 } 56 } 57