Пример #1
0
 void when_initializing_new_plate_with_a_valid_type()
 {
     var typeMock = new Mock<IPlateType>();
     typeMock.SetupGet(format => format.Format).Returns(new Format(8, 12, PositionNamings.Default));
     it["type should have valid format"] = () => typeMock.Object.Format.IsValid().should_be_true();
     before = () => plate = new Plate(typeMock.Object);
     it["plate should exists"] = () => plate.should_not_be_null();
     it["contains the IPlateType"] = () => plate.Type.should_not_be_null();
     it["format exists"] = () => plate.Type.Format.should_not_be_null();
     it["width should be 12"] = () => plate.Width.should_be(12);
     it["height should be 8"] = () => plate.Height.should_be(8);
 }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Plate"/> class. Creates shallow copy of the plate.
 /// </summary>
 /// <param name="plate">The plate.</param>
 public Plate(Plate plate)
     : this(plate.Type, plate.Content)
 {
 }
Пример #3
0
        void given_valid_plate_object()
        {
            var typeMock = new Mock<IPlateType>();
            typeMock.SetupGet(format => format.Format).Returns(new Format(8, 12, PositionNamings.Default));

            context["when using copy constructor"] = () =>
            {
                before = () => plate = new Plate(typeMock.Object);
                it["should copy the plate with no problem"] = () => (new Plate(plate)).should_not_be_null();
                it["should not be the same"] = () => (new Plate(plate)).should_not_be_same(plate);
                it["copy should have the same Type"] = () => (new Plate(plate)).Type.should_be_same(plate.Type);
                //it["copy should have the same DataType"] = () => (new Plate(plate)).DataType.should_be_same(plate.DataType);
                it["copy should have the same Content"] = () => (new Plate(plate)).Content.should_be_same(plate.Content);
            };

            //context["when converting to typed plate"] = () =>
            //{
            //    before = () => plate = new Plate(typeMock.Object);
            //    it["should convert the plate if the type is correct"] = () => Plate.ToPlate<SomeData>(plate).should_not_be_null();
            //    it["should fail if type is wrong"] = expect<ArgumentException>(() => Plate.ToPlate<SomeOtherData>(plate));
            //    it["copy should have the same Type"] = () => (Plate.ToPlate<SomeData>(plate)).Type.should_be_same(plate.Type);
            //    it["copy should have the same DataType"] = () => (Plate.ToPlate<SomeData>(plate)).DataType.should_be_same(plate.DataType);
            //    it["copy should have the same Content"] = () => (Plate.ToPlate<SomeData>(plate)).Content.should_be_same(plate.Content);
            //};

            context["when serializing plate"] = () =>
            {
                // some real values
                before = () =>
                         {
                             plate = new Plate(new DefaultType());
                             plate[1].Value = "Serialize";
                         };
                var stream = new MemoryStream();
                var formatter = new BinaryFormatter();
                Plate copy = null;

                it["should serialize with no errors"] = () => formatter.Serialize(stream, plate);
                //it["should serialize even typed"] = () =>
                //{
                //    stream = new MemoryStream();
                //    formatter.Serialize(stream, typedPlate);
                //};
                it["should output something to the stream"] = () => stream.Length.should_be_greater_than(0);
                it["should deserialize with no errors"] = () =>
                {
                    stream.Position = 0;
                    copy = (Plate)formatter.Deserialize(stream);
                    copy.should_not_be_null();
                };
                it["should not be the same as copy"] = () => plate.should_not_be_same(copy);
                it["should have the same content as copy"] =
                    () => copy[1].Value.should_be(plate[1].Value);
            };

            context["when serializing to xml"] = () =>
            {
                before = () => plate = new Plate(new DefaultType());

                var stream = new MemoryStream();

                it["should save to xml with no errors"] = () => { plate.Save(stream); plate.Save("tmp.xml"); };
                it["should produce nonempty stream"] = () => stream.Length.should_be_greater_than(0);
                it["should deserialize from xml with no errors"] = () =>
                                                                   {
                                                                       stream.Position = 0;
                                                                       Plate.FromStream(stream).should_not_be_null();
                                                                   };
            };
        }
Пример #4
0
        public static Plate FromStream(Stream stream)
        {
            var reader = new XmlTextReader(stream);
            IPlateType plateType = null;
            DateTime created = DateTime.Today;
            DateTime lastChanged = DateTime.Now;
            var items = new List<string>();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "Type")
                    {
                        var typeName = reader.GetAttribute("FullName");
                        if (typeName != null)
                        {
                            var typeType = System.Type.GetType(typeName);
                            if (typeType != null) plateType = (IPlateType)Activator.CreateInstance(typeType);
                        }

                        if (plateType != null)
                        {
                            reader.Read();
                            plateType.FromXml(reader);
                        }
                    } else if (reader.Name == "Content")
                    {
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element && reader.Name == "Well")
                            {
                                items.Add(reader.ReadElementContentAsString());
                            } else if (reader.NodeType == XmlNodeType.EndElement)
                            {
                                reader.ReadEndElement();
                                break;
                            }
                        }
                    } else if (reader.Name == "Created")
                    {
                        created = reader.ReadElementContentAsDateTime();
                    } else if (reader.Name == "LastChanged")
                    {
                        lastChanged = reader.ReadElementContentAsDateTime();
                    }
                } else if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.Name == "Plate")
                    {
                        reader.ReadEndElement();
                        break;
                    }
                }
            }

            if (plateType != null)
            {
                var plate = new Plate(plateType);
                var format = plate.Type.Format;
                for (int i = 0; i < plate.Content.Length; i++)
                {
                    plate.Content[i] = new Well(items[i], i / format.Width, i % format.Width);
                }

                plate.Created = created;
                plate.LastChanged = lastChanged;

                return plate;
            }

            return null;
        }