Example #1
0
        public AndWhichConstraint <OptionAssertions <T>, T> BeSome(T expectedValue)
        {
            var value = _option.Apply(new SomeAsserter <T>());

            value.Should().Be(expectedValue);
            return(new AndWhichConstraint <OptionAssertions <T>, T>(this, value));
        }
Example #2
0
            public void ApplyOption_OnNone_ReturnsResultTypeNone()
            {
                Option <TestObject> none = None.Value;

                var result = none.Apply(_ => (Option <TestObject>) new TestObject());

                result.Should().BeOfType <None <TestObject> >();
            }
        public void AndTheFunctionIsNullThenNoneIsReturned()
        {
            var first  = Option.Some(2);
            var second = Option.Some(4);
            var third  = Option.Some(8);

            Assert.Throws <ArgumentNullException>(() => Option.Apply((Func <int, int, int, int>)null, first, second, third));
        }
        public void AndTheFunctionIsNullTheAnExceptionIsThrown()
        {
            var first  = Option.Some(2);
            var second = Option.Some(4);
            Func <int, int, int> nullFunc = null;

            Assert.Throws <ArgumentNullException>(() => Option.Apply(nullFunc, first, second));
        }
        public void AndTheSecondParamIsNullThenAnExceptionIsThrown()
        {
            var first  = Option.Some(4);
            var second = (IOption <int>)null;

            int Add(int a, int b) => a + b;

            Assert.Throws <ArgumentNullException>(() => Option.Apply(Add, first, second));
        }
        public void AndTheFirstParamIsNullThenAnExceptionIsThrown()
        {
            var first  = (IOption <int>)null;
            var second = Option.Some(4);
            var third  = Option.Some(8);

            int Add(int a, int b, int c) => a + b + c;

            Assert.Throws <ArgumentNullException>(() => Option.Apply(Add, first, second, third));
        }
Example #7
0
        public void LinqOptionApplyWhenSomeFunctionSomeValue()
        {
            Option <Func <int, bool> > optionFunction = _isEven;
            Option <int>  optionValue  = 36;
            Option <bool> optionResult =
                optionValue.Apply(optionFunction);

            bool result = optionResult.Match(value => value, () => false);

            Assert.IsTrue(optionResult.IsSome && result);
        }
Example #8
0
        public void Apply_accept_OT_To_A_Func_Returns_New_Func_With_OT_Applied(int?num)
        {
            var ot = OptionExt.FromNullable(num);
            Option <Func <int?, string> > OFunc = Some <Func <int?, string> >(val => $"this is the val {val}");

            var res = OFunc.Apply(ot);

            res.Match(
                () => Assert.True(num == null),
                val => Assert.Equal(val, $"this is the val {num.Value}"));
        }
Example #9
0
        public void Test_Some_Apply()
        {
            // Arrange.
            Option <int> someInt = 5;
            int          sum     = 2;

            // Act.
            someInt.Apply(x => sum += x);

            // Assert.
            Assert.Equal(7, sum);
        }
Example #10
0
            public void Apply_OnSome_ResultContainsValue()
            {
                var property   = new TestObject.TestProperty();
                var testObject = new TestObject {
                    Property = property
                };
                Option <TestObject> some = testObject;

                var result = some.Apply(obj => obj.Property);

                ((Some <TestObject.TestProperty>)result).Content.Should().Be(property);
            }
        public void AndTheFirstOptionIsNoneThenNoneIsReturned()
        {
            var first  = Option.No <int>();
            var second = Option.Some(2);

            int Add(int a, int b) => a + b;

            var observed = Option.Apply(Add, first, second);

            var expected = Option.No <int>();

            Assert.AreEqual(expected, observed);
        }
Example #12
0
    public static void Main(String[] args)
    {
        Option <double> res1 = Sqrt(5.0);
        Option <double> res2 = Sqrt(-5.0);

        Console.WriteLine("res1={0} and res2={1}", res1, res2);
        double res3 = (double)res1;     // Explicit Option<double> --> double

        res2 = 17.0;                    // Implicit double --> Option<double>
        Console.WriteLine("res3={0} and res2={1}", res3, res2);
        res1 = res1.Apply(new Fun <double, double>(Math.Log));
        Console.WriteLine("res1={0}", res1);
    }
Example #13
0
        public void LinqOptionApplyWhenNoneFunctionNoneValue()
        {
            Option <Func <int, bool> > optionFunction = Option <Func <int, bool> > .None();

            Option <int> optionValue = Option <int> .None();

            Option <bool> optionResult =
                optionValue.Apply(optionFunction);

            bool result = optionResult.Match(value => value, () => false);

            Assert.IsTrue(optionResult.IsNone);
        }
        public void AndBothOptionsAreSomeThenTheFunctionIsInvoked()
        {
            var first  = Option.Some(2);
            var second = Option.Some(4);

            int Add(int a, int b) => a + b;

            var observed = Option.Apply(Add, first, second);

            var expected = Option.Some(Add(2, 4));

            Assert.AreEqual(expected, observed);
        }
Example #15
0
            public void Apply_OnSome_WhenUsingImplicitCast_ContainsValue()
            {
                var property   = new TestObject.TestProperty();
                var testObject = new TestObject {
                    Property = property
                };
                Option <TestObject> some = testObject;

                TestObject.TestProperty result =
                    some.Apply(obj => obj.Property) as Some <TestObject.TestProperty>;

                result.Should().Be(property);
            }
        public void AndTheOptionsAreSomeThenTheFunctionIsInvoked()
        {
            var first  = Option.Some(2);
            var second = Option.Some(4);
            var third  = Option.Some(8);

            int Add(int a, int b, int c) => a + b + c;

            var observed = Option.Apply(Add, first, second, third);

            var expected = Option.Some(Add(2, 4, 8));

            Assert.AreEqual(expected, observed);
        }
