-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathConfiguredXHTMLGeneratorTestHelpers.cs
More file actions
740 lines (668 loc) · 29.3 KB
/
Copy pathConfiguredXHTMLGeneratorTestHelpers.cs
File metadata and controls
740 lines (668 loc) · 29.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// Copyright (c) 2014-2023 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using SIL.LCModel.Core.Text;
using SIL.LCModel.Core.WritingSystems;
using SIL.FieldWorks.Common.Controls;
using SIL.LCModel.Core.KernelInterfaces;
using SIL.LCModel;
using SIL.LCModel.DomainServices;
using SIL.LCModel.Utils;
using XCore;
using SIL.FieldWorks.Common.FwUtils;
namespace SIL.FieldWorks.XWorks
{
public partial class ConfiguredXHTMLGeneratorTests
{
private static void DeleteTempXhtmlAndCssFiles(string xhtmlPath)
{
if (string.IsNullOrEmpty(xhtmlPath))
return;
File.Delete(xhtmlPath);
File.Delete(Path.ChangeExtension(xhtmlPath, "css"));
var xhtmlDir = Path.GetDirectoryName(xhtmlPath);
if (string.IsNullOrEmpty(xhtmlDir))
return;
File.Delete(Path.Combine(xhtmlDir, "ProjectDictionaryOverrides.css"));
File.Delete(Path.Combine(xhtmlDir, "ProjectReversalOverrides.css"));
}
/// <summary>
/// Creates a DictionaryConfigurationModel with one Main and one of each neeeded Minor Entry nodes, all with enabled HeadWord children
/// </summary>
internal static DictionaryConfigurationModel CreateInterestingConfigurationModel(LcmCache cache, PropertyTable propertyTable = null,
DictionaryConfigurationModel.ConfigType configType = DictionaryConfigurationModel.ConfigType.Root)
{
var mainHeadwordNode = new ConfigurableDictionaryNode
{
FieldDescription = "HeadWord",
CSSClassNameOverride = "entry",
DictionaryNodeOptions = GetWsOptionsForLanguages(new[] { "fr" }),
Before = "MainEntry: ",
};
var subEntryNode = new ConfigurableDictionaryNode
{
Children = new List<ConfigurableDictionaryNode> { mainHeadwordNode },
FieldDescription = "Subentries"
};
var mainEntryNode = new ConfigurableDictionaryNode
{
Children = new List<ConfigurableDictionaryNode> { mainHeadwordNode },
FieldDescription = "LexEntry"
};
if (configType == DictionaryConfigurationModel.ConfigType.Hybrid || configType == DictionaryConfigurationModel.ConfigType.Root)
mainEntryNode.Children.Add(subEntryNode);
if (configType == DictionaryConfigurationModel.ConfigType.Hybrid || configType == DictionaryConfigurationModel.ConfigType.Lexeme)
mainEntryNode.DictionaryNodeOptions = GetFullyEnabledListOptions(cache, DictionaryNodeListOptions.ListIds.Complex);
CssGeneratorTests.PopulateFieldsForTesting(mainEntryNode);
var minorEntryNode = mainEntryNode.DeepCloneUnderSameParent();
minorEntryNode.CSSClassNameOverride = "minorentry";
minorEntryNode.Before = "MinorEntry: ";
minorEntryNode.DictionaryNodeOptions = GetFullyEnabledListOptions(cache, DictionaryNodeListOptions.ListIds.Complex);
var minorSecondNode = minorEntryNode.DeepCloneUnderSameParent();
minorSecondNode.Before = "HalfStep: ";
minorSecondNode.DictionaryNodeOptions = GetFullyEnabledListOptions(cache, DictionaryNodeListOptions.ListIds.Variant);
var model = new DictionaryConfigurationModel
{
AllPublications = true,
Parts = new List<ConfigurableDictionaryNode> { mainEntryNode, minorEntryNode, minorSecondNode },
FilePath = propertyTable == null ? null : Path.Combine(DictionaryConfigurationListener.GetProjectConfigurationDirectory(propertyTable),
"filename" + DictionaryConfigurationModel.FileExtension),
IsRootBased = configType == DictionaryConfigurationModel.ConfigType.Root
};
if (configType != DictionaryConfigurationModel.ConfigType.Root)
model.Parts.Remove(minorEntryNode);
return model;
}
internal static ConfigurableDictionaryNode CreatePictureModel()
{
var thumbNailNode = new ConfigurableDictionaryNode
{
FieldDescription = "PictureFileRA",
CSSClassNameOverride = "picture"
};
var pictureNode = new ConfigurableDictionaryNode
{
FieldDescription = "PicturesOfSenses",
CSSClassNameOverride = "Pictures",
Children = new List<ConfigurableDictionaryNode> { thumbNailNode }
};
var sensesNode = new ConfigurableDictionaryNode { FieldDescription = "Senses" };
var mainEntryNode = new ConfigurableDictionaryNode
{
Children = new List<ConfigurableDictionaryNode> { sensesNode, pictureNode },
FieldDescription = "LexEntry"
};
CssGeneratorTests.PopulateFieldsForTesting(mainEntryNode);
return mainEntryNode;
}
/// <summary>
/// Creates an ILexEntry object, optionally with specified headword and gloss
/// </summary>
/// <param name="cache"></param>
/// <param name="headword">Optional: defaults to 'Citation'</param>
/// <param name="gloss">Optional: defaults to 'gloss'</param>
/// <param name="definition">Optional: default is to omit</param>
/// <returns></returns>
internal static ILexEntry CreateInterestingLexEntry(LcmCache cache, string headword = "Citation", string gloss = "gloss", string definition = null)
{
var entryFactory = cache.ServiceLocator.GetInstance<ILexEntryFactory>();
var entry = entryFactory.Create();
var wsEn = EnsureWritingSystemSetup(cache, "en", false);
var wsFr = EnsureWritingSystemSetup(cache, "fr", true);
AddHeadwordToEntry(entry, headword, wsFr);
entry.Comment.set_String(wsEn, TsStringUtils.MakeString("Comment", wsEn));
AddSenseToEntry(entry, gloss, wsEn, cache, definition);
return entry;
}
internal static int EnsureWritingSystemSetup(LcmCache cache, string wsStr, bool isVernacular)
{
var wsFact = cache.WritingSystemFactory;
var result = wsFact.GetWsFromStr(wsStr);
if (result < 1)
{
if (isVernacular)
{
cache.LangProject.AddToCurrentVernacularWritingSystems(cache.WritingSystemFactory.get_Engine(wsStr) as CoreWritingSystemDefinition);
}
else
{
cache.LangProject.AddToCurrentAnalysisWritingSystems(cache.WritingSystemFactory.get_Engine(wsStr) as CoreWritingSystemDefinition);
}
}
return wsFact.GetWsFromStr(wsStr);
}
/// <summary>
/// Creates an ILexEntry object, optionally with specified headword and gloss
/// </summary>
/// <param name="cache"></param>
/// <param name="headword">Optional: defaults to 'Citation'</param>
/// <param name="gloss">Optional: defaults to 'gloss'</param>
/// <returns></returns>
internal static ILexEntry CreateInterestingSuffix(LcmCache cache, string headword = "ba", string gloss = "gloss")
{
var entry = CreateInterestingLexEntry(cache, headword, gloss);
var wsEn = cache.WritingSystemFactory.GetWsFromStr("en");
var suffixType = cache.LangProject.LexDbOA.MorphTypesOA.FindOrCreatePossibility("suffix", wsEn);
entry.LexemeFormOA = cache.ServiceLocator.GetInstance<IMoAffixAllomorphFactory>().Create();
entry.LexemeFormOA.MorphTypeRA = suffixType as IMoMorphType;
return entry;
}
internal sealed class TempGuidOn<T> : IDisposable where T : ICmObject
{
public T Item { get; private set; }
private readonly Guid m_OriginalGuid;
public TempGuidOn(T item, Guid tempGuid)
{
Item = item;
m_OriginalGuid = item.Guid;
SetGuidOn(item, tempGuid);
}
~TempGuidOn()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
System.Diagnostics.Debug.WriteLineIf(!disposing, "****** Missing Dispose() call for " + GetType().Name + " ******");
if (disposing)
SetGuidOn(Item, m_OriginalGuid);
}
private static void SetGuidOn(ICmObject item, Guid newGuid)
{
var refGuidField = ReflectionHelper.GetField(item, "m_guid");
ReflectionHelper.SetField(refGuidField, "m_guid", newGuid);
}
}
/// <summary>
/// Use reflection to set the guid on a variant form. May not work for all kinds of tests or appropriately be editing the database.
/// Because changing the Guid causes teardown problem, it must be reset prior to teardown (hence the Disposable <returns/>)
/// </summary>
internal static TempGuidOn<ILexEntryRef> CreateVariantForm(LcmCache cache, IVariantComponentLexeme main, ILexEntry variantForm, Guid guid,
string type = TestVariantName)
{
return new TempGuidOn<ILexEntryRef>(CreateVariantForm(cache, main, variantForm, type), guid);
}
/// <summary>
/// 'internal static' so Reversal tests can use it
/// </summary>
internal static ILexEntryRef CreateVariantForm(LcmCache cache, IVariantComponentLexeme main, ILexEntry variantForm, string type = TestVariantName)
{
var owningList = cache.LangProject.LexDbOA.VariantEntryTypesOA;
Assert.That(owningList, Is.Not.Null, "No VariantEntryTypes property on Lexicon object.");
var varType = owningList.ReallyReallyAllPossibilities.LastOrDefault(poss => poss.Name.AnalysisDefaultWritingSystem.Text == type) as ILexEntryType;
if (varType == null && type != null) // if this type doesn't exist, create it
{
varType = cache.ServiceLocator.GetInstance<ILexEntryTypeFactory>().Create();
owningList.PossibilitiesOS.Add(varType);
varType.Name.set_String(cache.DefaultAnalWs, type);
}
var refOut = variantForm.MakeVariantOf(main, varType);
// ILexEntry.MakeVariantOf sets a Type even if null is specified. But we want to test typeless variants, so clear them if null is specified.
if (type == null)
refOut.VariantEntryTypesRS.Clear();
return refOut;
}
/// <summary>
/// Use reflection to set the guid on a complex form. May not work for all kinds of tests or appropriately be editing the database.
/// Because changing the Guid causes teardown problem, it must be reset prior to teardown (hence the Disposable <returns/>)
/// </summary>
internal static TempGuidOn<ILexEntryRef> CreateComplexForm(LcmCache cache, IVariantComponentLexeme main, ILexEntry complexForm, Guid guid,
bool subentry)
{
return new TempGuidOn<ILexEntryRef>(CreateComplexForm(cache, main, complexForm, subentry), guid);
}
internal static ILexEntryRef CreateComplexForm(LcmCache cache, ICmObject main, ILexEntry complexForm, bool subentry, byte complexFormTypeIndex = 1)
{
return CreateComplexForm(cache, main, complexForm, subentry,
(ILexEntryType)cache.LangProject.LexDbOA.ComplexEntryTypesOA.PossibilitiesOS[complexFormTypeIndex]);
}
private static ILexEntryRef CreateComplexForm(LcmCache cache, ICmObject main, ILexEntry complexForm, bool subentry, Guid typeGuid)
{
return CreateComplexForm(cache, main, complexForm, subentry,
(ILexEntryType)cache.LangProject.LexDbOA.ComplexEntryTypesOA.PossibilitiesOS.First(x => x.Guid == typeGuid));
}
private static ILexEntryRef CreateComplexForm(LcmCache cache, ICmObject main, ILexEntry complexForm, bool subentry, ILexEntryType complexEntryType)
{
var complexEntryRef = cache.ServiceLocator.GetInstance<ILexEntryRefFactory>().Create();
complexForm.EntryRefsOS.Add(complexEntryRef);
var complexEntryTypeAbbrText = complexEntryType.Abbreviation.BestAnalysisAlternative.Text;
var complexEntryTypeRevAbbr = complexEntryType.ReverseAbbr;
// If there is no reverseAbbr, generate one from the forward abbr (e.g. "comp. of") by trimming the trailing " of"
if (complexEntryTypeRevAbbr.BestAnalysisAlternative.Equals(complexEntryTypeRevAbbr.NotFoundTss))
complexEntryTypeRevAbbr.SetAnalysisDefaultWritingSystem(complexEntryTypeAbbrText.Substring(0, complexEntryTypeAbbrText.Length - 3));
complexEntryRef.ComplexEntryTypesRS.Add(complexEntryType);
complexEntryRef.RefType = LexEntryRefTags.krtComplexForm;
complexEntryRef.ComponentLexemesRS.Add(main);
if (subentry)
complexEntryRef.PrimaryLexemesRS.Add(main);
else
complexEntryRef.ShowComplexFormsInRS.Add(main);
return complexEntryRef;
}
/// <summary>
/// Generates a Lexical Reference.
/// If refTypeReverseName is specified, generates a Ref of an Asymmetric Type (EntryOrSenseTree) with the specified reverse name;
/// otherwise, generates a Ref of a Symmetric Type (EntryOrSenseSequence).
/// </summary>
internal static void CreateLexicalReference(LcmCache cache, ICmObject mainEntry, ICmObject referencedForm, string refTypeName, string refTypeReverseName = null)
{
CreateLexicalReference(cache, mainEntry, referencedForm, null, refTypeName, refTypeReverseName);
}
private static void CreateLexicalReference(LcmCache cache, ICmObject firstEntry, ICmObject secondEntry, ICmObject thirdEntry, string refTypeName, string refTypeReverseName = null)
{
var lrt = CreateLexRefType(cache, LexRefTypeTags.MappingTypes.kmtEntryOrSenseSequence, refTypeName, "", refTypeReverseName, "");
if (!string.IsNullOrEmpty(refTypeReverseName))
{
lrt.ReverseName.set_String(cache.DefaultAnalWs, refTypeReverseName);
lrt.MappingType = (int)MappingTypes.kmtEntryOrSenseTree;
}
var lexRef = cache.ServiceLocator.GetInstance<ILexReferenceFactory>().Create();
lrt.MembersOC.Add(lexRef);
lexRef.TargetsRS.Add(firstEntry);
lexRef.TargetsRS.Add(secondEntry);
if (thirdEntry != null)
lexRef.TargetsRS.Add(thirdEntry);
}
private static ILexRefType CreateLexRefType(LcmCache cache, LexRefTypeTags.MappingTypes type, string name, string abbr, string revName, string revAbbr)
{
if (cache.LangProject.LexDbOA.ReferencesOA == null)
{
cache.LangProject.LexDbOA.ReferencesOA = cache.ServiceLocator.GetInstance<ICmPossibilityListFactory>().Create();
}
var referencePossibilities = cache.LangProject.LexDbOA.ReferencesOA.PossibilitiesOS;
if (referencePossibilities.Any(r => r.Name.BestAnalysisAlternative.Text == name))
{
return referencePossibilities.First(r => r.Name.BestAnalysisAlternative.Text == name) as ILexRefType;
}
var lrt = cache.ServiceLocator.GetInstance<ILexRefTypeFactory>().Create();
referencePossibilities.Add(lrt);
lrt.MappingType = (int)type;
lrt.Name.set_String(cache.DefaultAnalWs, name);
lrt.Abbreviation.set_String(cache.DefaultAnalWs, abbr);
if (!string.IsNullOrEmpty(revName))
lrt.ReverseName.set_String(cache.DefaultAnalWs, revName);
if (!string.IsNullOrEmpty(revAbbr))
lrt.ReverseAbbreviation.set_String(cache.DefaultAnalWs, revAbbr);
return lrt;
}
private void CreateLexReference(ILexRefType lrt, IEnumerable<ICmObject> sensesAndEntries)
{
CreateLexReference(lrt, sensesAndEntries, Guid.Empty);
}
private void CreateLexReference(ILexRefType lrt, IEnumerable<ICmObject> sensesAndEntries, Guid lexRefGuid)
{
var lexRef = Cache.ServiceLocator.GetInstance<ILexReferenceFactory>().Create(lexRefGuid, lrt);
foreach (var senseOrEntry in sensesAndEntries)
lexRef.TargetsRS.Add(senseOrEntry);
}
internal static ICmPossibility CreatePublicationType(string name, LcmCache cache)
{
return DictionaryConfigurationImportController.AddPublicationType(name, cache);
}
internal static void AddHeadwordToEntry(ILexEntry entry, string headword, int wsId)
{
// The headword field is special: it uses Citation if available, or LexemeForm if Citation isn't filled in
entry.CitationForm.set_String(wsId, TsStringUtils.MakeString(headword, wsId));
}
internal static void AddLexemeFormToEntry(ILexEntry entry, string lexemeForm, LcmCache cache)
{
entry.LexemeFormOA = cache.ServiceLocator.GetInstance<IMoAffixAllomorphFactory>().Create();
entry.LexemeFormOA.Form.SetVernacularDefaultWritingSystem(lexemeForm);
}
internal static ILexPronunciation AddPronunciationToEntry(ILexEntry entry, string content, int wsId, LcmCache cache)
{
var pronunciation = cache.ServiceLocator.GetInstance<ILexPronunciationFactory>().Create();
entry.PronunciationsOS.Add(pronunciation);
pronunciation.Form.set_String(wsId, TsStringUtils.MakeString(content, wsId));
return pronunciation;
}
internal static void AddSenseToEntry(ILexEntry entry, string gloss, int wsId, LcmCache cache, string definition = null)
{
var senseFactory = cache.ServiceLocator.GetInstance<ILexSenseFactory>();
var sense = senseFactory.Create();
entry.SensesOS.Add(sense);
if (!string.IsNullOrEmpty(gloss))
{
sense.Gloss.set_String(wsId, TsStringUtils.MakeString(gloss, wsId));
}
if (!string.IsNullOrEmpty(definition))
{
sense.Definition.set_String(wsId, TsStringUtils.MakeString(definition, wsId));
}
}
private void AddSenseAndTwoSubsensesToEntry(ICmObject entryOrSense, string gloss)
{
AddSenseAndTwoSubsensesToEntry(entryOrSense, gloss, Cache, m_wsEn);
}
internal static void AddSenseAndTwoSubsensesToEntry(ICmObject entryOrSense, string gloss, LcmCache cache, int ws)
{
var senseFactory = cache.ServiceLocator.GetInstance<ILexSenseFactory>();
var sense = senseFactory.Create();
var entry = entryOrSense as ILexEntry;
if (entry != null)
entry.SensesOS.Add(sense);
else
((ILexSense)entryOrSense).SensesOS.Add(sense);
sense.Gloss.set_String(ws, TsStringUtils.MakeString(gloss, ws));
var subSensesOne = senseFactory.Create();
sense.SensesOS.Add(subSensesOne);
subSensesOne.Gloss.set_String(ws, TsStringUtils.MakeString(gloss + "2.1", ws));
var subSensesTwo = senseFactory.Create();
sense.SensesOS.Add(subSensesTwo);
subSensesTwo.Gloss.set_String(ws, TsStringUtils.MakeString(gloss + "2.2", ws));
}
private void AddSingleSubSenseToSense(string gloss, ILexSense sense)
{
sense.Gloss.set_String(m_wsEn, TsStringUtils.MakeString(gloss, m_wsEn));
AddSubSenseToSense(gloss + "1.1", sense);
}
private void AddSubSenseToSense(string gloss, ILexSense sense)
{
var subSensesOne = sense.Cache.ServiceLocator.GetInstance<ILexSenseFactory>().Create();
sense.SensesOS.Add(subSensesOne);
subSensesOne.Gloss.set_String(m_wsEn, TsStringUtils.MakeString(gloss, m_wsEn));
}
internal static ILexExampleSentence AddExampleToSense(ILexSense sense, string content, LcmCache cache, int vern, int analy, string translation = null)
{
var exampleFact = cache.ServiceLocator.GetInstance<ILexExampleSentenceFactory>();
var example = exampleFact.Create(new Guid(), sense);
example.Example.set_String(vern, TsStringUtils.MakeString(content, vern));
if (translation != null)
{
var type = cache.ServiceLocator.GetInstance<ICmPossibilityRepository>().GetObject(CmPossibilityTags.kguidTranFreeTranslation);
var cmTranslation = cache.ServiceLocator.GetInstance<ICmTranslationFactory>().Create(example, type);
cmTranslation.Translation.set_String(analy, TsStringUtils.MakeString(translation, analy));
example.TranslationsOC.Add(cmTranslation);
}
return example;
}
private IMoForm AddAllomorphToEntry(ILexEntry entry, bool stem = true)
{
const int stringRepresentationFlid = 5097008;
var env = Cache.ServiceLocator.GetInstance<IPhEnvironmentFactory>().Create();
Cache.LangProject.PhonologicalDataOA.EnvironmentsOS.Add(env);
Cache.MainCacheAccessor.SetString(env.Hvo, stringRepresentationFlid, TsStringUtils.MakeString("phoneyEnv", m_wsEn));
if (stem)
{
var morphFact = Cache.ServiceLocator.GetInstance<IMoStemAllomorphFactory>();
var morph = morphFact.Create();
entry.AlternateFormsOS.Add(morph);
morph.Form.set_String(m_wsFr, TsStringUtils.MakeString("StemAllomorph", m_wsFr));
// add environment to the stem allomorph (PhoneEnvRC is not in the common base class between stem and affix allomorphs)
morph.PhoneEnvRC.Add(env);
return morph;
}
else
{
// if not stem, then it is an affix allomorph
var morphFact = Cache.ServiceLocator.GetInstance<IMoAffixAllomorphFactory>();
var morph = morphFact.Create();
entry.AlternateFormsOS.Add(morph);
morph.Form.set_String(m_wsFr, TsStringUtils.MakeString("AffixAllomorph", m_wsFr));
// add environment to the allomorph
morph.PhoneEnvRC.Add(env);
return morph;
}
}
internal static ICmPicture CreatePicture(LcmCache cache, bool exists = true, string caption = "caption", string ws = "en")
{
var pic = cache.ServiceLocator.GetInstance<ICmPictureFactory>().Create();
if (caption != null)
{
var wsHandle = cache.WritingSystemFactory.GetWsFromStr(ws);
pic.Caption.set_String(wsHandle, TsStringUtils.MakeString(caption, wsHandle));
}
var file = cache.ServiceLocator.GetInstance<ICmFileFactory>().Create();
if (cache.LangProject.MediaOC.Any())
{
cache.LangProject.MediaOC.First().FilesOC.Add(file);
}
else
{
var folder = cache.ServiceLocator.GetInstance<ICmFolderFactory>().Create();
cache.LangProject.MediaOC.Add(folder);
folder.FilesOC.Add(file);
}
file.InternalPath = exists
? Path.Combine(FwDirectoryFinder.SourceDirectory, "xWorks/xWorksTests/TestData/ImageFiles/test_auth_copy_license.jpg")
: "does/not/exist.jpg";
pic.PictureFileRA = file;
return pic;
}
internal static IStText CreateMultiParaText(string content, LcmCache cache)
{
var text = cache.ServiceLocator.GetInstance<ITextFactory>().Create();
//cache.LangProject.
var stText = cache.ServiceLocator.GetInstance<IStTextFactory>().Create();
cache.LangProject.InterlinearTexts.Add(stText);
text.ContentsOA = stText;
var para = cache.ServiceLocator.GetInstance<IStTxtParaFactory>().Create();
stText.ParagraphsOS.Add(para);
para.Contents = MakeVernTss("First para " + content, cache);
var para1 = cache.ServiceLocator.GetInstance<IStTxtParaFactory>().Create();
stText.ParagraphsOS.Add(para1);
para1.Contents = MakeVernTss("Second para " + content, cache);
return text.ContentsOA;
}
private static ITsString MakeVernTss(string content, LcmCache cache)
{
return TsStringUtils.MakeString(content, cache.DefaultVernWs);
}
private ITsString MakeMulitlingualTss(IEnumerable<string> content)
{
// automatically alternates runs between 'en' and 'fr'
var tsFact = TsStringUtils.TsStrFactory;
var lastWs = m_wsFr;
var builder = tsFact.GetIncBldr();
foreach (var runContent in content)
{
lastWs = lastWs == m_wsEn ? m_wsFr : m_wsEn; // switch ws for each run
builder.AppendTsString(TsStringUtils.MakeString(runContent, lastWs));
}
return builder.GetString();
}
internal static ITsString MakeBidirectionalTss(IEnumerable<string> content, LcmCache cache)
{
var wsHe = EnsureHebrewExists(cache);
var wsEn = cache.ServiceLocator.WritingSystems.AllWritingSystems
.First(ws => ws.Id == "en").Handle;
// automatically alternates runs between 'en' and 'he' (Hebrew)
var tsFact = TsStringUtils.TsStrFactory;
var lastWs = wsEn;
var builder = tsFact.GetIncBldr();
foreach (var runContent in content)
{
lastWs = lastWs == wsEn ? wsHe : wsEn; // switch ws for each run
builder.AppendTsString(tsFact.MakeString(runContent, lastWs));
}
return builder.GetString();
}
private static int EnsureHebrewExists(LcmCache cache)
{
var heWs =
cache.ServiceLocator.WritingSystems.AllWritingSystems.FirstOrDefault(ws =>
ws.Id == "he");
if (heWs != null)
return heWs.Handle;
var wsManager = cache.ServiceLocator.WritingSystemManager;
CoreWritingSystemDefinition hebrew;
wsManager.GetOrSet("he", out hebrew);
hebrew.RightToLeftScript = true;
return hebrew.Handle;
}
private void SetDictionaryNormalDirection(InheritableStyleProp<TriStateBool> rightToLeft)
{
ReflectionHelper.SetField(DictionaryNormalStyle, "m_rtl", rightToLeft);
}
internal static void SetPublishAsMinorEntry(ILexEntry entry, bool publish)
{
foreach (var ler in entry.EntryRefsOS)
ler.HideMinorEntry = publish ? 0 : 1;
}
public static DictionaryNodeOptions GetSenseNodeOptions()
{
return new DictionaryNodeSenseOptions { DisplayEachSenseInAParagraph = false };
}
public static DictionaryNodeOptions GetWsOptionsForLanguages(string[] languages)
{
return new DictionaryNodeWritingSystemOptions { Options = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(languages) };
}
public static DictionaryNodeOptions GetWsOptionsForLanguages(string[] languages, DictionaryNodeWritingSystemOptions.WritingSystemType type)
{
return new DictionaryNodeWritingSystemOptions
{
Options = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(languages),
WsType = type
};
}
public static DictionaryNodeOptions GetWsOptionsForLanguageswithDisplayWsAbbrev(string[] languages,
DictionaryNodeWritingSystemOptions.WritingSystemType type = 0)
{
return new DictionaryNodeWritingSystemOptions
{
Options = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(languages),
DisplayWritingSystemAbbreviations = true,
WsType = type
};
}
public static DictionaryNodeOptions GetListOptionsForItems(DictionaryNodeListOptions.ListIds listName, ICmPossibility[] checkedItems)
{
var listOptions = new DictionaryNodeListOptions
{
ListId = listName,
Options = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(checkedItems.Select(id => id.Guid.ToString()).ToList())
};
return listOptions;
}
public static DictionaryNodeOptions GetListOptionsForStrings(DictionaryNodeListOptions.ListIds listName, IEnumerable<string> checkedItems)
{
var listOptions = new DictionaryNodeListOptions
{
ListId = listName,
Options = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(checkedItems)
};
return listOptions;
}
public static DictionaryNodeOptions GetFullyEnabledListOptions(DictionaryNodeListOptions.ListIds listName, LcmCache cache)
{
return GetFullyEnabledListOptions(cache, listName);
}
public static DictionaryNodeOptions GetFullyEnabledListOptions(LcmCache cache, DictionaryNodeListOptions.ListIds listName)
{
List<DictionaryNodeListOptions.DictionaryNodeOption> dnoList;
var useParaOptions = false;
switch (listName)
{
case DictionaryNodeListOptions.ListIds.Minor:
dnoList = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(
new[] { XmlViewsUtils.GetGuidForUnspecifiedVariantType(), XmlViewsUtils.GetGuidForUnspecifiedComplexFormType() }
.Select(guid => guid.ToString())
.Union(cache.LangProject.LexDbOA.ComplexEntryTypesOA.PossibilitiesOS
.Union(cache.LangProject.LexDbOA.VariantEntryTypesOA.PossibilitiesOS).Select(item => item.Guid.ToString())));
break;
case DictionaryNodeListOptions.ListIds.Variant:
dnoList = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(
new[] { XmlViewsUtils.GetGuidForUnspecifiedVariantType().ToString() }
.Union(cache.LangProject.LexDbOA.VariantEntryTypesOA.PossibilitiesOS.Select(item => item.Guid.ToString())));
break;
case DictionaryNodeListOptions.ListIds.Complex:
useParaOptions = true;
dnoList = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(
new[] { XmlViewsUtils.GetGuidForUnspecifiedComplexFormType().ToString() }
.Union(cache.LangProject.LexDbOA.ComplexEntryTypesOA.PossibilitiesOS.Select(item => item.Guid.ToString())));
break;
case DictionaryNodeListOptions.ListIds.Note:
useParaOptions = true;
dnoList = DictionaryDetailsControllerTests.ListOfEnabledDNOsFromStrings(
new[] { XmlViewsUtils.GetGuidForUnspecifiedExtendedNoteType().ToString() }
.Union(cache.LangProject.LexDbOA.ExtendedNoteTypesOA.PossibilitiesOS.Select(item => item.Guid.ToString())));
break;
default:
throw new NotImplementedException(string.Format("Unknown list id {0}", listName));
}
DictionaryNodeListOptions listOptions = useParaOptions ? new DictionaryNodeListAndParaOptions() : new DictionaryNodeListOptions();
listOptions.ListId = listName;
listOptions.Options = dnoList;
return listOptions;
}
/// <summary>
/// Search haystack with regexQuery, and assert that requiredNumberOfMatches matches are found.
/// Can be used in place of AssertThatXmlIn.String().HasSpecifiedNumberOfMatchesForXpath(),
/// when slashes are needed in an argument to xpath starts-with.
/// </summary>
private static void AssertRegex(string haystack, string regexQuery, int requiredNumberOfMatches)
{
var regex = new Regex(regexQuery);
var matches = regex.Matches(haystack);
Assert.That(matches.Count, Is.EqualTo(requiredNumberOfMatches), "Unexpected number of matches");
}
public IPartOfSpeech CreatePartOfSpeech(string name, string abbr)
{
var posSeq = Cache.LangProject.PartsOfSpeechOA.PossibilitiesOS;
var pos = Cache.ServiceLocator.GetInstance<IPartOfSpeechFactory>().Create();
posSeq.Add(pos);
pos.Name.set_String(m_wsEn, name);
pos.Abbreviation.set_String(m_wsEn, abbr);
return pos;
}
// ReSharper disable once InconsistentNaming
public IMoMorphSynAnalysis CreateMSA(ILexEntry entry, IPartOfSpeech pos)
{
var msa = Cache.ServiceLocator.GetInstance<IMoStemMsaFactory>().Create();
entry.MorphoSyntaxAnalysesOC.Add(msa);
msa.PartOfSpeechRA = pos;
return msa;
}
}
#region Test classes and interfaces for testing the reflection code in GetPropertyTypeForConfigurationNode
internal class TestRootClass
{
public ITestInterface RootMember { get; set; }
public TestNonInterface ConcreteMember { get; set; }
}
internal interface ITestInterface : ITestBaseOne, ITestBaseTwo
{
string TestString { get; }
}
internal interface ITestBaseOne
{
IMoForm TestMoForm { get; }
}
internal interface ITestBaseTwo : ITestGrandParent
{
ICmObject TestIcmObject { get; }
}
internal class TestNonInterface
{
// ReSharper disable UnusedMember.Local // Justification: called by reflection
private string TestNonInterfaceString { get; set; }
// ReSharper restore UnusedMember.Local
}
internal interface ITestGrandParent
{
Stack<TestRootClass> TestCollection { get; }
}
internal class TestPictureClass
{
public ILcmList<ICmPicture> Pictures { get; set; }
}
internal static class TestExtensionMethod
{
static string Creator(this TestPictureClass extend)
{
return "bob";
}
}
#endregion
}