Пример #1
0
        public void GenerateSlug_Stores_slug_and_returns_if_it_does_not_already_exist()
        {
            A.CallTo(() => _fakeSlugAlgorithm.Slug("Some text"))
            .Returns("some-text");

            A.CallTo(() => _fakeSlugStore.Exists("some-text"))
            .Returns(false);

            var result = _slugGenerator.GenerateSlug("Some text");

            A.CallTo(() => _fakeSlugStore.Store(A <Slug> .That.Matches(x => x.Value == "some-text")))
            .MustHaveHappened();

            Assert.That(result, Is.EqualTo("some-text"));
        }
Пример #2
0
        public string GenerateSlug(string text, string[] uniquifiers = null)
        {
            var initialSlug = _slugAlgorithm.Slug(text);

            //we might get lucky on first try
            if (!_slugStore.Exists(initialSlug))
            {
                _slugStore.Store(new Slug(initialSlug));
                return(initialSlug);
            }

            //if we've got uniquifiers, iterate them
            if (uniquifiers != null && uniquifiers.Any())
            {
                string slugWithUniquifier = null;

                var uniquifierIterationIndex = 0;

                while (string.IsNullOrEmpty(slugWithUniquifier) && uniquifierIterationIndex < uniquifiers.Length)
                {
                    var uniquifier = uniquifiers.ElementAt(uniquifierIterationIndex);

                    slugWithUniquifier = _slugAlgorithm.Slug(text + " " + uniquifier);

                    if (_slugStore.Exists(slugWithUniquifier))
                    {
                        slugWithUniquifier = null;
                    }

                    uniquifierIterationIndex++;
                }

                //we couldn't generate a unique slug with any of the uniquifiers supplied
                //so now use the first uniquifier in the list, and append an incremental number until we find the unique
                if (string.IsNullOrEmpty(slugWithUniquifier))
                {
                    return(GenerateAndStoreSlugWithIncrementedNumberAppendage(text + " " + uniquifiers.First()));
                }

                _slugStore.Store(new Slug(slugWithUniquifier));

                return(slugWithUniquifier);
            }
            //no uniquifiers so add number on the end until we find a unique value
            return(GenerateAndStoreSlugWithIncrementedNumberAppendage(text));
        }