Пример #1
0
        public static SoftKeySet CreateCommandName([NotNull] Type commadType)
        {
            if (commadType == null)
            {
                throw new ArgumentNullException(nameof(commadType));
            }

            if (!typeof(IConsoleCommand).IsAssignableFrom(commadType))
            {
                throw new ArgumentException(paramName: nameof(commadType), message: $"'{nameof(commadType)}' needs to be derived from '{nameof(ICommand)}'");
            }

            var names = GetCommandNames();

            var category = commadType.GetCustomAttribute <CategoryAttribute>()?.Category;

            if (category.IsNotNull())
            {
                names = names.Select(n => SoftString.Create($"{category}.{n}"));
            }

            return(SoftKeySet.Create(names));

            IEnumerable <SoftString> GetCommandNames()
            {
                yield return(GetCommandDefaultName(commadType));

                foreach (var name in commadType.GetCustomAttribute <AliasAttribute>() ?? Enumerable.Empty <SoftString>())
                {
                    yield return(name);
                }
            }
        }
Пример #2
0
        public static Identifier GetCommandId([NotNull] Type commandType)
        {
            if (commandType == null)
            {
                throw new ArgumentNullException(nameof(commandType));
            }

            if (!typeof(IConsoleCommand).IsAssignableFrom(commandType))
            {
                throw new ArgumentException(
                          paramName: nameof(commandType),
                          message: $"'{nameof(commandType)}' needs to be derived from '{nameof(ICommand)}'");
            }

            return(CommandNameCache.GetOrAdd(commandType, t =>
            {
                var category = t.GetCustomAttribute <CategoryAttribute>()?.Category;

                var names = GetCommandNames(t).Distinct();

                return new Identifier(
                    category is null
                        ? names
                        : names.SelectMany(name => new[] { name, SoftString.Create($"{category}.{name}") })
                    );
            }));
        }
Пример #3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jToken     = JToken.Load(reader);
            var softString = jToken.Value <string>();

            return(SoftString.Create(softString));
        }
Пример #4
0
        private IEnumerable <SoftString> GetComponents(SoftString scheme)
        {
            if (scheme)
            {
                yield return($"{scheme.ToString()}:");
            }

            if (Authority)
            {
                yield return($"//{Authority.ToString()}{(Path.Original ? "/" : string.Empty)}");
            }

            yield return(Path.Original);

            if (Query.Any())
            {
                var queryPairs =
                    Query
                    .OrderBy(x => x.Key)
                    .Select(x => $"{x.Key.ToString()}{(x.Value ? "=" : string.Empty)}{x.Value.ToString()}");
                yield return($"?{string.Join("&", queryPairs)}");
            }

            if (Fragment)
            {
                yield return($"#{Fragment.ToString()}");
            }
        }
Пример #5
0
        internal Logger([NotNull] SoftString name, [NotNull] LogPredicate logPredicate, Action dispose)
        {
            _name         = name ?? throw new ArgumentNullException(nameof(name));
            _logPredicate = logPredicate;
            _dispose      = dispose;

            _observers = new HashSet <IObserver <ILog> >();
        }
Пример #6
0
        public void Add([NotNull] SoftString id)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            Add((Identifier)id);
        }
Пример #7
0
        private Func <HttpRequest, ResponseInfo> GetResponseFactory(UriString path, SoftString method)
        {
            if (_teacup is null)
            {
                throw new InvalidOperationException($"Cannot get response without scope. Call '{nameof(BeginScope)}' first.");
            }

            return(_teacup.GetResponseFactory(path, method));
        }
Пример #8
0
 public void Comparable_IsCanonical_True()
 {
     Assert.That.Comparable().IsCanonical(
         SoftString.Create("b"),
         SoftString.Create("a"),
         SoftString.Create("b"),
         SoftString.Create("c")
         );
 }
Пример #9
0
        public void Add([NotNull] SoftString id, [NotNull] string value)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            Add((Identifier)id, value);
        }
Пример #10
0
 public static IImmutableDictionary <SoftString, Type> From(IEnumerable <Type> types)
 {
     return
         ((
              from type in types.Select(ValidateHasNoGenericArguments)
              // Pick the nearest namespace-attribute or one derived from it.
              let prefix = type.GetCustomAttributes <NamespaceAttribute>().FirstOrDefault()?.ToString()
                           let prettyName = type.ToPrettyString()
                                            where !prettyName.IsDynamicType()
                                            //from name in new[] { prettyName, type.GetCustomAttribute<AliasAttribute>()?.ToString() }
                                            //where name.IsNotNullOrEmpty()
                                            select(type, prettyName: SoftString.Create((prefix.IsNullOrEmpty() ? string.Empty : $"{prefix}.") + prettyName))
              ).ToImmutableDictionary(t => t.prettyName, t => t.type));
 }
Пример #11
0
        public void GetValue_StaticPropertyLocal_GotValue()
        {
            var configuration = Mock.Create <IConfiguration>();

            Mock
            .Arrange(() => configuration.GetValue(
                         Arg.Matches <SoftString>(name => name == SoftString.Create($"{Namespace}+TestClass.Bar")),
                         Arg.IsAny <Type>(),
                         Arg.IsAny <SoftString>())
                     )
            .Returns("bar")
            .OccursOnce();

            TestClass.AssertGetValueStatic(configuration);
        }
