Skip to content

Commit 6e6ec6f

Browse files
authored
fix issue trying to remove a writing system (#1713)
* write test for removing a writing system for both Rich and normal MultiStrings
1 parent fde02a2 commit 6e6ec6f

4 files changed

Lines changed: 76 additions & 23 deletions

File tree

backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateDictionaryProxy.cs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,32 @@ public bool ContainsKey(WritingSystemId key)
3434

3535
public void Add(object key, object? value)
3636
{
37-
var valStr = value as string ?? throw new ArgumentException("unable to convert value to string", nameof(value));
38-
if (key is WritingSystemId keyWs)
37+
WritingSystemId keyWs;
38+
if (key is WritingSystemId typed)
3939
{
40-
Add(keyWs, valStr);
40+
keyWs = typed;
4141
}
4242
else if (key is string keyStr)
4343
{
44-
Add(keyStr, valStr);
44+
keyWs = keyStr;
4545
}
4646
else
4747
{
4848
throw new ArgumentException("unable to convert key to writing system id", nameof(key));
4949
}
50+
51+
if (value is string valueStr)
52+
{
53+
Add(keyWs, valueStr);
54+
}
55+
else if (value is RichString valueRich)
56+
{
57+
Add(keyWs, valueRich);
58+
}
59+
else
60+
{
61+
throw new ArgumentException("unable to convert value to string", nameof(value));
62+
}
5063
}
5164

5265
public bool Contains(object key)
@@ -105,6 +118,26 @@ IDictionaryEnumerator IDictionary.GetEnumerator()
105118

106119
public void Remove(object key)
107120
{
121+
if (key is WritingSystemId keyWs)
122+
{
123+
Remove(keyWs);
124+
}
125+
else if (key is string keyStr)
126+
{
127+
Remove(keyStr);
128+
}
129+
else
130+
{
131+
throw new ArgumentException($"unable to convert key {key} to writing system id", nameof(key));
132+
}
133+
}
134+
135+
public bool Remove(WritingSystemId key)
136+
{
137+
var containedKey = ContainsKey(key);
138+
var writingSystemHandle = lexboxLcmApi.GetWritingSystemHandle(key);
139+
multiString.set_String(writingSystemHandle, null);
140+
return containedKey;
108141
}
109142

110143
public bool IsFixedSize => false;
@@ -151,10 +184,6 @@ public void CopyTo(Array array, int index)
151184

152185
public bool IsReadOnly => false;
153186

154-
public bool Remove(WritingSystemId key)
155-
{
156-
throw new NotSupportedException();
157-
}
158187

159188
public bool TryGetValue(WritingSystemId key, out string value)
160189
{

backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,17 @@ public class UpdateRichMultiStringProxy(ITsMultiString multiString, FwDataMiniLc
8888

8989
void IDictionary.Add(object key, object? value)
9090
{
91-
var valStr = value as RichString ??
92-
throw new ArgumentException($"unable to convert value {value?.GetType().Name ?? "null"} to RichString",
93-
nameof(value));
94-
if (key is WritingSystemId keyWs)
95-
{
96-
proxy.Add(keyWs, valStr);
97-
}
98-
else if (key is string keyStr)
99-
{
100-
proxy.Add(keyStr, valStr);
101-
}
102-
else
103-
{
104-
throw new ArgumentException("unable to convert key to writing system id", nameof(key));
105-
}
91+
((IDictionary)proxy).Add(key, value);
92+
}
93+
94+
void IDictionary.Remove(object key)
95+
{
96+
((IDictionary)proxy).Remove(key);
97+
}
98+
99+
object? IDictionary.this[object key]
100+
{
101+
get => ((IDictionary)proxy)[key];
102+
set => ((IDictionary)proxy)[key] = value;
106103
}
107104
}

backend/FwLite/MiniLcm.Tests/UpdateEntryTestsBase.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ public async Task UpdateEntry_Works()
5959
updatedEntry.Should().BeEquivalentTo(entry, options => options);
6060
}
6161

62+
[Fact]
63+
public async Task UpdateEntry_SupportsRemovingAMultiStringWs()
64+
{
65+
var entry = await Api.GetEntry(Entry1Id);
66+
ArgumentNullException.ThrowIfNull(entry);
67+
var before = entry.Copy();
68+
entry.CitationForm.Remove("en");
69+
var updatedEntry = await Api.UpdateEntry(before, entry);
70+
updatedEntry.CitationForm.Values.Should().NotContainKey("en");
71+
}
72+
73+
[Fact]
74+
public async Task UpdateEntry_SupportsRemovingARichMultiStringWs()
75+
{
76+
var entry = await Api.GetEntry(Entry1Id);
77+
ArgumentNullException.ThrowIfNull(entry);
78+
var before = entry.Copy();
79+
entry.Note.Remove("en");
80+
var updatedEntry = await Api.UpdateEntry(before, entry);
81+
updatedEntry.Note.Should().NotContainKey("en");
82+
}
83+
6284
[Fact]
6385
public async Task UpdateEntry_SupportsSenseChanges()
6486
{

backend/FwLite/MiniLcm/Models/MultiString.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public void Add(string key, string value)
9595
Values.Add(key, value);
9696
}
9797

98+
public bool Remove(WritingSystemId key)
99+
{
100+
return Values.Remove(key);
101+
}
102+
98103
void IDictionary.Add(object key, object? value)
99104
{
100105
((IDictionary)Values).Add(key, value);

0 commit comments

Comments
 (0)