Esempio n. 1
0
 public void TestUnwrapObjectPassingInvalidObject()
 {
     StringSchema schema = new StringSchema();
     IValueSchema<object> wrapper = schema.Wrap();
     Assert.IsNotNull(wrapper);
     Assert.AreEqual(typeof(string), wrapper.Type);
     wrapper.UnwrapRefType<ArrayList>();
 }
Esempio n. 2
0
 public void TestUnwrapStructPassingObjecSchema()
 {
     StringSchema schema = new StringSchema();
     IValueSchema<object> wrapper = schema.Wrap();
     Assert.IsNotNull(wrapper);
     Assert.AreEqual(typeof(string), wrapper.Type);
     wrapper.UnwrapValueType<int>();
 }
        public void NullableGuidValues(Guid value)
        {
            var schema = new StringSchema();

            var deserializer = DeserializerBuilder.BuildDeserializer <Guid?>(schema);
            var serializer   = SerializerBuilder.BuildSerializer <Guid>(schema);

            Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value)));
        }
        public void NullableDateTimeValues(DateTime value)
        {
            var schema = new StringSchema();

            var deserializer = DeserializerBuilder.BuildDeserializer <DateTime?>(schema);
            var serializer   = SerializerBuilder.BuildSerializer <DateTime>(schema);

            Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value)));
        }
        public void UriValues(Uri value)
        {
            var schema = new StringSchema();

            var deserializer = DeserializerBuilder.BuildDeserializer <Uri>(schema);
            var serializer   = SerializerBuilder.BuildSerializer <Uri>(schema);

            Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value)));
        }
        public void NullableTimeSpanValues(TimeSpan value)
        {
            var schema = new StringSchema();

            var deserializer = DeserializerBuilder.BuildDeserializer <TimeSpan?>(schema);
            var serializer   = SerializerBuilder.BuildSerializer <TimeSpan>(schema);

            Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value)));
        }
Esempio n. 7
0
        private async void ShowComplexWindow(object sender, RoutedEventArgs e)
        {
            var nameSchema = new StringSchema();
            var window     = new MaterialWindow(new MaterialDialog
            {
                Title = "Demo material forms",
                Form  = new MaterialForm
                {
                    new CommandSchema
                    {
                        Name        = "Login window",
                        CommandHint = "SHOW",
                        Callback    = ShowLogin
                    },
                    new CommandSchema
                    {
                        Name        = "Settings window",
                        CommandHint = "SHOW",
                        Callback    = ShowDemo(SettingsDialog)
                    },
                    new CommandSchema
                    {
                        Name        = "2x settings window (data binding)",
                        CommandHint = "SHOW",
                        Callback    = ShowDemo(SettingsDialog, true)
                    },
                    new CommandSchema
                    {
                        Name        = "Dialog inside window",
                        CommandHint = "SHOW",
                        Callback    = async args =>
                        {
                            await((WindowSession)args.Session).ShowDialog(new MaterialDialog
                            {
                                Message        = "Discard draft?",
                                PositiveAction = "DISCARD"
                            }, 250d);
                        }
                    },
                    new CommandSchema
                    {
                        Name        = "E-mail window",
                        CommandHint = "SHOW",
                        Callback    = ShowDemo(EmailDialog)
                    },
                    new CommandSchema
                    {
                        Name        = "Modal 2x schema (data binding)",
                        CommandHint = "SHOW",
                        Callback    = async args => await((WindowSession)args.Session).ShowDialog(new MaterialDialog(nameSchema, nameSchema), 250d)
                    }
                }
            });

            await window.Show();
        }
        public void UseFirstChildOfUnionSchema()
        {
            var @int    = new IntSchema();
            var @string = new StringSchema();
            var union   = new UnionSchema(new Schema[] { @int, @string });

            var defaultValue = new ObjectDefaultValue <int>(1, union);

            Assert.Equal(@int, defaultValue.Schema);
        }
Esempio n. 9
0
        public void TestStringSchemaMinMaxLengthValid()
        {
            StringSchema schema = new StringSchema {
                MinLength = 2, MaxLength = 5
            };

            schema.Validate("12");
            schema.Validate("123");
            schema.Validate("1234");
            schema.Validate("12345");
        }