Example #17
0
        /// <summary>
        /// Loads all required resources.
        /// </summary>
        private void Initialise()
        {
            // Load compiled-in resources.
            EmbeddedResources.Load();

            Log.Send("------------------------------");
            Log.Send($"Starting pass-winmenu {Version}");
            Log.Send("------------------------------");

            Log.Send($"Enabled security protocols: {ServicePointManager.SecurityProtocol}");

            // Create the notification service first, so it's available if initialisation fails.
            notificationService = Notifications.Create();

            // Initialise the DI Container builder.
            var builder = new ContainerBuilder();

            builder.Register(_ => notificationService)
            .AsImplementedInterfaces()
            .SingleInstance();

            // Now load the configuration options that we'll need
            // to continue initialising the rest of the applications.
            var runtimeConfig = RuntimeConfiguration.Parse(Environment.GetCommandLineArgs());

            LoadConfigFile(runtimeConfig);

            builder.Register(_ => ConfigManager.Config).AsSelf();
            builder.Register(_ => ConfigManager.Config.Gpg).AsSelf();
            builder.Register(_ => ConfigManager.Config.Git).AsSelf();
            builder.Register(_ => ConfigManager.Config.PasswordStore).AsSelf();
            builder.Register(_ => ConfigManager.Config.Application.UpdateChecking).AsSelf();
            builder.Register(_ => ConfigManager.Config.PasswordStore.UsernameDetection).AsSelf();

#if DEBUG
            Log.EnableFileLogging();
#else
            if (ConfigManager.Config.CreateLogFile)
            {
                Log.EnableFileLogging();
            }
#endif

            // Register actions and hotkeys
            builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(ActionDispatcher)))
            .InNamespaceOf <ActionDispatcher>()
            .Except <ActionDispatcher>()
            .AsImplementedInterfaces();
            builder.RegisterType <HotkeyManager>()
            .AsSelf();

            builder.RegisterType <ActionDispatcher>()
            .WithParameter(
                (p, ctx) => p.ParameterType == typeof(Dictionary <HotkeyAction, IAction>),
                (info, context) => context.Resolve <IEnumerable <IAction> >().ToDictionary(a => a.ActionType));

            // Register environment wrappers
            builder.RegisterTypes(
                typeof(FileSystem),
                typeof(SystemEnvironment),
                typeof(Processes),
                typeof(ExecutablePathResolver)
                ).AsImplementedInterfaces();

            // Register GPG types
            builder.RegisterTypes(
                typeof(GpgInstallationFinder),
                typeof(GpgHomedirResolver),
                typeof(GpgAgentConfigReader),
                typeof(GpgAgentConfigUpdater),
                typeof(GpgTransport),
                typeof(GpgAgent),
                typeof(GpgResultVerifier),
                typeof(GPG)
                ).AsImplementedInterfaces()
            .AsSelf();

            // Register GPG installation
            // Single instance, as there is no need to look for the same GPG installation multiple times.
            builder.Register(context => context.Resolve <GpgInstallationFinder>().FindGpgInstallation(ConfigManager.Config.Gpg.GpgPath))
            .SingleInstance();

            builder.RegisterType <DialogCreator>()
            .AsSelf();

            // Register the internal password manager
            builder.Register(context => context.Resolve <IFileSystem>().DirectoryInfo.FromDirectoryName(context.Resolve <PasswordStoreConfig>().Location))
            .Named("PasswordStore", typeof(IDirectoryInfo));

            builder.RegisterType <GpgRecipientFinder>().WithParameter(
                (parameter, context) => true,
                (parameter, context) => context.ResolveNamed <IDirectoryInfo>("PasswordStore"))
            .AsImplementedInterfaces();

            builder.RegisterType <PasswordManager>().WithParameter(
                (parameter, context) => parameter.ParameterType == typeof(IDirectoryInfo),
                (parameter, context) => context.ResolveNamed <IDirectoryInfo>("PasswordStore"))
            .AsImplementedInterfaces()
            .AsSelf();

            builder.RegisterType <PasswordFileParser>().AsSelf();

            // Create the Git wrapper, if enabled.
            // This needs to be a single instance to stop startup warnings being displayed multiple times.
            builder.RegisterType <GitSyncStrategies>().AsSelf();
            builder.Register(CreateSyncService)
            .AsSelf()
            .SingleInstance();

            builder.Register(context => UpdateCheckerFactory.CreateUpdateChecker(context.Resolve <UpdateCheckingConfig>(), context.Resolve <INotificationService>()));
            builder.RegisterType <RemoteUpdateCheckerFactory>().AsSelf();
            builder.Register(context => context.Resolve <RemoteUpdateCheckerFactory>().Build()).AsSelf();

            // Build the container
            container = builder.Build();

            var gpgConfig = container.Resolve <GpgConfig>();
            if (gpgConfig.GpgAgent.Config.AllowConfigManagement)
            {
                container.Resolve <GpgAgentConfigUpdater>().UpdateAgentConfig(gpgConfig.GpgAgent.Config.Keys);
            }

            var actionDispatcher = container.Resolve <ActionDispatcher>();

            notificationService.AddMenuActions(actionDispatcher);

            // Assign our hotkeys.
            AssignHotkeys(actionDispatcher);

            // Start checking for updates
            updateChecker       = container.Resolve <UpdateChecker>();
            remoteUpdateChecker = container.Resolve <Option <RemoteUpdateChecker> >();

            if (container.Resolve <UpdateCheckingConfig>().CheckForUpdates)
            {
                updateChecker.Start();
            }
            remoteUpdateChecker.Apply(c => c.Start());
        }