mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Avoid adding unnecessary space and improve unit test
This commit is contained in:
parent
7d9de4b0d2
commit
b869eb8d1d
2 changed files with 38 additions and 22 deletions
|
|
@ -27,7 +27,7 @@ namespace Flow.Launcher.Infrastructure
|
|||
{
|
||||
switch (e.PropertyName)
|
||||
{
|
||||
case nameof (Settings.ShouldUsePinyin):
|
||||
case nameof(Settings.ShouldUsePinyin):
|
||||
if (_settings.ShouldUsePinyin)
|
||||
{
|
||||
Reload();
|
||||
|
|
@ -52,7 +52,7 @@ namespace Flow.Launcher.Infrastructure
|
|||
|
||||
private void CreateDoublePinyinTableFromStream(Stream jsonStream)
|
||||
{
|
||||
var table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonStream) ??
|
||||
var table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonStream) ??
|
||||
throw new InvalidOperationException("Failed to deserialize double pinyin table: result is null");
|
||||
|
||||
var schemaKey = _settings.DoublePinyinSchema.ToString();
|
||||
|
|
@ -128,12 +128,12 @@ namespace Flow.Launcher.Infrastructure
|
|||
if (IsChineseCharacter(content[i]))
|
||||
{
|
||||
var translated = _settings.UseDoublePinyin ? ToDoublePinyin(resultList[i]) : resultList[i];
|
||||
|
||||
if (i > 0)
|
||||
|
||||
if (i > 0 && content[i - 1] != ' ')
|
||||
{
|
||||
resultBuilder.Append(' ');
|
||||
}
|
||||
|
||||
|
||||
map.AddNewIndex(resultBuilder.Length, translated.Length);
|
||||
resultBuilder.Append(translated);
|
||||
previousIsChinese = true;
|
||||
|
|
@ -144,11 +144,14 @@ namespace Flow.Launcher.Infrastructure
|
|||
if (previousIsChinese)
|
||||
{
|
||||
previousIsChinese = false;
|
||||
resultBuilder.Append(' ');
|
||||
if (content[i] != ' ')
|
||||
{
|
||||
resultBuilder.Append(' ');
|
||||
}
|
||||
}
|
||||
|
||||
map.AddNewIndex(resultBuilder.Length, resultList[i].Length);
|
||||
resultBuilder.Append(resultList[i]);
|
||||
|
||||
map.AddNewIndex(resultBuilder.Length, 1);
|
||||
resultBuilder.Append(content[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -156,7 +159,7 @@ namespace Flow.Launcher.Infrastructure
|
|||
|
||||
var translation = resultBuilder.ToString();
|
||||
var result = (translation, map);
|
||||
|
||||
|
||||
return _pinyinCache[content] = result;
|
||||
}
|
||||
|
||||
|
|
@ -185,8 +188,8 @@ namespace Flow.Launcher.Infrastructure
|
|||
|
||||
private string ToDoublePinyin(string fullPinyin)
|
||||
{
|
||||
return currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue)
|
||||
? doublePinyinValue
|
||||
return currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue)
|
||||
? doublePinyinValue
|
||||
: fullPinyin;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,19 +22,32 @@ namespace Flow.Launcher.Test
|
|||
ClassicAssert.AreEqual(10, GetOriginalToTranslatedAt(mapping, 1));
|
||||
}
|
||||
|
||||
[TestCase(0, 0)]
|
||||
[TestCase(2, 1)]
|
||||
[TestCase(3, 1)]
|
||||
[TestCase(5, 2)]
|
||||
[TestCase(6, 2)]
|
||||
|
||||
[TestCase(0, 0)] // "F" -> "F"
|
||||
[TestCase(1, 1)] // "l" -> "l"
|
||||
[TestCase(2, 2)] // "o" -> "o"
|
||||
[TestCase(3, 3)] // "w" -> "w"
|
||||
[TestCase(4, 4)] // " " -> " "
|
||||
[TestCase(5, 5)] // "Y" (translated from "用") -> original index 5
|
||||
[TestCase(6, 5)] // "o" (translated from "用") -> original index 5
|
||||
[TestCase(7, 5)] // "n" (translated from "用") -> original index 5
|
||||
[TestCase(8, 5)] // "g" (translated from "用") -> original index 5
|
||||
[TestCase(10, 6)] // "H" (translated from "户") -> original index 6
|
||||
public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, int expectedOriginalIndex)
|
||||
{
|
||||
var mapping = new TranslationMapping();
|
||||
// a测试
|
||||
// a Ce Shi
|
||||
mapping.AddNewIndex(0, 1);
|
||||
mapping.AddNewIndex(2, 2);
|
||||
mapping.AddNewIndex(5, 3);
|
||||
// Test case :
|
||||
// 0123456
|
||||
// Flow 用户
|
||||
// 0123456789012
|
||||
// Flow Yong Hu"
|
||||
mapping.AddNewIndex(0, 1); // F
|
||||
mapping.AddNewIndex(1, 1); // l
|
||||
mapping.AddNewIndex(2, 1); // o
|
||||
mapping.AddNewIndex(3, 1); // w
|
||||
mapping.AddNewIndex(4, 1); // ' '
|
||||
mapping.AddNewIndex(5, 4); // 用 -> Yong
|
||||
mapping.AddNewIndex(11, 2); // 户 -> Hu
|
||||
|
||||
var result = mapping.MapToOriginalIndex(translatedIndex);
|
||||
ClassicAssert.AreEqual(expectedOriginalIndex, result);
|
||||
|
|
|
|||
Loading…
Reference in a new issue