Esempio n. 10
0
 public void TestUnwrapObject()
 {
     StringSchema schema = new StringSchema();
     IValueSchema<object> wrapper = schema.Wrap();
     Assert.IsNotNull(wrapper);
     Assert.AreEqual(typeof(string), wrapper.Type);
     IValueSchema<string> vs = wrapper.UnwrapRefType<string>();
     Assert.IsNotNull(vs);
     StringSchema stringSchema = vs as StringSchema;
     Assert.IsNotNull(stringSchema);
 }
Esempio n. 11
0
        public void TestStringSchemaNoNullAllowed()
        {
            StringSchema schema = new StringSchema {
                AllowNull = false
            };

            Assert.Throws <PropertyValidationException>(() =>
            {
                schema.Validate(null);
            });
        }
Esempio n. 12
0
        public void TestStringSchemaMinMaxLengthInvalidMax()
        {
            StringSchema schema = new StringSchema {
                MinLength = 2, MaxLength = 5
            };

            schema.Validate("12");
            schema.Validate("123");
            schema.Validate("1234");
            schema.Validate("12345");
            Assert.Throws <PropertyValidationException>(() => { schema.Validate("123456"); });
        }
Esempio n. 13
0
        public void GuidValues(Guid value)
        {
            var schema = new StringSchema()
            {
                LogicalType = new UuidLogicalType()
            };

            var deserializer = DeserializerBuilder.BuildDeserializer <Guid>(schema);
            var serializer   = SerializerBuilder.BuildSerializer <Guid>(schema);

            Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value)));
        }
Esempio n. 14
0
        public void TestStringSchemaAllowedValuesInvalid()
        {
            StringSchema schema = new StringSchema
            {
                PossibleValues = new[] { "one", "two", "three", "four" }
            };

            Assert.Throws <PropertyValidationException>(() =>
            {
                schema.Validate("five");
            });
        }
Esempio n. 15
0
        public void TestUnwrapObjectPassingInvalidObject()
        {
            StringSchema          schema  = new StringSchema();
            IValueSchema <object> wrapper = schema.Wrap();

            Assert.IsNotNull(wrapper);
            Assert.AreEqual(typeof(string), wrapper.Type);
            Assert.Throws <InvalidCastException>(() =>
            {
                wrapper.UnwrapRefType <ArrayList>();
            });
        }
        public void NullableDateTimeOffsetValues(DateTimeOffset value)
        {
            var schema = new StringSchema();

            var deserializer = DeserializerBuilder.BuildDeserializer <DateTimeOffset?>(schema);
            var serializer   = SerializerBuilder.BuildSerializer <DateTimeOffset>(schema);

            var decoded = deserializer.Deserialize(serializer.Serialize(value));

            // comparing two DateTimeOffsets doesn’t necessarily ensure that they’re identical:
            Assert.Equal(value.DateTime, decoded.Value.DateTime);
            Assert.Equal(value.Offset, decoded.Value.Offset);
        }
Esempio n. 17
0
        /// <summary>
        /// Builds a timestamp schema.
        /// </summary>
        /// <param name="resolution">
        /// A type resolution.
        /// </param>
        /// <param name="cache">
        /// A schema cache.
        /// </param>
        /// <returns>
        /// A <see cref="StringSchema" />.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the resolution is not a <see cref="TimestampResolution" />.
        /// </exception>
        public override Schema BuildSchema(TypeResolution resolution, IDictionary <Type, Schema> cache)
        {
            if (!(resolution is TimestampResolution timestamp))
            {
                throw new ArgumentException("The timestamp case can only be applied to timestamp resolutions.", nameof(resolution));
            }

            var schema = new StringSchema();

            cache.Add(timestamp.Type, schema);

            return(schema);
        }
Esempio n. 18
0
        /// <summary>
        /// Builds a URI schema.
        /// </summary>
        /// <param name="resolution">
        /// A type resolution.
        /// </param>
        /// <param name="cache">
        /// A schema cache.
        /// </param>
        /// <returns>
        /// A <see cref="StringSchema" />.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the resolution is not a <see cref="UriResolution" />.
        /// </exception>
        public override Schema BuildSchema(TypeResolution resolution, IDictionary <Type, Schema> cache)
        {
            if (!(resolution is UriResolution uri))
            {
                throw new ArgumentException("The URI case can only be applied to URI resolutions.", nameof(resolution));
            }

            var schema = new StringSchema();

            cache.Add(uri.Type, schema);

            return(schema);
        }
