Exemple #1
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}") })
                    );
            }));
        }
Exemple #2
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);
                }
            }
        }
Exemple #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));
        }
Exemple #4
0
 public void Comparable_IsCanonical_True()
 {
     Assert.That.Comparable().IsCanonical(
         SoftString.Create("b"),
         SoftString.Create("a"),
         SoftString.Create("b"),
         SoftString.Create("c")
         );
 }
Exemple #5
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));
 }
        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);
        }
Exemple #7
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"));
 }
        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);
        }
        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);
        }
        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);
        }
Exemple #11
0
 public void ctor_DisallowsNullValue()
 {
     // ReSharper disable once AssignNullToNotNullAttribute - we want it to be null
     Assert.That.Throws <ArgumentNullException>(() => SoftString.Create(null));
 }
Exemple #12
0
 public void MyTestMethod()
 {
     Assert.That.Comparable().CompareTo().IsLessThan(SoftString.Create("fao"), "foz", "foz ", " foz", " foz ");
     Assert.That.Comparable().CompareTo().IsEqualTo(SoftString.Create("foo"), "foo", "fOo", "fOo ", " fOo", " Foo ");
     Assert.That.Comparable().CompareTo().IsGreaterThan(SoftString.Create("foo"), "foa", "foa ", " foa", " foa ");
 }
Exemple #13
0
 public void opEqual_SameValues_True()
 {
     Assert.That.BinaryOperator().Equality().IsTrue(default(SoftString), default(string));
     Assert.That.BinaryOperator().Equality().IsTrue(SoftString.Create("foo"), "foo", "fOo", "foo ", " fOo", " foo ");
 }
Exemple #14
0
 public void Equals_SameObjects_True()
 {
     Assert.That.Equatable().EqualsMethod().IsTrue(SoftString.Create("foo"), "foo", "fOo", "foo ", " fOo", " foo ");
 }
Exemple #15
0
 public void Equatable_IsCanonical_True()
 {
     Assert.That.Equatable().IsCanonical <SoftString>(SoftString.Create("foo"));
     Assert.That.Equatable().IsCanonical <string>(SoftString.Create("foo"));
 }
Exemple #16
0
 public void opExplicit_CanExplicitlyCastToString()
 {
     Assert.That.UnaryOperator().Convert(SoftString.Create("foo")).IsEqual("foo");
 }
Exemple #17
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 ");
 }
Exemple #18
0
 public void Equals_CanIdentifySimilarValues()
 {
     Assert.That.Equatable().EqualsMethod().IsTrue(SoftString.Create("foo"), "foo", "fOo", "foo ", " fOo", " foo ");
 }
Exemple #19
0
 public void this_CanGetChar()
 {
     Assert.AreEqual('r', SoftString.Create("bar")[2]);
 }
Exemple #20
0
 public static SoftString ToSoftString(this string value) => value is null ? null : SoftString.Create(value);
Exemple #21
0
 public void Length_GetsLengthOfTrimmedValue()
 {
     Assert.AreEqual(3, SoftString.Create(" fOo ").Length);
 }
Exemple #22
0
 public void ToString_NullString_Empty()
 {
     Assert.That.Collection().IsEmpty(SoftString.Create(null).ToString());
 }
Exemple #23
0
 public void Equals_CanIdentifyDifferentValues()
 {
     Assert.That.Equatable().EqualsMethod().IsFalse(SoftString.Create("foo"), "bar", "bar ", " bar", " bar ");
 }
Exemple #24
0
 public void ToString_EmptyString_Empty()
 {
     Assert.That.Collection().IsEmpty(SoftString.Create(string.Empty).ToString());
     Assert.That.Collection().IsEmpty(SoftString.Empty.ToString());
 }
Exemple #25
0
 public void opEqual_CanIdentifyDifferentValues()
 {
     Assert.That.BinaryOperator().Equality().IsFalse(SoftString.Create("foo"), "bar", " fOob");
 }
Exemple #26
0
 public void ToString_NonEmptyString_NonEmpty()
 {
     Assert.AreEqual("foo", SoftString.Create("foo").ToString());
     Assert.AreEqual("fOo", SoftString.Create("fOo").ToString());
 }
Exemple #27
0
 public void opImplicit_CanImplicitlyCastFromString()
 {
     Assert.That.UnaryOperator().Convert("foo").IsEqual(SoftString.Create("foo"));
 }
Exemple #28
0
 public void this_IndexInRange_Char()
 {
     Assert.AreEqual('r', SoftString.Create("bar")[2]);
 }
Exemple #29
0
 public void Length_StringWithWhitespace_StringWithoutWhitespace()
 {
     Assert.AreEqual(3, SoftString.Create(" fOo ").Length);
 }
Exemple #30
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var classNames =
                output
                .Attributes["class"]
                ?.Value
                .ToString()
                .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (classNames is null)
            {
                return;
            }

            var classPrefix = ClassPrefix;

            var inlineableClassNames =
                (from className in classNames
                 where className?.StartsWith(classPrefix) ?? false
                 select SoftString.Create(className)).ToList();

            if (inlineableClassNames.Empty())
            {
                return;
            }

            inlineableClassNames =
                inlineableClassNames
                .Select(className => $".{className.ToString()}".ToSoftString())
                .ToList();

            var url = _urlHelperFactory.GetUrlHelper(ViewContext);

            var theme = ViewContext.HttpContext.EmailMetadata()?.Theme ?? "default";

            var themeCssFileName = url.RouteUrl(RouteNames.Themes, new { name = theme });

            var route = ViewContext.HttpContext.ExtensionType().ToString();
            var extensionCssFileName = url.RouteUrl(route, new { extension = ViewContext.HttpContext.ExtensionId() });

            var themeCss = await _cssProvider.GetCss(themeCssFileName);

            var extensionCss = extensionCssFileName is null ? new List <CssRuleset>() : await _cssProvider.GetCss(extensionCssFileName);

            var declarations =
                from ruleset in themeCss.Concat(extensionCss)
                from selector in ruleset.Selectors
                join className in inlineableClassNames on selector equals className
                select ruleset.Declarations.TrimEnd(';');

            var style = declarations.Join("; ");

            if (style.IsNullOrEmpty())
            {
                // Make debugging of missing styles easier by highlighting the element with a red border.
                output.Attributes.SetAttribute("style", ClassNotFoundStyle);
            }
            else
            {
                output.Attributes.SetAttribute("style", style);
                output.Attributes.RemoveAll("class");
            }
        }