Example #1
0
        public void WithLowLevelConverter_GivenDoubleType_ThenValidStringsCanBeConvertedToNumber()
        {
            var doubleDescriptor = new ValueDescriptor();

            Assert.Equal(1.0, ValueConversions.ToNumber(doubleDescriptor, CultureInfo.InvariantCulture, "1.0"));
            Assert.Equal(1.0, ValueConversions.ToNumber(doubleDescriptor, CultureInfo.InvariantCulture, "1e0"));
        }
Example #2
0
        public void GivenValueDescriptor_ThenItCanBeComparedWihtOthers()
        {
            var value1 = new ValueDescriptor {
                Reference = "a"
            };
            var value2 = new ValueDescriptor {
                Reference = "a"
            };
            var value3 = new ValueDescriptor {
                Reference = "a"
            };

            Assert.Equal(value1, value2);
            Assert.Equal(value2, value3);
            Assert.Equal(value3, value1);

            Assert.True(value1.Equals(value1));
            Assert.True(value1.Equals(value2));
            Assert.True(value1.Equals((object)value2));
            Assert.False(value1.Equals(null));
            Assert.False(value1.Equals(new object()));

            // ValueDescriptor is a mutable object, I prefer to do not
            // override == and != ooperators and keep original behavior (reference comparison)
            Assert.True(value1 != value2);
            Assert.False(value1 == null);
        }