Esempio n. 19
0
        public void TestStringSchemaMaxLengthValid()
        {
            StringSchema schema = new StringSchema {
                MaxLength = 5
            };

            schema.Validate(string.Empty);
            schema.Validate("1");
            schema.Validate("12");
            schema.Validate("123");
            schema.Validate("1234");
            schema.Validate("12345");
        }
Esempio n. 20
0
        private async void ShowInput(object sender, RoutedEventArgs e)
        {
            StringSchema schema = new StringSchema {
                Name = "Name", IconKind = PackIconKind.Account
            };
            bool?result = await WindowFactory.FromSingleSchema("What is your name?", schema).Show();

            if (result == true)
            {
                string name = schema.Value;
                await WindowFactory.Alert($"Hello {name}!").Show();
            }
        }
Esempio n. 21
0
        public void UseFirstChildOfUnionSchema()
        {
            var document = JsonSerializer.Deserialize <JsonDocument>("1");
            var element  = document.RootElement;

            var @int    = new IntSchema();
            var @string = new StringSchema();
            var union   = new UnionSchema(new Schema[] { @int, @string });

            var defaultValue = new JsonDefaultValue(element, union);

            Assert.Equal(@int, defaultValue.Schema);
        }
        public void TestStringSerializeProperties()
        {
            JsonPropertySerializer serializer = new JsonPropertySerializer(new PropertySchemaFactory());
            StringSchema           schema     = new StringSchema {
                MaxLength = 100, MinLength = 1, DefaultValue = "hello"
            };
            PropertyElement element = serializer.Serialize("hello", schema.Wrap());

            Assert.IsNotNull(element);
            Assert.AreEqual(typeof(StringSchema).FullName, element.Schema.SchemaType);
            Assert.AreEqual(SerializationTypeHint.String, element.SerializationHint);
            Assert.AreEqual("hello", element.Value);
        }
Esempio n. 23
0
        public void TestUnwrapObject()
        {
            StringSchema          schema  = new StringSchema();
            IValueSchema <object> wrapper = schema.Wrap();

            Assert.IsNotNull(wrapper);
            Assert.AreEqual(typeof(string), wrapper.Type);
            IValueSchema <string> vs = wrapper.UnwrapRefType <string>();

            Assert.IsNotNull(vs);
            StringSchema stringSchema = vs as StringSchema;

            Assert.IsNotNull(stringSchema);
        }
Esempio n. 24
0
 public void TestWrapObject()
 {
     StringSchema  schema=new StringSchema{AllowNull = true,DefaultValue = "Hello",PossibleValues = new[]{"one","two","Hello","Goodbye"}};
     IValueSchema<object> wrapper = schema.Wrap();
     Assert.IsNotNull(wrapper);
     Assert.AreEqual(typeof(string),wrapper.Type);
     Assert.IsTrue(wrapper.AllowNull);
     Assert.AreEqual("Hello",wrapper.DefaultValue);
     Assert.IsNotNull(wrapper.PossibleValues);
     Assert.AreEqual(4,wrapper.PossibleValues.Count());
     Assert.AreEqual("one",wrapper.PossibleValues.ElementAt(0));
     Assert.AreEqual("two", wrapper.PossibleValues.ElementAt(1));
     Assert.AreEqual("Hello", wrapper.PossibleValues.ElementAt(2));
     Assert.AreEqual("Goodbye", wrapper.PossibleValues.ElementAt(3));
 }
 public void TestSerializeStringSchema()
 {
     IValueSchema<string> schema = new StringSchema {AllowNull = true,
         DefaultValue = "abc",
         MaxLength = 100,
         MinLength = 2,
         PossibleValues = new[]{"abc","def"}
     };
     JsonSchemaSerializationVisitor visitor=new JsonSchemaSerializationVisitor();
     schema.Accept(visitor);
     Assert.AreEqual(typeof(string),visitor.ValueType);
     Assert.AreEqual(typeof(StringSchema),visitor.SchemaType);
     Assert.IsNotNull(visitor.JsonValue);
     Assert.AreNotEqual(0,visitor.JsonValue.Length);
 }
