public void GenerateSlug_Appends_number_to_text_if_original_text_slugged_exists_Starting_at_supplied_seed_value()
        {
            //Arrange
            var slugGenerator = new SlugGenerator(new SlugGeneratorOptions {
                IterationSeedValue = 3
            }, _fakeSlugStore, _fakeSlugAlgorithm);                                                                                        //start at 3

            A.CallTo(() => _fakeSlugAlgorithm.Slug("Some text"))
            .Returns("some-text");

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

            A.CallTo(() => _fakeSlugAlgorithm.Slug("Some text 3"))
            .Returns("some-text-3");

            //Act
            var result = slugGenerator.GenerateSlug("Some text");

            //Assert
            Assert.That(result, Is.EqualTo("some-text-3"));

            A.CallTo(() => _fakeSlugAlgorithm.Slug("Some text 1"))
            .MustNotHaveHappened();

            A.CallTo(() => _fakeSlugAlgorithm.Slug("Some text 2"))
            .MustNotHaveHappened();
        }
Beispiel #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));
        }
Beispiel #3
0
        public void GenerateSlug_Slugs_given_text_initially()
        {
            var result = _slugGenerator.GenerateSlug("Some text");

            A.CallTo(() => _fakeSlugAlgorithm.Slug("Some text"))
            .MustHaveHappened();
        }