Пример #1
0
        public void Read_invalid()
        {
            // Arrange
            var json = "{ 'name': 'this JSON is invalid for this converter' }";

            var textReader    = new StringReader(json);
            var jsonReader    = new JsonTextReader(textReader);
            var objectType    = (Type)null;
            var existingValue = (object)null;
            var serializer    = new JsonSerializer();

            var converter = new CustomFieldsConverter();

            // Act
            jsonReader.Read();
            var result = converter.ReadJson(jsonReader, objectType, existingValue, serializer);

            // Assert
            result.ShouldNotBeNull();
            result.ShouldBeOfType <Field[]>();

            var resultAsArray = (Field[])result;

            resultAsArray.Length.ShouldBe(0);
        }
Пример #2
0
        public void Read_unknown_fieldtype()
        {
            // Arrange
            var json = @"[
				{ 'id':0, 'name':'field0', 'type':'__bogus__' }
			]"            ;

            var textReader    = new StringReader(json);
            var jsonReader    = new JsonTextReader(textReader);
            var objectType    = (Type)null;
            var existingValue = (object)null;
            var serializer    = new JsonSerializer();

            var converter = new CustomFieldsConverter();

            // Act
            jsonReader.Read();
            Should.Throw <Exception>(() => converter.ReadJson(jsonReader, objectType, existingValue, serializer))
            .Message.ShouldBe("__bogus__ is an unknown field type");
        }
Пример #3
0
        public void Read_multiple()
        {
            // Arrange
            var json = @"{
				'a': '2020-02-07T14:51:00Z',
				'b': 'abc123',
				'c': 123
			}"            ;

            var textReader    = new StringReader(json);
            var jsonReader    = new JsonTextReader(textReader);
            var objectType    = (Type)null;
            var existingValue = (object)null;
            var serializer    = new JsonSerializer();

            var converter = new CustomFieldsConverter();

            // Act
            jsonReader.Read();
            var result = converter.ReadJson(jsonReader, objectType, existingValue, serializer);

            // Assert
            result.ShouldNotBeNull();
            result.ShouldBeOfType <Field[]>();

            var resultAsArray = (Field[])result;

            resultAsArray.Length.ShouldBe(3);

            resultAsArray[0].Name.ShouldBe("a");
            resultAsArray[0].ShouldBeOfType <Field <DateTime> >();
            ((Field <DateTime>)resultAsArray[0]).Value.ShouldBe(new DateTime(2020, 2, 7, 14, 51, 0, DateTimeKind.Utc));

            resultAsArray[1].Name.ShouldBe("b");
            resultAsArray[1].ShouldBeOfType <Field <string> >();
            ((Field <string>)resultAsArray[1]).Value.ShouldBe("abc123");

            resultAsArray[2].Name.ShouldBe("c");
            resultAsArray[2].ShouldBeOfType <Field <long> >();
            ((Field <long>)resultAsArray[2]).Value.ShouldBe(123);
        }
Пример #4
0
        public void Read_multiple()
        {
            // Arrange
            var json = @"[
				{ 'id':0, 'name':'field0', 'type':'date', 'value':1490709300 },
				{ 'id':1, 'name':'field1', 'type':'date' },
				{ 'id':2, 'name':'field2', 'type':'text', 'value':'abc123' },
				{ 'id':3, 'name':'field3', 'type':'text' },
				{ 'id':4, 'name':'field4', 'type':'number', 'value':123 },
				{ 'id':5, 'name':'field5', 'type':'number' }
			]"            ;

            var textReader    = new StringReader(json);
            var jsonReader    = new JsonTextReader(textReader);
            var objectType    = (Type)null;
            var existingValue = (object)null;
            var serializer    = new JsonSerializer();

            var converter = new CustomFieldsConverter();

            // Act
            jsonReader.Read();
            var result = converter.ReadJson(jsonReader, objectType, existingValue, serializer);

            // Assert
            result.ShouldNotBeNull();
            result.ShouldBeOfType <Field[]>();

            var resultAsArray = (Field[])result;

            resultAsArray.Length.ShouldBe(6);

            resultAsArray[0].Id.ShouldBe(0);
            resultAsArray[0].Name.ShouldBe("field0");
            resultAsArray[0].ShouldBeOfType <Field <DateTime> >();
            ((Field <DateTime>)resultAsArray[0]).Value.ShouldBe(new DateTime(2017, 3, 28, 13, 55, 0, DateTimeKind.Utc));

            resultAsArray[1].Id.ShouldBe(1);
            resultAsArray[1].Name.ShouldBe("field1");
            resultAsArray[1].ShouldBeOfType <Field <DateTime?> >();
            ((Field <DateTime?>)resultAsArray[1]).Value.ShouldBeNull();

            resultAsArray[2].Id.ShouldBe(2);
            resultAsArray[2].Name.ShouldBe("field2");
            resultAsArray[2].ShouldBeOfType <Field <string> >();
            ((Field <string>)resultAsArray[2]).Value.ShouldBe("abc123");

            resultAsArray[3].Id.ShouldBe(3);
            resultAsArray[3].Name.ShouldBe("field3");
            resultAsArray[3].ShouldBeOfType <Field <string> >();
            ((Field <string>)resultAsArray[3]).Value.ShouldBeNull();

            resultAsArray[4].Id.ShouldBe(4);
            resultAsArray[4].Name.ShouldBe("field4");
            resultAsArray[4].ShouldBeOfType <Field <long> >();
            ((Field <long>)resultAsArray[4]).Value.ShouldBe(123);

            resultAsArray[5].Id.ShouldBe(5);
            resultAsArray[5].Name.ShouldBe("field5");
            resultAsArray[5].ShouldBeOfType <Field <long?> >();
            ((Field <long?>)resultAsArray[5]).Value.ShouldBeNull();
        }