Esempio n. 26
0
        public void DynamicDateTimeValues(dynamic value)
        {
            var schema = new StringSchema();

            var deserialize = deserializerBuilder.BuildDelegate <dynamic>(schema);
            var serialize   = serializerBuilder.BuildDelegate <dynamic>(schema);

            using (stream)
            {
                serialize(value, new BinaryWriter(stream));
            }

            var reader = new BinaryReader(stream.ToArray());

            Assert.Equal(value.ToString("O"), deserialize(ref reader));
        }
Esempio n. 27
0
        public void UriValues(Uri value)
        {
            var schema = new StringSchema();

            var deserialize = deserializerBuilder.BuildDelegate <Uri>(schema);
            var serialize   = serializerBuilder.BuildDelegate <Uri>(schema);

            using (stream)
            {
                serialize(value, new BinaryWriter(stream));
            }

            var reader = new BinaryReader(stream.ToArray());

            Assert.Equal(value, deserialize(ref reader));
        }
Esempio n. 28
0
        public void NullableTimeSpanValues(TimeSpan value)
        {
            var schema = new StringSchema();

            var deserialize = deserializerBuilder.BuildDelegate <TimeSpan?>(schema);
            var serialize   = serializerBuilder.BuildDelegate <TimeSpan>(schema);

            using (stream)
            {
                serialize(value, new BinaryWriter(stream));
            }

            var reader = new BinaryReader(stream.ToArray());

            Assert.Equal(value, deserialize(ref reader));
        }
Esempio n. 29
0
        /// <summary>
        /// Builds a UUID schema.
        /// </summary>
        /// <param name="resolution">
        /// A type resolution.
        /// </param>
        /// <param name="cache">
        /// A schema cache.
        /// </param>
        /// <returns>
        /// A <see cref="StringSchema" />.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the resolution is not a <see cref="UuidResolution" />.
        /// </exception>
        public override Schema BuildSchema(TypeResolution resolution, IDictionary <Type, Schema> cache)
        {
            if (!(resolution is UuidResolution uuid))
            {
                throw new ArgumentException("The UUID case can only be applied to UUID resolutions.", nameof(resolution));
            }

            var schema = new StringSchema()
            {
                LogicalType = new UuidLogicalType()
            };

            cache.Add(uuid.Type, schema);

            return(schema);
        }
