Search in sources :

Example 41 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestCompoundFilterID.

/**
 * Test compound filter ID syntax
 */
@Test
public void TestCompoundFilterID() {
    String[] DATA = { // multiple filters
    "[abc]; [abc]", // multiple filters
    null, // multiple filters
    null, // multiple filters
    null, // misplaced filter
    "Latin-Greek; [abc];", // misplaced filter
    null, // misplaced filter
    null, // misplaced filter
    null, "[b]; Latin-Greek; Upper; ([xyz])", "F", "abc", "a\u0392c", "[b]; (Lower); Latin-Greek; Upper(); ([\u0392])", "R", "\u0391\u0392\u0393", "\u0391b\u0393", "#\n::[b]; ::Latin-Greek; ::Upper; ::([xyz]);", "F", "abc", "a\u0392c", "#\n::[b]; ::(Lower); ::Latin-Greek; ::Upper(); ::([\u0392]);", "R", "\u0391\u0392\u0393", "\u0391b\u0393" };
    for (int i = 0; i < DATA.length; i += 4) {
        String id = DATA[i];
        int direction = (DATA[i + 1] != null && DATA[i + 1].charAt(0) == 'R') ? Transliterator.REVERSE : Transliterator.FORWARD;
        String source = DATA[i + 2];
        String exp = DATA[i + 3];
        boolean expOk = (DATA[i + 1] != null);
        Transliterator t = null;
        IllegalArgumentException e = null;
        try {
            if (id.charAt(0) == '#') {
                t = Transliterator.createFromRules("ID", id, direction);
            } else {
                t = Transliterator.getInstance(id, direction);
            }
        } catch (IllegalArgumentException ee) {
            e = ee;
        }
        boolean ok = (t != null && e == null);
        if (ok == expOk) {
            logln("Ok: " + id + " => " + t + (e != null ? (", " + e.getMessage()) : ""));
            if (source != null) {
                expect(t, source, exp);
            }
        } else {
            errln("FAIL: " + id + " => " + t + (e != null ? (", " + e.getMessage()) : ""));
        }
    }
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 42 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestCoverage.

// ======================================================================
// These tests are not mirrored (yet) in icu4c at
// source/test/intltest/transtst.cpp
// ======================================================================
/**
 * Improve code coverage.
 */
@Test
public void TestCoverage() {
    // NullTransliterator
    Transliterator t = Transliterator.getInstance("Null", Transliterator.FORWARD);
    expect(t, "a", "a");
    // Source, target set
    t = Transliterator.getInstance("Latin-Greek", Transliterator.FORWARD);
    t.setFilter(new UnicodeSet("[A-Z]"));
    logln("source = " + t.getSourceSet());
    logln("target = " + t.getTargetSet());
    t = Transliterator.createFromRules("x", "(.) > &Any-Hex($1);", Transliterator.FORWARD);
    logln("source = " + t.getSourceSet());
    logln("target = " + t.getTargetSet());
}
Also used : UnicodeSet(android.icu.text.UnicodeSet) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 43 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestCasing.

@Test
public void TestCasing() {
    Transliterator toLower = Transliterator.getInstance("lower");
    Transliterator toCasefold = Transliterator.getInstance("casefold");
    Transliterator toUpper = Transliterator.getInstance("upper");
    Transliterator toTitle = Transliterator.getInstance("title");
    for (int i = 0; i < 0x600; ++i) {
        String s = UTF16.valueOf(i);
        String lower = UCharacter.toLowerCase(ULocale.ROOT, s);
        assertEquals("Lowercase", lower, toLower.transform(s));
        String casefold = UCharacter.foldCase(s, true);
        assertEquals("Casefold", casefold, toCasefold.transform(s));
        String title = UCharacter.toTitleCase(ULocale.ROOT, s, null);
        assertEquals("Title", title, toTitle.transform(s));
        String upper = UCharacter.toUpperCase(ULocale.ROOT, s);
        assertEquals("Upper", upper, toUpper.transform(s));
    }
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 44 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestFunction.

@Test
public void TestFunction() {
    // Careful with spacing and ';' here:  Phrase this exactly
    // as toRules() is going to return it.  If toRules() changes
    // with regard to spacing or ';', then adjust this string.
    String rule = "([:Lu:]) > $1 '(' &Lower( $1 ) '=' &Hex( &Any-Lower( $1 ) ) ')';";
    Transliterator t = Transliterator.createFromRules("Test", rule, Transliterator.FORWARD);
    if (t == null) {
        errln("FAIL: createFromRules failed");
        return;
    }
    String r = t.toRules(true);
    if (r.equals(rule)) {
        logln("OK: toRules() => " + r);
    } else {
        errln("FAIL: toRules() => " + r + ", expected " + rule);
    }
    expect(t, "The Quick Brown Fox", "T(t=\\u0074)he Q(q=\\u0071)uick B(b=\\u0062)rown F(f=\\u0066)ox");
    rule = "([^\\ -\\u007F]) > &Hex/Unicode( $1 ) ' ' &Name( $1 ) ;";
    t = Transliterator.createFromRules("Test", rule, Transliterator.FORWARD);
    if (t == null) {
        errln("FAIL: createFromRules failed");
        return;
    }
    r = t.toRules(true);
    if (r.equals(rule)) {
        logln("OK: toRules() => " + r);
    } else {
        errln("FAIL: toRules() => " + r + ", expected " + rule);
    }
    expect(t, "\u0301", "U+0301 \\N{COMBINING ACUTE ACCENT}");
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 45 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestOutputSet.

/**
 * Make sure sets on output are disallowed.
 */
@Test
public void TestOutputSet() {
    String rule = "$set = [a-cm-n]; b > $set;";
    Transliterator t = null;
    try {
        t = Transliterator.createFromRules("ID", rule, Transliterator.FORWARD);
        if (t != null) {
            errln("FAIL: Did not get the expected exception");
        }
    } catch (IllegalArgumentException e) {
        logln("Ok: " + e.getMessage());
        return;
    }
    errln("FAIL: No syntax error");
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Aggregations

Transliterator (android.icu.text.Transliterator)97 Test (org.junit.Test)88 ReplaceableString (android.icu.text.ReplaceableString)66 CaseInsensitiveString (android.icu.util.CaseInsensitiveString)57 UnicodeSet (android.icu.text.UnicodeSet)19 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)5 ULocale (android.icu.util.ULocale)3 Enumeration (java.util.Enumeration)3 UnicodeFilter (android.icu.text.UnicodeFilter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 UnicodeMap (android.icu.dev.util.UnicodeMap)1 CanonicalIterator (android.icu.text.CanonicalIterator)1 Normalizer2 (android.icu.text.Normalizer2)1 Replaceable (android.icu.text.Replaceable)1 BufferedWriter (java.io.BufferedWriter)1 HashSet (java.util.HashSet)1