Exemple #1
0
        public static IServiceCollection AddShortCodeGenerator <TGenerator>(
            this IServiceCollection services,
            Action <ShortCodeOptions> options = null)
            where TGenerator : class, IShortCodeGenerator
        {
            ShortCodeOptions scOptions = new ShortCodeOptions();

            options?.Invoke(scOptions);

            services.AddSingleton(scOptions);
            services.AddSingleton <IEncoder>(p => new Encoder(scOptions.CharacterSpace));
            services.AddSingleton <IShortCodeGenerator, TGenerator>();
            services.AddSingleton <IShortCodeFactory, ShortCodeFactory>();
            return(services);
        }
        public void Ctor_LogsWarning_WhenOptionsAreInconsistent()
        {
            var logger  = new TestCaptureLogger <ShortCodeFactory>();
            var options = new ShortCodeOptions
            {
                CharacterSpace = Encoder.LettersAndDigits,
                FixedLength    = 20,
            };
            var encoder = new Encoder(options.CharacterSpace);

            _ = new ShortCodeFactory(new Mock <IShortCodeGenerator>().Object, encoder, options, logger);

            logger.Logs.Count.ShouldBe(1);
            Console.WriteLine(logger.Logs[0].FormattedMessage);
            logger.Logs[0].LogLevel.ShouldBe(LogLevel.Warning);
            logger.Logs[0].FormattedMessage.ShouldStartWith("The Short Code generator will always produce codes with padding");
        }
        public void GetCodes_ShouldReturnRequiredNumberOfCodes(int numberOfCodes)
        {
            var options = new ShortCodeOptions
            {
                CharacterSpace = Encoder.LettersAndDigits,
                FixedLength    = 5,
            };
            var factory = new ShortCodeFactory(
                new GuidCodeGenerator(),
                new Encoder(Encoder.ReducedAmbiguity),
                options,
                new NullLogger <ShortCodeFactory>());

            var result = factory.GetCodes(numberOfCodes).ToArray();

            result.Length.ShouldBe(numberOfCodes);
            result.Distinct().Count().ShouldBe(numberOfCodes);
        }
        public void GetNextCode_ShouldReturnCode()
        {
            var options = new ShortCodeOptions
            {
                CharacterSpace = Encoder.LettersAndDigits,
                FixedLength    = 5,
            };
            var factory = new ShortCodeFactory(
                new GuidCodeGenerator(),
                new Encoder(Encoder.ReducedAmbiguity),
                options,
                new NullLogger <ShortCodeFactory>());

            var result = factory.GetNextCode();

            Console.WriteLine(result);
            result.Length.ShouldBe(5);
        }