Exemple #1
0
        public void op_Delete_whenStorageDefined()
        {
            var obj = new Lexicon(NormalityComparer.Ordinal);

            var storage = new Mock<IStoreLexicon>();
            storage.Setup(x => x.Delete(obj)).Verifiable();

            obj.Storage = storage.Object;

            obj.Delete();

            storage.VerifyAll();
        }
        public void op_Save_Lexicon()
        {
            var lexicon = new Lexicon(NormalityComparer.Ordinal);

            var mock = new Mock<IStoreLexicon>();
            mock
                .Setup(x => x.Save(lexicon))
                .Verifiable();

            mock.Object.Save(lexicon);

            mock.VerifyAll();
        }
Exemple #3
0
        public void op_Save_IStoreLexicon()
        {
            var obj = new Lexicon(NormalityComparer.Ordinal);

            var storage = new Mock<IStoreLexicon>();
            storage.Setup(x => x.Save(obj)).Verifiable();

            obj.Save(storage.Object);

            Assert.Same(storage.Object, obj.Storage);

            storage.VerifyAll();
        }
        public void op_RemoveMatch_string_Lexicon_whenMatch()
        {
            var expected = string.Empty;

            var lexicon = new Lexicon(NormalityComparer.Ordinal)
                              {
                                  "Example"
                              };

            var actual = "Example".RemoveMatch(lexicon);

            Assert.Equal(expected, actual);
        }
        public void op_RemoveMatch_string_Lexicon()
        {
            const string expected = "Foo";

            var lexicon = new Lexicon(NormalityComparer.Ordinal)
                              {
                                  "Bar"
                              };

            var actual = expected.RemoveMatch(lexicon);

            Assert.Equal(expected, actual);
        }
        public void op_Load_INormalizationComparer()
        {
            var expected = new Lexicon(NormalityComparer.Ordinal);

            var mock = new Mock<IStoreLexicon>();
            mock
                .Setup(x => x.Load(NormalityComparer.Ordinal))
                .Returns(expected)
                .Verifiable();

            var actual = mock.Object.Load(NormalityComparer.Ordinal);

            Assert.Same(expected, actual);

            mock.VerifyAll();
        }
        public static string RemoveMatch(this string value,
                                         Lexicon lexicon)
#endif
        {
            if (null == value)
            {
                return null;
            }

            if (0 == value.Length)
            {
                return string.Empty;
            }

            if (null == lexicon)
            {
                throw new ArgumentNullException("lexicon");
            }

            return lexicon.Contains(value)
                       ? string.Empty
                       : value;
        }
        public void op_Save_LexiconWithComma()
        {
            using (var file = new TempFile())
            {
                file.Info.Delete();

                var lexicon = new Lexicon(NormalityComparer.Ordinal)
                                  {
                                      "foo, bar"
                                  };

                IStoreLexicon store = new CsvLexiconStorage(file.Info);
                store.Save(lexicon);

                file.Info.Refresh();
                Assert.True(file.Info.Exists);

                Assert.True(store.Load(NormalityComparer.Ordinal).Contains("foo, bar"));
            }
        }
        public void op_Load_INormalityComparerNull()
        {
            using (var file = new TempFile())
            {
                file.Info.Delete();

                var lexicon = new Lexicon(NormalityComparer.Ordinal)
                                  {
                                      "Example"
                                  };

                IStoreLexicon store = new CsvLexiconStorage(file.Info);
                store.Save(lexicon);

                Assert.Throws<ArgumentNullException>(() => store.Load(null));
            }
        }
 public static string RemoveMatch(string value, 
                                  Lexicon lexicon)
Exemple #11
0
        public virtual void Delete(Lexicon lexicon)
        {
            Trace.WriteIf(Tracing.Is.TraceVerbose, string.Empty);
            if (null == lexicon)
            {
                throw new ArgumentNullException("lexicon");
            }

            Location.Refresh();
            if (Location.Exists)
            {
                Location.Delete();
            }
        }
Exemple #12
0
        public virtual void Save(Lexicon lexicon)
        {
            Trace.WriteIf(Tracing.Is.TraceVerbose, string.Empty);
            if (null == lexicon)
            {
                throw new ArgumentNullException("lexicon");
            }

            using (var writers = new StreamWriterDictionary("CANONICAL,SYNONYMS")
                                     {
                                         Access = FileAccess.Write,
                                         Mode = FileMode.Create,
                                         Share = FileShare.None
                                     })
            {
#if NET20
                if (0 == IEnumerableExtensionMethods.Count(lexicon))
#else
                if (!lexicon.Any())
#endif
                {
                    writers.Item(Location.FullName).WriteLine(string.Empty);
                    return;
                }

#if NET20
                var items = new SortedList<string, LexicalItem>();
                foreach (var item in lexicon)
                {
                    items.Add(item.CanonicalForm, item);
                }

                foreach (var item in items)
                {
                    var synonyms = new SortedList<string, string>();
                    foreach (var synonym in item.Value.Synonyms)
                    {
                        synonyms.Add(synonym, synonym);
                    }

                    writers
                        .Item(Location.FullName)
                        .WriteLine(StringExtensionMethods.FormatWith(
                            "{0},{1}", 
                            CsvStringExtensionMethods.FormatCommaSeparatedValue(item.Value.CanonicalForm), 
                            CsvStringExtensionMethods.FormatCommaSeparatedValue(IEnumerableExtensionMethods.Concat(synonyms.Values, ';'))));
                }
#else
                foreach (var item in lexicon.OrderBy(x => x.CanonicalForm))
                {
                    writers
                        .Item(Location.FullName)
                        .WriteLine("{0},{1}".FormatWith(
                                                        item.CanonicalForm.FormatCommaSeparatedValue(),
                                                        item.Synonyms.OrderBy(x => x).Concat(';').FormatCommaSeparatedValue()));
                }

#endif
            }
        }
Exemple #13
0
        public virtual Lexicon Load(INormalityComparer comparer)
        {
            Trace.WriteIf(Tracing.Is.TraceVerbose, string.Empty);
            var result = new Lexicon(comparer)
                             {
                                 Storage = this
                             };

            Load(result, Location);

            return result;
        }