Esempio n. 30
0
        public void DateTimeValues(DateTime value)
        {
            var schema = new StringSchema();

            var deserialize = deserializerBuilder.BuildDelegate <DateTime>(schema);
            var serialize   = serializerBuilder.BuildDelegate <DateTime>(schema);

            using (stream)
            {
                serialize(value, new Utf8JsonWriter(stream));
            }

            var reader = new Utf8JsonReader(stream.ToArray());

            Assert.Equal(value, deserialize(ref reader));
        }
 /// <summary>
 /// Writes an <see cref="PrimitiveSchema" />.
 /// </summary>
 /// <inheritdoc />
 public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
 {
     if (schema is PrimitiveSchema primitiveSchema)
     {
         json.WriteStringValue(primitiveSchema switch
         {
             BooleanSchema _ => JsonSchemaToken.Boolean,
             BytesSchema _ => JsonSchemaToken.Bytes,
             DoubleSchema _ => JsonSchemaToken.Double,
             FloatSchema _ => JsonSchemaToken.Float,
             IntSchema _ => JsonSchemaToken.Int,
             LongSchema _ => JsonSchemaToken.Long,
             NullSchema _ => JsonSchemaToken.Null,
             StringSchema _ => JsonSchemaToken.String,
             _ => throw new UnsupportedSchemaException(schema, $"Unknown primitive schema {schema}."),
         });
Esempio n. 32
0
        public void DynamicTimeSpanValues(TimeSpan value)
        {
            var schema = new StringSchema();

            var deserialize = deserializerBuilder.BuildDelegate <dynamic>(schema);
            var serialize   = serializerBuilder.BuildDelegate <dynamic>(schema);

            using (stream)
            {
                serialize(value, new Utf8JsonWriter(stream));
            }

            var reader = new Utf8JsonReader(stream.ToArray());

            Assert.Equal(XmlConvert.ToString(value), deserialize(ref reader));
        }
Esempio n. 33
0
        public void TestStringSchemaEquality()
        {
            StringSchema s1 = new StringSchema();
            StringSchema s2 = new StringSchema();

            Assert.IsTrue(s1.Equals(s2));
            Assert.IsTrue(s2.Equals(s1));
            Assert.AreEqual(s1.GetHashCode(), s2.GetHashCode());
            s1 = new StringSchema
            {
                DefaultValue   = "two",
                MaxLength      = 100,
                MinLength      = 1,
                PossibleValues = new[] { "one", "two", "three", "four" }
            };
            s2 = new StringSchema
            {
                DefaultValue   = "two",
                MaxLength      = 100,
                MinLength      = 1,
                PossibleValues = new[] { "one", "two", "three", "four" }
            };

            Assert.IsTrue(s1.Equals(s2));
            Assert.IsTrue(s2.Equals(s1));
            Assert.AreEqual(s1.GetHashCode(), s2.GetHashCode());

            s2 = new StringSchema
            {
                DefaultValue   = "two",
                MaxLength      = 100,
                MinLength      = 1,
                PossibleValues = new[] { "one", "2", "three", "four" }
            };
            Assert.IsFalse(s1.Equals(s2));
            Assert.IsFalse(s2.Equals(s1));
            Assert.AreNotEqual(s1.GetHashCode(), s2.GetHashCode());

            s2 = new StringSchema {
                AllowNull = true
            };

            Assert.IsFalse(s1.Equals(s2));
            Assert.IsFalse(s2.Equals(s1));
            Assert.AreNotEqual(s1.GetHashCode(), s2.GetHashCode());
        }
        public void TestSerializeStringSchema()
        {
            IValueSchema <string> schema = new StringSchema {
                AllowNull      = true,
                DefaultValue   = "abc",
                MaxLength      = 100,
                MinLength      = 2,
                PossibleValues = new[] { "abc", "def" }
            };
            SchemaJsonSerializationVisitor visitor = new SchemaJsonSerializationVisitor();

            schema.Accept(visitor);
            Assert.AreEqual(typeof(string), visitor.ValueType);
            Assert.AreEqual(typeof(StringSchema), visitor.SchemaType);
            Assert.IsNotNull(visitor.JsonValue);
            Assert.AreNotEqual(0, visitor.JsonValue.Length);
        }
        public void NonUnionReferenceToUnionTest()
        {
            var writerSchema = new StringSchema();
            var readerSchema = new UnionSchema(new FloatSchema(), new StringSchema(), new BytesSchema());
            var writer       = new DatumWriter <string>(writerSchema);
            var reader       = new DatumReader <object>(readerSchema, writerSchema);

            var expectedValue = "Test String";

            using (var stream = new MemoryStream())
                using (var encoder = new BinaryEncoder(stream))
                    using (var decoder = new BinaryDecoder(stream))
                    {
                        writer.Write(encoder, expectedValue);
                        stream.Seek(0, SeekOrigin.Begin);
                        Assert.AreEqual(expectedValue, reader.Read(decoder));
                    }
        }
 public void TestStringSerializeProperties()
 {
     JsonPropertySerializer serializer=new JsonPropertySerializer(new PropertySchemaFactory());
     StringSchema schema=new StringSchema{MaxLength = 100,MinLength = 1, DefaultValue = "hello"};
     PropertyElement element = serializer.Serialize("key", "hello", schema.Wrap());
     Assert.IsNotNull(element);
     Assert.AreEqual(typeof(StringSchema).FullName,element.SchemaType);
     Assert.AreEqual(SerializationValueType.String,element.SerializationValueType);
     Assert.AreEqual("hello",element.ValString);
 }
        public void TestStringDeSerializeProperties()
        {
            JsonPropertySerializer serializer = new JsonPropertySerializer(new PropertySchemaFactory());
            StringSchema schema = new StringSchema { MaxLength = 100, MinLength = 1, DefaultValue = "hello" };
            PropertyElement element = serializer.Serialize("key", "hello", schema.Wrap());
            Assert.IsNotNull(element);
            Assert.AreEqual(typeof(StringSchema).FullName, element.SchemaType);
            Assert.AreEqual(SerializationValueType.String, element.SerializationValueType);
            Assert.AreEqual("hello", element.ValString);

            IValueSchema<object> vs;
            object value = serializer.Deserialize(element, out vs);
            Assert.IsNotNull(vs);
            Assert.IsNotNull(value);
            IValueSchema<string> strSchema = vs.UnwrapRefType<string>();
            Assert.IsNotNull(strSchema);
        }