Exemplo n.º 1
0
        public void Nullable_Integers_Parsed_From_Strings_Correctly(string intPropertyName, string environmentKey, bool writable, bool changeable)
        {
            var intProperty = typeof(OwinContext).GetProperty(intPropertyName);

            // When the string is null the integer should also be null
            _Environment[environmentKey] = null;
            Assert.IsNull(intProperty.GetValue(_Context, null));

            // When the string is a number the integer should reflect it
            _Environment[environmentKey] = "123";
            Assert.AreEqual(123, intProperty.GetValue(_Context, null));

            // Setting the string to an unparseable value should report a null integer
            _Environment[environmentKey] = "not a number";
            Assert.IsNull(intProperty.GetValue(_Context, null));

            if (writable)
            {
                if (changeable)
                {
                    // Setting the integer value should change the environment
                    intProperty.SetValue(_Context, 88);
                    Assert.AreEqual("88", _Environment[environmentKey]);

                    // Setting the integer to null should remove the environment value
                    intProperty.SetValue(_Context, null);
                    Assert.IsFalse(_Environment.ContainsKey(environmentKey));
                }
                else
                {
                    // We can write if it's unchanged
                    _Environment[environmentKey] = "88";
                    intProperty.SetValue(_Context, 88);
                    Assert.AreEqual("88", _Environment[environmentKey]);

                    // We can write brand new values
                    _Environment.Remove(environmentKey);
                    intProperty.SetValue(_Context, 99);
                    Assert.AreEqual("99", _Environment[environmentKey]);

                    // We cannot change existing values to new ones
                    var sawException = false;
                    try {
                        intProperty.SetValue(_Context, 200);
                    } catch (TargetInvocationException ex) {
                        if (ex.InnerException is InvalidOperationException)
                        {
                            sawException = true;
                        }
                    }
                    Assert.IsTrue(sawException);
                    Assert.AreEqual("99", _Environment[environmentKey]);
                }
            }
        }
Exemplo n.º 2
0
        public void ContainsKey_Returns_True_If_Dictionary_Contains_Key()
        {
            _Dictionary.Add("key", 1);

            Assert.IsTrue(_Dictionary.ContainsKey("key"));
        }