예제 #1
0
        public bool MatchesVisitor(string definition)
        {
            Mandate.ParameterNotNullOrEmpty(definition, "definition");

            MemberProfileFieldSetting setting;

            try
            {
                setting = JsonConvert.DeserializeObject <MemberProfileFieldSetting>(definition);
            }
            catch (JsonReaderException)
            {
                throw new ArgumentException(string.Format("Provided definition is not valid JSON: {0}", definition));
            }

            var value = _memberProfileFieldProvider.GetMemberProfileFieldValue(setting.Alias);

            switch (setting.Match)
            {
            case MemberProfileFieldSettingMatch.MatchesValue:
                return(string.Equals(setting.Value, value, StringComparison.InvariantCultureIgnoreCase));

            case MemberProfileFieldSettingMatch.DoesNotMatchValue:
                return(!string.Equals(setting.Value, value, StringComparison.InvariantCultureIgnoreCase));

            case MemberProfileFieldSettingMatch.GreaterThanValue:
            case MemberProfileFieldSettingMatch.GreaterThanOrEqualToValue:
            case MemberProfileFieldSettingMatch.LessThanValue:
            case MemberProfileFieldSettingMatch.LessThanOrEqualToValue:
                return(ComparisonHelpers.CompareValues(value, setting.Value, GetComparison(setting.Match)));

            default:
                return(false);
            }
        }
        public bool MatchesVisitor(string definition)
        {
            Mandate.ParameterNotNullOrEmpty(definition, "definition");

            SessionSetting sessionSetting;

            try
            {
                sessionSetting = JsonConvert.DeserializeObject <SessionSetting>(definition);
            }
            catch (JsonReaderException)
            {
                throw new ArgumentException(string.Format("Provided definition is not valid JSON: {0}", definition));
            }

            if (string.IsNullOrEmpty(sessionSetting.Key))
            {
                throw new ArgumentNullException("key", "Session key not set");
            }

            var keyExists = _sessionProvider.KeyExists(sessionSetting.Key);
            var value     = string.Empty;

            if (keyExists)
            {
                value = _sessionProvider.GetValue(sessionSetting.Key);
            }

            switch (sessionSetting.Match)
            {
            case SessionSettingMatch.Exists:
                return(keyExists);

            case SessionSettingMatch.DoesNotExist:
                return(!keyExists);

            case SessionSettingMatch.MatchesValue:
                return(keyExists && value == sessionSetting.Value);

            case SessionSettingMatch.ContainsValue:
                return(keyExists && value.Contains(sessionSetting.Value));

            case SessionSettingMatch.GreaterThanValue:
            case SessionSettingMatch.GreaterThanOrEqualToValue:
            case SessionSettingMatch.LessThanValue:
            case SessionSettingMatch.LessThanOrEqualToValue:
                return(keyExists &&
                       ComparisonHelpers.CompareValues(value, sessionSetting.Value, GetComparison(sessionSetting.Match)));

            default:
                return(false);
            }
        }
        public void ComparisonHelpers_CompareValues_StringComparisons_ReturnsCorrectValues()
        {
            // Arrange
            const string value1 = "a";
            const string value2 = "b";
            const string value3 = "c";

            // Act
            var result  = ComparisonHelpers.CompareValues(value2, value1, Comparison.GreaterThan);
            var result2 = ComparisonHelpers.CompareValues(value2, value2, Comparison.GreaterThan);
            var result3 = ComparisonHelpers.CompareValues(value2, value3, Comparison.GreaterThan);

            var result4 = ComparisonHelpers.CompareValues(value2, value1, Comparison.GreaterThanOrEqual);
            var result5 = ComparisonHelpers.CompareValues(value2, value2, Comparison.GreaterThanOrEqual);
            var result6 = ComparisonHelpers.CompareValues(value2, value3, Comparison.GreaterThanOrEqual);

            var result7 = ComparisonHelpers.CompareValues(value2, value1, Comparison.LessThan);
            var result8 = ComparisonHelpers.CompareValues(value2, value2, Comparison.LessThan);
            var result9 = ComparisonHelpers.CompareValues(value2, value3, Comparison.LessThan);

            var result10 = ComparisonHelpers.CompareValues(value2, value1, Comparison.LessThanOrEqual);
            var result11 = ComparisonHelpers.CompareValues(value2, value2, Comparison.LessThanOrEqual);
            var result12 = ComparisonHelpers.CompareValues(value2, value3, Comparison.LessThanOrEqual);

            // Assert
            Assert.IsTrue(result);
            Assert.IsFalse(result2);
            Assert.IsFalse(result3);

            Assert.IsTrue(result4);
            Assert.IsTrue(result5);
            Assert.IsFalse(result6);

            Assert.IsFalse(result7);
            Assert.IsFalse(result8);
            Assert.IsTrue(result9);

            Assert.IsFalse(result10);
            Assert.IsTrue(result11);
            Assert.IsTrue(result12);
        }