Example #3
0
        public void WithLowLevelConverter_GivenDoubleType_ThenAnyFloatingPointOrIntegerCanBeConvertedToNumber()
        {
            var doubleDescriptor = new ValueDescriptor();

            Assert.Equal(1.0, ValueConversions.ToNumber(doubleDescriptor, CultureInfo.InvariantCulture, 1.0));
            Assert.Equal(1.0, ValueConversions.ToNumber(doubleDescriptor, CultureInfo.InvariantCulture, 1.0f));
            Assert.Equal(1.0, ValueConversions.ToNumber(doubleDescriptor, CultureInfo.InvariantCulture, 1));
        }
        /// <summary>
        /// Fills the font, color and (later!) list hashtables so they can be rendered and used by other renderers.
        /// </summary>
        private void CollectTables(DocumentObject dom)
        {
            ValueDescriptorCollection vds = Meta.GetMeta(dom).ValueDescriptors;
            int count = vds.Count;

            for (int idx = 0; idx < count; idx++)
            {
                ValueDescriptor vd = vds[idx];
                if (!vd.IsRefOnly && !vd.IsNull(dom))
                {
                    if (vd.ValueType == typeof(Color))
                    {
                        Color clr = (Color)vd.GetValue(dom, GV.ReadWrite);
                        clr = clr.GetMixedTransparencyColor();
                        if (!this.colorList.Contains(clr))
                        {
                            this.colorList.Add(clr);
                        }
                    }
                    else if (vd.ValueType == typeof(Font))
                    {
                        Font fnt = vd.GetValue(dom, GV.ReadWrite) as Font; //ReadOnly
                        if (!fnt.IsNull("Name") && !this.fontList.Contains(fnt.Name))
                        {
                            this.fontList.Add(fnt.Name);
                        }
                    }
                    else if (vd.ValueType == typeof(ListInfo))
                    {
                        ListInfo lst = vd.GetValue(dom, GV.ReadWrite) as ListInfo; //ReadOnly
                        if (!this.listList.Contains(lst))
                        {
                            this.listList.Add(lst);
                        }
                    }
                    if (typeof(DocumentObject).IsAssignableFrom(vd.ValueType))
                    {
                        CollectTables(vd.GetValue(dom, GV.ReadWrite) as DocumentObject); //ReadOnly
                        if (typeof(DocumentObjectCollection).IsAssignableFrom(vd.ValueType))
                        {
                            DocumentObjectCollection coll = vd.GetValue(dom, GV.ReadWrite) as DocumentObjectCollection; //ReadOnly
                            if (coll != null)
                            {
                                foreach (DocumentObject obj in coll)
                                {
                                    //In SeriesCollection kann null vorkommen.
                                    if (obj != null)
                                    {
                                        CollectTables(obj);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Example #5
0
        public void WithLowLevelConverter_GivenStringType_CannotConvertAnyValueToNumber()
        {
            var stringDescriptor = new ValueDescriptor {
                Type = TypeOfValue.String
            };

            // Note that if fails even if string is a valid number!
            Assert.Equal(null, ValueConversions.ToNumber(stringDescriptor, CultureInfo.InvariantCulture, "test"));
            Assert.Equal(null, ValueConversions.ToNumber(stringDescriptor, CultureInfo.InvariantCulture, "1.0"));
        }
Example #6
0
        /// <summary>
        /// Creates an object using the default constructor.
        /// </summary>
        public object CreateValue(string name)
        {
            ValueDescriptor vd = Meta[name];

            if (vd != null)
            {
                return(vd.CreateValue());
            }
            return(null);
        }
        public override int GetHashCode()
        {
            unchecked {
                var hashCode = EqualityComparer <T> .Default.GetHashCode(Value);

                hashCode = (hashCode * 397) ^ (ValueDescriptor?.GetHashCode() ?? 0);
                hashCode = (hashCode * 397) ^ (int)Source;
                hashCode = (hashCode * 397) ^ (CustomExpression?.GetHashCode() ?? 0);
                return(hashCode);
            }
        }
        public static ProtocolDescriptor CreateTest()
        {
            // This simple protocol consists of one default section
            // with all requried fields. Note that NCalc expression syntax
            // isn't enforced by ProtocolDescriptor itself.
            var section = new SectionDescriptor
            {
                Name = PhysicalDataSectionName
            };

            // ...with a numeric weight field
            section.Values.Add(new ValueDescriptor
            {
                Reference = WeightFieldId,
                Name      = WeightFieldName,
            });

            // ...with a numeri height field
            section.Values.Add(new ValueDescriptor
            {
                Reference = HeightFieldId,
                Name      = HeightFieldName,
            });

            // ...and a gender at birth list field
            var genderField = new ValueDescriptor
            {
                Reference = GenderFieldId,
                Name      = GenderFieldName,
                DefaultValueExpression = "0",
            };

            genderField.AvailableValues.Add(new ListItem {
                Name = "Male", Value = "0"
            });
            genderField.AvailableValues.Add(new ListItem {
                Name = "Female", Value = "1"
            });
            section.Values.Add(genderField);

            // Just create and return this simple protocol, caller will decorate those fields
            // according to what must be tested.
            var protocol = new ProtocolDescriptor
            {
                Reference = BmiProtocolReference,
                Name      = BmiProtocolName
            };

            protocol.Sections.Add(section);

            return(protocol);
        }
Example #9
0
        public void WithLowLevelConverter_GivenBooleanType_ThenAnyValueCanBeConvertedToBoolean()
        {
            var boolDescriptor = new ValueDescriptor {
                Type = TypeOfValue.Boolean
            };

            Assert.Equal(1.0, ValueConversions.ToNumber(boolDescriptor, CultureInfo.InvariantCulture, 10));
            Assert.Equal(0.0, ValueConversions.ToNumber(boolDescriptor, CultureInfo.InvariantCulture, 0));
            Assert.Equal(0.0, ValueConversions.ToNumber(boolDescriptor, CultureInfo.InvariantCulture, null));
            Assert.Equal(1.0, ValueConversions.ToNumber(boolDescriptor, CultureInfo.InvariantCulture, true));
            Assert.Equal(0.0, ValueConversions.ToNumber(boolDescriptor, CultureInfo.InvariantCulture, false));
            Assert.Equal(1.0, ValueConversions.ToNumber(boolDescriptor, CultureInfo.InvariantCulture, "true"));
            Assert.Equal(1.0, ValueConversions.ToNumber(boolDescriptor, CultureInfo.InvariantCulture, "10"));
        }
Example #10
0
        public void GivenEditableValueWithoutReferenceId_ThenAddIssue()
        {
            var protocol = ProtocolFactory.CreateTest();

            Assert.Empty(protocol.ValidateModel());

            // Calculated values cannot have a default value
            var value = new ValueDescriptor {
                Reference = "", Name = "Test"
            };

            protocol.Sections.First().Values.Add(value);

            Assert.Equal(1, protocol.ValidateModel().Count());
        }
Example #11
0
        public void GivenCalculatedValueWithDefault_ThenAddIssue()
        {
            var protocol = ProtocolFactory.CreateTest();

            Assert.Empty(protocol.ValidateModel());

            // Calculated values cannot have a default value
            var value = new ValueDescriptor {
                Reference = "Test", Name = "Test"
            };

            value.CalculatedValueExpression = "[Weight] / [Height]";
            value.DefaultValueExpression    = "10";
            protocol.Sections.First().Values.Add(value);

            Assert.Equal(1, protocol.ValidateModel().Count());
        }
Example #12
0
        public void GivenListOfAvailableValuesForCalculatedField_ThenAddIssue()
        {
            var protocol = ProtocolFactory.CreateTest();

            Assert.Empty(protocol.ValidateModel());

            // Calculated values cannot have a list of available values
            var value = new ValueDescriptor {
                Reference = "Test", Name = "Test"
            };

            value.CalculatedValueExpression = "[Weight] / [Height]";
            value.AvailableValues.Add(new ListItem {
                Name = "1", Value = "1"
            });
            protocol.Sections.First().Values.Add(value);

            Assert.Equal(1, protocol.ValidateModel().Count());
        }
Example #13
0
        public void GivenStringFieldAggregatedWithAverage_ThenAddIssue()
        {
            var protocol = ProtocolFactory.CreateTest();

            Assert.Empty(protocol.ValidateModel());

            var value = new ValueDescriptor {
                Reference = "Name", Name = "Name"
            };

            value.Type = TypeOfValue.String;
            value.PreferredAggregation = AggregationMode.Average;
            protocol.Sections.First().Values.Add(value);

            // Strings cannot be averaged
            Assert.Equal(1, protocol.ValidateModel().Count());

            // ...unless we supply a custom transformation
            value.TransformationForAggregationExpression = "length(this)"; // Code is not really parsed during model validation...
            Assert.Equal(0, protocol.ValidateModel().Count());
        }
Example #14
0
        public void WhenStoreValidValue_ThenItCanBeRead()
        {
            var protocol = ProtocolFactory.CreateTest();
            var a        = new ValueDescriptor {
                Reference = "a"
            };
            var b = new ValueDescriptor {
                Reference = "b"
            };

            protocol.Sections.First().Values.Add(a);
            protocol.Sections.First().Values.Add(b);

            var dataset = new DataSet(protocol);

            dataset.AddValue(a, 1.0);
            dataset.AddValue("b", 2.0);
            dataset.Calculate();

            Assert.Equal(1.0, Convert.ToDouble(dataset["a"].Value));
            Assert.Equal(2.0, Convert.ToDouble(dataset["b"].Value));
        }
Example #15
0
        private ValueDescriptor[] ReadValueDescriptors(ref int offset)
        {
            ValueDescriptor[] descriptors = new ValueDescriptor[BitConverter.ToInt32(this.buffer, offset)];
            offset += 4;

            int valueOffset = descriptors.Length * 4 + offset;

            for (int index = 0; index < descriptors.Length; index++)
            {
                ValueDescriptor descriptor = descriptors[index] = new ValueDescriptor();
                descriptor.Size = BitConverter.ToUInt16(this.buffer, offset);
                offset         += 2;

                descriptor.Type = (BinaryValueType)this.buffer[offset];
                ////fourth byte should be null
                offset += 2;

                ////keep track of where the value should be
                descriptor.Offset = valueOffset;
                valueOffset      += descriptor.Size;
            }

            return(descriptors);
        }
            /// <summary>
            /// Decodes self and element types
            /// </summary>
            internal Type DecodeType(BinaryReader br, DeserializationManager manager, bool allowOpenTypes = false)
            {
                DataTypeDescriptor existingDescriptor;

                // Simple or impure type. Handling generics occurs in recursive ReadType if needed.
                if (CollectionDataType == DataTypes.Null)
                {
                    Type       = GetElementType(ElementDataType, br, manager, allowOpenTypes, out existingDescriptor);
                    StoredType = existingDescriptor?.StoredType;
                    return(Type);
                }

                Type result;

                // generic type definition
                if (ElementDataType == DataTypes.GenericTypeDefinition)
                {
                    result = GetCollectionType(CollectionDataType);
                }
                else
                {
                    // simple collection element or dictionary key: Since in DataTypes the element is encoded together with the collection
                    // the element type descriptor was not created in the constructor. We create it now.
                    if (ElementDataType != DataTypes.Null)
                    {
                        ElementDescriptor            = new DataTypeDescriptor(ElementDataType, GetElementType(ElementDataType, br, manager, allowOpenTypes, out existingDescriptor), this);
                        ElementDescriptor.StoredType = existingDescriptor?.StoredType;
                    }
                    // complex element type: recursive decoding
                    else
                    {
                        ElementDescriptor.DecodeType(br, manager, allowOpenTypes);
                    }

                    // Dictionary TValue
                    if (IsDictionary)
                    {
                        ValueDescriptor.DecodeType(br, manager, allowOpenTypes);
                    }

                    if (IsArray)
                    {
                        // 0 means zero based 1D array
                        Rank = br.ReadByte();
                        return(Type = Rank == 0
                            ? ElementDescriptor.Type.MakeArrayType()
                            : ElementDescriptor.Type.MakeArrayType(Rank));
                    }

                    result = GetCollectionType(CollectionDataType);
                    bool isNullable = IsNullable = result.IsNullable();
                    if (!result.ContainsGenericParameters)
                    {
                        return(Type = result);
                    }

                    Type typeDef = isNullable ? result.GetGenericArguments()[0] : result;
                    result = typeDef.GetGenericArguments().Length == 1
                        ? typeDef.GetGenericType(ElementDescriptor.Type)
                        : typeDef.GetGenericType(ElementDescriptor.Type, ValueDescriptor.Type);
                    result = isNullable ? Reflector.NullableType.GetGenericType(result) : result;
                }

                if (result.IsGenericTypeDefinition)
                {
                    result = manager.HandleGenericTypeDef(br, new DataTypeDescriptor(result), allowOpenTypes, false).Type;
                }
                return(Type = result);
            }
Example #17
0
 private static Completion CreateAttributeValueCompletion(ValueDescriptor value)
 {
     Debug.Assert(value != null, "value");
     return(new Completion(value.DisplayName, value.DisplayName, value.Description, null, null));
 }