Pinyin { public static String toPinyin(String str, String separator) { return Engine.toPinyin(str, mTrieDict, mPinyinDicts, separator, mSelector); } private  Pinyin(); static void init(Config config); static void add(PinyinDict dict); static Config newConfig(); static String toPinyin(String str, String separator); static String toPinyin(String str, String separator, PinyinRules rules); static String toPinyin(char c); static String toPinyin(char c, PinyinRules rules); static boolean isChinese(char c);  }
@Test public void testToPinyin_Str_no_chinese_str() { String str = "hello"; String expected = "h,e,l,l,o"; assertThat(Pinyin.toPinyin(str, ","), is(expected)); }
@Test public void testToPinyin_Str_no_dict() { String str = "一个测试重庆test,中英文符号；;《>。"; String expected = "YI GE CE SHI ZHONG QING t e s t , ZHONG YING WEN FU HAO ； ; 《 > 。"; assertThat(Pinyin.toPinyin(str, " "), is(expected)); }
@Test public void testToPinyin_custom_rules_1() throws BadHanyuPinyinOutputFormatCombination { assertThat(Pinyin.toPinyin('嗯'), is("NG")); assertThat(Pinyin.toPinyin('嗯', new PinyinRules().add('嗯', "EN")), is("EN")); }
@Test public void testToPinyin_custom_rules_2() throws BadHanyuPinyinOutputFormatCombination { assertThat(Pinyin.toPinyin("长春", ""), is("ZHANGCHUN")); assertThat(Pinyin.toPinyin("长春", "", new PinyinRules().add("长春", "CHANGCHUN")), is("CHANGCHUN")); }
@Test public void testToPinyin_char() throws BadHanyuPinyinOutputFormatCombination { char[] allChars = allChars(); final int allCharsLength = allChars.length; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setCaseType(HanyuPinyinCaseType.UPPERCASE); format.setVCharType(HanyuPinyinVCharType.WITH_V); int chineseCount = 0; for (int i = 0; i < allCharsLength; i++) { char targetChar = allChars[i]; String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(targetChar, format); if (pinyins != null && pinyins.length > 0) { chineseCount++; assertThat(Pinyin.toPinyin(targetChar), equalTo(pinyins[0])); } else { assertThat(Pinyin.toPinyin(targetChar), equalTo(String.valueOf(targetChar))); } } int expectedChineseCount = 20378; assertThat(chineseCount, is(expectedChineseCount)); }
@Test public void testToPinyin_Str_null_str() { String str = null; assertThat(Pinyin.toPinyin(str, ","), nullValue()); }
@Test public void testToPinyin_Str_empty_str() { String str = ""; assertThat(Pinyin.toPinyin(str, ","), is("")); }