Esempio n. 1
0
        public void ExpressionPropertyTests_EnumExpression()
        {
            var data = new
            {
                test = TestEnum.Two
            };

            var val = new EnumExpression <TestEnum>("three");

            Assert.Null(val.ExpressionText);
            Assert.Equal(TestEnum.Three, val.Value);
            Assert.Equal(val.ToString(), JsonConvert.DeserializeObject <EnumExpression <TestEnum> >(JsonConvert.SerializeObject(val, settings: settings), settings: settings).ToString());
            var(result, error) = val.TryGetValue(data);
            Assert.Equal(TestEnum.Three, result);
            Assert.Null(error);

            val = new EnumExpression <TestEnum>("=three");
            Assert.Null(val.ExpressionText);
            Assert.Equal(TestEnum.Three, val.Value);
            Assert.Equal(val.ToString(), JsonConvert.DeserializeObject <EnumExpression <TestEnum> >(JsonConvert.SerializeObject(val, settings: settings), settings: settings).ToString());
            (result, error) = val.TryGetValue(data);
            Assert.Equal(TestEnum.Three, result);
            Assert.Null(error);

            val = new EnumExpression <TestEnum>("=test");
            Assert.NotNull(val.ExpressionText);
            Assert.Equal(default(TestEnum), val.Value);
            Assert.Equal(val.ToString(), JsonConvert.DeserializeObject <EnumExpression <TestEnum> >(JsonConvert.SerializeObject(val, settings: settings), settings: settings).ToString());
            (result, error) = val.TryGetValue(data);
            Assert.Equal(TestEnum.Two, result);
            Assert.Null(error);

            val = new EnumExpression <TestEnum>(TestEnum.Three);
            Assert.Null(val.ExpressionText);
            Assert.Equal(TestEnum.Three, val.Value);
            Assert.Equal(val.ToString(), JsonConvert.DeserializeObject <EnumExpression <TestEnum> >(JsonConvert.SerializeObject(val, settings: settings), settings: settings).ToString());
            (result, error) = val.TryGetValue(data);
            Assert.Equal(TestEnum.Three, result);
            Assert.Null(error);

            val = new EnumExpression <TestEnum>("garbage");
            Assert.NotNull(val.ExpressionText);
            Assert.Equal(default(TestEnum), val.Value);
            Assert.Equal(val.ToString(), JsonConvert.DeserializeObject <EnumExpression <TestEnum> >(JsonConvert.SerializeObject(val, settings: settings), settings: settings).ToString());
            (result, error) = val.TryGetValue(data);
            Assert.Equal(default(TestEnum), result);
            Assert.Null(error);

            val = new EnumExpression <TestEnum>("=sum(garbage)");
            Assert.NotNull(val.ExpressionText);
            Assert.Equal(default(TestEnum), val.Value);
            Assert.Equal(val.ToString(), JsonConvert.DeserializeObject <EnumExpression <TestEnum> >(JsonConvert.SerializeObject(val, settings: settings), settings: settings).ToString());
            (result, error) = val.TryGetValue(data);
            Assert.Equal(default(TestEnum), result);
            Assert.NotNull(error);
        }
 public override void WriteJson(JsonWriter writer, EnumExpression <T> value, JsonSerializer serializer)
 {
     if (value.ExpressionText != null)
     {
         serializer.Serialize(writer, value.ToString());
     }
     else
     {
         serializer.Serialize(writer, value.Value);
     }
 }