winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

TestConfigurationUnitProcessor.cs (5519B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="TestConfigurationUnitProcessor.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.UnitTests.Helpers
      8 {
      9     using System.Collections.Generic;
     10 
     11     /// <summary>
     12     /// A test implementation of IConfigurationProcessorFactory.
     13     /// </summary>
     14     internal partial class TestConfigurationUnitProcessor : IConfigurationUnitProcessor
     15     {
     16         /// <summary>
     17         /// Initializes a new instance of the <see cref="TestConfigurationUnitProcessor"/> class.
     18         /// </summary>
     19         /// <param name="unit">The unit.</param>
     20         internal TestConfigurationUnitProcessor(ConfigurationUnit unit)
     21         {
     22             this.Unit = unit;
     23         }
     24 
     25         /// <summary>
     26         /// The delegate for ApplySettings.
     27         /// </summary>
     28         /// <returns>The result.</returns>
     29         internal delegate IApplySettingsResult ApplySettingsDelegateType();
     30 
     31         /// <summary>
     32         /// The delegate for GetSettings.
     33         /// </summary>
     34         /// <returns>The result.</returns>
     35         internal delegate IGetSettingsResult GetSettingsDelegateType();
     36 
     37         /// <summary>
     38         /// The delegate for TestSettings.
     39         /// </summary>
     40         /// <returns>The result.</returns>
     41         internal delegate ITestSettingsResult TestSettingsDelegateType();
     42 
     43         /// <summary>
     44         /// The delegate for TestSettings that passes the unit in.
     45         /// </summary>
     46         /// <param name="unit">The unit.</param>
     47         /// <returns>The result.</returns>
     48         internal delegate ITestSettingsResult TestSettingsDelegateWithUnitType(ConfigurationUnit unit);
     49 
     50         /// <summary>
     51         /// Gets or sets the directives overlay.
     52         /// </summary>
     53         public IReadOnlyDictionary<string, object>? DirectivesOverlay { get; set; }
     54 
     55         /// <summary>
     56         /// Gets the configuration unit.
     57         /// </summary>
     58         public ConfigurationUnit Unit { get; private set; }
     59 
     60         /// <summary>
     61         /// Gets or sets the delegate object for ApplySettings.
     62         /// </summary>
     63         internal ApplySettingsDelegateType? ApplySettingsDelegate { get; set; }
     64 
     65         /// <summary>
     66         /// Gets the number of times ApplySettings is called.
     67         /// </summary>
     68         internal int ApplySettingsCalls { get; private set; } = 0;
     69 
     70         /// <summary>
     71         /// Gets or sets the delegate object for GetSettings.
     72         /// </summary>
     73         internal GetSettingsDelegateType? GetSettingsDelegate { get; set; }
     74 
     75         /// <summary>
     76         /// Gets the number of times GetSettings is called.
     77         /// </summary>
     78         internal int GetSettingsCalls { get; private set; } = 0;
     79 
     80         /// <summary>
     81         /// Gets or sets the delegate object for TestSettings.
     82         /// </summary>
     83         internal TestSettingsDelegateType? TestSettingsDelegate { get; set; }
     84 
     85         /// <summary>
     86         /// Gets or sets the delegate object for TestSettings that takes in the unit.
     87         /// </summary>
     88         internal TestSettingsDelegateWithUnitType? TestSettingsDelegateWithUnit { get; set; }
     89 
     90         /// <summary>
     91         /// Gets the number of times TestSettings is called.
     92         /// </summary>
     93         internal int TestSettingsCalls { get; private set; } = 0;
     94 
     95         /// <summary>
     96         /// Calls the ApplySettingsDelegate if one is provided; returns success if not.
     97         /// </summary>
     98         /// <returns>The result.</returns>
     99         public IApplySettingsResult ApplySettings()
    100         {
    101             ++this.ApplySettingsCalls;
    102             if (this.ApplySettingsDelegate != null)
    103             {
    104                 return this.ApplySettingsDelegate();
    105             }
    106             else
    107             {
    108                 return new ApplySettingsResultInstance(this.Unit);
    109             }
    110         }
    111 
    112         /// <summary>
    113         /// Calls the GetSettingsDelegate if one is provided; returns success if not (with no settings values).
    114         /// </summary>
    115         /// <returns>The result.</returns>
    116         public IGetSettingsResult GetSettings()
    117         {
    118             ++this.GetSettingsCalls;
    119             if (this.GetSettingsDelegate != null)
    120             {
    121                 return this.GetSettingsDelegate();
    122             }
    123             else
    124             {
    125                 return new GetSettingsResultInstance(this.Unit);
    126             }
    127         }
    128 
    129         /// <summary>
    130         /// Calls the TestSettingsDelegate if one is provided; returns success if not (with a positive test result).
    131         /// </summary>
    132         /// <returns>The result.</returns>
    133         public ITestSettingsResult TestSettings()
    134         {
    135             ++this.TestSettingsCalls;
    136             if (this.TestSettingsDelegateWithUnit != null)
    137             {
    138                 return this.TestSettingsDelegateWithUnit(this.Unit);
    139             }
    140             else if (this.TestSettingsDelegate != null)
    141             {
    142                 return this.TestSettingsDelegate();
    143             }
    144             else
    145             {
    146                 return new TestSettingsResultInstance(this.Unit) { TestResult = ConfigurationTestResult.Positive };
    147             }
    148         }
    149     }
    150 }