Home | History | Annotate | Download | only in unittest
      1 package org.unicode.cldr.unittest;
      2 
      3 import java.util.Arrays;
      4 import java.util.Collections;
      5 import java.util.EnumSet;
      6 import java.util.HashSet;
      7 import java.util.Set;
      8 
      9 import org.unicode.cldr.test.CoverageLevel2;
     10 import org.unicode.cldr.util.CLDRConfig;
     11 import org.unicode.cldr.util.CLDRFile;
     12 import org.unicode.cldr.util.CLDRLocale;
     13 import org.unicode.cldr.util.CoreCoverageInfo;
     14 import org.unicode.cldr.util.CoreCoverageInfo.CoreItems;
     15 import org.unicode.cldr.util.Factory;
     16 import org.unicode.cldr.util.LanguageTagParser;
     17 import org.unicode.cldr.util.Level;
     18 import org.unicode.cldr.util.Organization;
     19 import org.unicode.cldr.util.PathHeader;
     20 import org.unicode.cldr.util.StandardCodes;
     21 import org.unicode.cldr.util.SupplementalDataInfo;
     22 
     23 import com.google.common.collect.LinkedHashMultimap;
     24 import com.google.common.collect.Multimap;
     25 
     26 public class TestCoverage extends TestFmwkPlus {
     27 
     28     static final StandardCodes sc = StandardCodes.make();
     29     static final CLDRConfig testInfo = CLDRConfig.getInstance();
     30     static final SupplementalDataInfo sdi = testInfo.getSupplementalDataInfo();
     31 
     32     public static void main(String[] args) {
     33         new TestCoverage().run(args);
     34     }
     35 
     36     static Set<CoreItems> all = Collections.unmodifiableSet(EnumSet
     37         .allOf(CoreItems.class));
     38     static Set<CoreItems> none = Collections.unmodifiableSet(EnumSet
     39         .noneOf(CoreItems.class));
     40 
     41     public void TestBasic() {
     42         CLDRFile engCldrFile = testInfo.getEnglish();
     43         Multimap<CoreItems, String> errors = LinkedHashMultimap.create();
     44         Set<CoreItems> coreCoverage = CoreCoverageInfo.getCoreCoverageInfo(
     45             engCldrFile, errors);
     46         if (!assertEquals("English should be complete", all, coreCoverage)) {
     47             showDiff("Missing", all, coreCoverage);
     48         }
     49         CLDRFile skimpyLocale = testInfo.getCldrFactory().make("asa", false);
     50         errors.clear();
     51         coreCoverage = CoreCoverageInfo.getCoreCoverageInfo(skimpyLocale,
     52             errors);
     53         if (!assertEquals("Skimpy locale should not be complete", none,
     54             coreCoverage)) {
     55             showDiff("Missing", all, coreCoverage);
     56             showDiff("Extra", coreCoverage, none);
     57         }
     58     }
     59 
     60     public void TestSelected() {
     61         Object[][] tests = {
     62             { "en", "//ldml/localeDisplayNames/subdivisions/subdivision[@type=\"gbeng\"]", Level.MODERN, 8 },
     63             { "en", "//ldml/numbers/minimalPairs/ordinalMinimalPairs[@ordinal=\"other\"]", Level.MODERATE, 20 },
     64             { "en", "//ldml/numbers/minimalPairs/pluralMinimalPairs[@count=\"other\"]", Level.MODERATE, 20 },
     65         };
     66         PathHeader.Factory phf = PathHeader.getFactory(testInfo.getEnglish());
     67         for (Object[] test : tests) {
     68             String localeId = (String) test[0];
     69             String path = (String) test[1];
     70             Level expectedLevel = (Level) test[2];
     71             int expectedVotes = (Integer) test[3];
     72             CoverageLevel2 coverageLevel = CoverageLevel2.getInstance(sdi, localeId);
     73             Level level = coverageLevel.getLevel(path);
     74             PathHeader ph = phf.fromPath(path);
     75             assertEquals(localeId + " : " + path + " : ", expectedLevel, level);
     76             CLDRLocale loc = CLDRLocale.getInstance(localeId);
     77             int actualVotes = sdi.getRequiredVotes(loc, ph);
     78             assertEquals(localeId + " : " + path + " : ", expectedVotes, actualVotes);
     79         }
     80     }
     81 
     82     static final boolean DEBUG = false;
     83 
     84     public void TestLocales() {
     85         long start = System.currentTimeMillis();
     86         logln("Status\tLocale\tName\tLevel\tCount" + showColumn(all)
     87             + "\tError Messages");
     88         LanguageTagParser ltp = new LanguageTagParser();
     89         Multimap<CoreItems, String> errors = LinkedHashMultimap.create();
     90         Set<String> toTest = new HashSet(
     91             Arrays.asList("ky mn ms uz az kk pa sr zh lo".split(" ")));
     92         Set<String> defaultContents = sdi.getDefaultContentLocales();
     93 
     94         Factory fullCldrFactory = testInfo.getFullCldrFactory();
     95         for (String locale : fullCldrFactory.getAvailable()) {
     96             if (!ltp.set(locale).getRegion().isEmpty() || locale.equals("root")
     97                 || defaultContents.contains(locale)) {
     98                 continue;
     99             }
    100             Level level = sc.getLocaleCoverageLevel(Organization.cldr, locale);
    101             if (DEBUG && (!toTest.contains(locale) || level != Level.MODERN)) {
    102                 continue;
    103             }
    104             if (locale.equals("am")) {
    105                 int debug = 0;
    106             }
    107 
    108             CLDRFile testFile = fullCldrFactory.make(locale, false);
    109             Set<CoreItems> coreCoverage;
    110             errors.clear();
    111             try {
    112                 coreCoverage = CoreCoverageInfo.getCoreCoverageInfo(testFile,
    113                     errors);
    114             } catch (Exception e) {
    115                 errln("Failure for locale: " + getLocaleAndName(locale));
    116                 e.printStackTrace();
    117                 continue;
    118             }
    119             Set missing = EnumSet.allOf(CoreItems.class);
    120             missing.removeAll(coreCoverage);
    121             if (missing.size() != 0) {
    122                 errln("\t" + getLocaleAndName(locale) + "\t" + level + "\t"
    123                     + missing.size() + showColumn(missing) + "\t" + errors);
    124             } else {
    125                 logln("OK\t" + getLocaleAndName(locale) + "\t" + level + "\t"
    126                     + missing.size());
    127             }
    128         }
    129         long end = System.currentTimeMillis();
    130         logln("Elapsed:\t" + (end - start));
    131     }
    132 
    133     private String getLocaleAndName(String locale) {
    134         return locale + "\t" + testInfo.getEnglish().getName(locale);
    135     }
    136 
    137     private String showColumn(Set items) {
    138         StringBuilder result = new StringBuilder();
    139         for (CoreItems x : CoreItems.values()) {
    140             result.append("\t");
    141             if (items.contains(x)) {
    142                 result.append(x);
    143             }
    144         }
    145         return result.toString();
    146     }
    147 
    148     public void showDiff(String title, Set<CoreItems> all,
    149         Set<CoreItems> coreCoverage) {
    150         Set diff = EnumSet.copyOf(all);
    151         diff.removeAll(coreCoverage);
    152         if (diff.size() != 0) {
    153             errln("\t" + title + ": " + diff);
    154         }
    155     }
    156 }
    157