Search in sources :

Example 56 with Transliterator

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

the class CompoundTransliteratorTest method TestTransliterate.

@Test
public void TestTransliterate() {
    logln("Testing the handleTransliterate() API of CompoundTransliterator");
    Transliterator ct1 = null;
    try {
        ct1 = Transliterator.getInstance("Any-Hex;Hex-Any");
    } catch (IllegalArgumentException iae) {
        errln("FAIL: construction using CompoundTransliterator(String ID) failed for " + "Any-Hex;Hex-Any");
        throw iae;
    }
    String s = "abcabc";
    expect(ct1, s, s);
    Transliterator.Position index = new Transliterator.Position();
    ReplaceableString rsource2 = new ReplaceableString(s);
    String expectedResult = s;
    ct1.transliterate(rsource2, index);
    ct1.finishTransliteration(rsource2, index);
    String result = rsource2.toString();
    expectAux(ct1.getID() + ":ReplaceableString, index(0,0,0,0)", s + "->" + rsource2, result.equals(expectedResult), expectedResult);
    Transliterator.Position index2 = new Transliterator.Position(1, 3, 2, 3);
    ReplaceableString rsource3 = new ReplaceableString(s);
    ct1.transliterate(rsource3, index2);
    ct1.finishTransliteration(rsource3, index2);
    result = rsource3.toString();
    expectAux(ct1.getID() + ":String, index2(1,2,2,3)", s + "->" + rsource3, result.equals(expectedResult), expectedResult);
    String[] Data = { // ID, input string, transliterated string
    "Any-Hex;Hex-Any;Any-Hex", "hello", "\\u0068\\u0065\\u006C\\u006C\\u006F", "Any-Hex;Hex-Any", "hello! How are you?", "hello! How are you?", // quotes lost
    "Devanagari-Latin;Latin-Devanagari", // quotes lost
    "\u092D\u0948'\u0930'\u0935", // quotes lost
    "\u092D\u0948\u0930\u0935", "Latin-Cyrillic;Cyrillic-Latin", "a'b'k'd'e'f'g'h'i'j'Shch'shch'zh'h", "a'b'k'd'e'f'g'h'i'j'Shch'shch'zh'h", "Latin-Greek;Greek-Latin", "ABGabgAKLMN", "ABGabgAKLMN", // "Latin-Arabic;Arabic-Latin",               "Ad'r'a'b'i'k'dh'dd'gh", "Adrabikdhddgh",
    "Hiragana-Katakana", "\u3041\u308f\u3099\u306e\u304b\u3092\u3099", "\u30A1\u30f7\u30ce\u30ab\u30fa", "Hiragana-Katakana;Katakana-Hiragana", "\u3041\u308f\u3099\u306e\u304b\u3051", "\u3041\u308f\u3099\u306e\u304b\u3051", "Katakana-Hiragana;Hiragana-Katakana", "\u30A1\u30f7\u30ce\u30f5\u30f6", "\u30A1\u30f7\u30ce\u30ab\u30b1", "Latin-Katakana;Katakana-Latin", "vavivuvevohuzizuzoninunasesuzezu", "vavivuvevohuzizuzoninunasesuzezu" };
    Transliterator ct2 = null;
    for (int i = 0; i < Data.length; i += 3) {
        try {
            ct2 = Transliterator.getInstance(Data[i + 0]);
        } catch (IllegalArgumentException iae2) {
            errln("FAIL: CompoundTransliterator construction failed for " + Data[i + 0]);
            throw iae2;
        }
        expect(ct2, Data[i + 1], Data[i + 2]);
    }
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 57 with Transliterator

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

the class TransliteratorTest method TestParseError.

/**
 * Make sure parse errors reference the right line.
 */
@Test
public void TestParseError() {
    String rule = "a > b;\n" + "# more stuff\n" + "d << b;";
    try {
        Transliterator t = Transliterator.createFromRules("ID", rule, Transliterator.FORWARD);
        if (t != null) {
            errln("FAIL: Did not get expected exception");
        }
    } catch (IllegalArgumentException e) {
        String err = e.getMessage();
        if (err.indexOf("d << b") >= 0) {
            logln("Ok: " + err);
        } else {
            errln("FAIL: " + err);
        }
        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)

Example 58 with Transliterator

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

the class TransliteratorTest method TestCreateInstance.

@Test
public void TestCreateInstance() {
    String FORWARD = "F";
    String REVERSE = "R";
    String[] DATA = { // JB#912
    "Latin-Hangul", // JB#912
    REVERSE, // JB#912
    "Hangul-Latin", // JB#2689: bad compound causes crash
    "InvalidSource-InvalidTarget", FORWARD, "", "InvalidSource-InvalidTarget", REVERSE, "", "Hex-Any;InvalidSource-InvalidTarget", FORWARD, "", "Hex-Any;InvalidSource-InvalidTarget", REVERSE, "", "InvalidSource-InvalidTarget;Hex-Any", FORWARD, "", "InvalidSource-InvalidTarget;Hex-Any", REVERSE, "", null };
    for (int i = 0; DATA[i] != null; i += 3) {
        String id = DATA[i];
        int dir = (DATA[i + 1] == FORWARD) ? Transliterator.FORWARD : Transliterator.REVERSE;
        String expID = DATA[i + 2];
        Exception e = null;
        Transliterator t;
        try {
            t = Transliterator.getInstance(id, dir);
        } catch (Exception e1) {
            e = e1;
            t = null;
        }
        String newID = (t != null) ? t.getID() : "";
        boolean ok = (newID.equals(expID));
        if (t == null) {
            newID = e.getMessage();
        }
        if (ok) {
            logln("Ok: createInstance(" + id + "," + DATA[i + 1] + ") => " + newID);
        } else {
            errln("FAIL: createInstance(" + id + "," + DATA[i + 1] + ") => " + newID + ", expected " + expID);
        }
    }
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 59 with Transliterator

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

the class TransliteratorTest method keyboardAux.

private void keyboardAux(Transliterator t, String[] DATA) {
    Transliterator.Position index = new Transliterator.Position();
    ReplaceableString s = new ReplaceableString();
    for (int i = 0; i < DATA.length; i += 2) {
        StringBuffer log;
        if (DATA[i] != null) {
            log = new StringBuffer(s.toString() + " + " + DATA[i] + " -> ");
            t.transliterate(s, index, DATA[i]);
        } else {
            log = new StringBuffer(s.toString() + " => ");
            t.finishTransliteration(s, index);
        }
        UtilityExtensions.formatInput(log, s, index);
        if (s.toString().equals(DATA[i + 1])) {
            logln(log.toString());
        } else {
            errln("FAIL: " + log.toString() + ", expected " + DATA[i + 1]);
        }
    }
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator)

Example 60 with Transliterator

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

the class TransliteratorTest method TestTitleAccents.

/**
 * Test title case handling of accent (should ignore accents)
 */
@Test
public void TestTitleAccents() {
    Transliterator t = Transliterator.getInstance("Title");
    expect(t, "a\u0300b can't abe", "A\u0300b Can't Abe");
}
Also used : 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