Пример #12
0
 public void Select_SettingDoesNotExist_SettingNotFoundException()
 {
     Assert.That.ThrowsExceptionFiltered <DynamicException>(() =>
     {
         var config = new Configuration(options =>
         {
             options
             .UseJsonConverter()
             .UseInMemory(Enumerable.Empty <ISetting>());
         });
         config.Select <int>(SoftString.Create("abc"));
         Assert.Fail();
     },
                                                            ex => ex.NameEquals("SettingNotFoundException"));
 }
Пример #13
0
        public void GetValue_InstancePropertyLocal_GotValue()
        {
            var configuration = Mock.Create <IConfiguration>();

            Mock
            .Arrange(() => configuration.GetValue(
                         Arg.Matches <SoftString>(name => name == SoftString.Create($"{Namespace}+TestClass.Foo")),
                         Arg.IsAny <Type>(),
                         Arg.IsAny <SoftString>())
                     )
            .Returns("foo")
            .OccursOnce();

            var testClass = new TestClass();

            testClass.AssertGetValue(configuration);
        }
Пример #14
0
        public void GetValue_StaticProperty_GotValue()
        {
            var configuration = Mock.Create <IConfiguration>();

            Mock
            .Arrange(() => configuration.GetValue(
                         Arg.Matches <SoftString>(name => name == SoftString.Create($"{Namespace}+TestClass.Bar")),
                         Arg.IsAny <Type>(),
                         Arg.IsAny <SoftString>())
                     )
            .Returns("bar")
            .OccursOnce();

            var value = configuration.GetValueFor(() => TestClass.Bar);

            configuration.Assert();
            Assert.AreEqual("bar", value);
        }
Пример #15
0
        public void AssignValue_InstanceProperty_Assigned()
        {
            var configuration = Mock.Create <IConfiguration>();

            Mock
            .Arrange(() => configuration.GetValue(
                         Arg.Matches <SoftString>(name => name == SoftString.Create($"{Namespace}+TestClass.Foo")),
                         Arg.IsAny <Type>(),
                         Arg.IsAny <SoftString>())
                     )
            .Returns("foo")
            .OccursOnce();

            var testClass = new TestClass();

            configuration.AssignValueTo(() => testClass.Foo);

            configuration.Assert();
            Assert.AreEqual("foo", testClass.Foo);
        }
Пример #16
0
 private NLog.ILogger GetLogger(SoftString name)
 {
     return(_cache.GetOrAdd(name, n => NLog.LogManager.GetLogger(name.ToString())));
 }
Пример #17
0
 internal static HtmlTableColumn Create <T>(SoftString name) => new HtmlTableColumn
 {
     Name = name,
     Type = typeof(T)
 };
Пример #18
0
 public static T ValueOrDefault <T>(this HtmlTableRow row, SoftString name) => row[name] is T value ? value : default;
Пример #19
0
 public HtmlTableCell this[SoftString name] => this.Single(cell => cell.Column.Name.Equals(name));
Пример #20
0
 public Func <HttpRequest, ResponseInfo> GetResponseFactory(UriString uri, SoftString method)
 {
     return(_mocks.FirstOrDefault(m => m.Uri == uri)?.GetResponseFactory(method));
 }
Пример #21
0
 public UriStringComponent([NotNull] SoftString value) => Original = value ?? throw new ArgumentNullException(nameof(value));
Пример #22
0
 public void opEqual_CanIdentifyDifferentValues()
 {
     Assert.That.BinaryOperator().Equality().IsFalse(SoftString.Create("foo"), "bar", " fOob");
 }
Пример #23
0
 public void Equals_CanIdentifyDifferentValues()
 {
     Assert.That.Equatable().EqualsMethod().IsFalse(SoftString.Create("foo"), "bar", "bar ", " bar", " bar ");
 }
Пример #24
0
 public void Equals_CanIdentifySimilarValues()
 {
     Assert.That.Equatable().EqualsMethod().IsTrue(SoftString.Create("foo"), "foo", "fOo", "foo ", " fOo", " foo ");
 }
Пример #25
0
 public void Length_GetsLengthOfTrimmedValue()
 {
     Assert.AreEqual(3, SoftString.Create(" fOo ").Length);
 }
Пример #26
0
 public string ToString(SoftString scheme) => string.Join(string.Empty, GetComponents(scheme));
Пример #27
0
 public void opExplicit_CanExplicitlyCastToString()
 {
     Assert.That.UnaryOperator().Convert(SoftString.Create("foo")).IsEqual("foo");
 }
Пример #28
0
 public void Equatable_IsCanonical_True()
 {
     Assert.That.Equatable().IsCanonical <SoftString>(SoftString.Create("foo"));
     Assert.That.Equatable().IsCanonical <string>(SoftString.Create("foo"));
 }
Пример #29
0
 public void opEqual_CanIdentifySimilarValues()
 {
     Assert.That.BinaryOperator().Equality().IsTrue(default(SoftString), default(string));
     Assert.That.BinaryOperator().Equality().IsTrue(SoftString.Create("foo"), "foo", "fOo", "foo ", " fOo", " foo ");
 }
Пример #30
0
 public void opImplicit_CanImplicitlyCastFromString()
 {
     Assert.That.UnaryOperator().Convert("foo").IsEqual(SoftString.Create("foo"));
 }