static void DescribeMethod(StringBuilder sb, Expression <Func <MethodInfo> > fmi)
        {
            var prop = fmi.Body as MemberExpression;

            sb.AppendLine("## " + prop.Member.Name);

            var mi = fmi.Compile().Invoke();

            if (mi == null)
            {
                sb.AppendLine("`null`");
            }
            else
            {
                sb.AppendLine("Return Type: " + SchemaTest.HumanName(mi.ReturnType));
                sb.AppendLine("IsVirtual: " + mi.IsVirtual);
                sb.AppendLine("Declaring Type: " + SchemaTest.HumanName(mi.DeclaringType));
                foreach (var arg in mi.GetParameters())
                {
                    sb.AppendLine(String.Format("{0}: {1}", arg.Name, SchemaTest.HumanName(arg.ParameterType)));
                }
            }

            sb.AppendLine();
        }
        public void FindMethods()
        {
            var sb = new StringBuilder();

            foreach (var type in new[]
            {
                typeof(List <int>),
                typeof(IEnumerable <int>),
                typeof(ExplicitEnumerableBadCount),
                typeof(ExplicitReadOnlyCollection),
                typeof(ExplicitReadOnlyCollectionImplicitCount),
                typeof(ExplicitReadOnlyCollectionPublicEnumerator),
                typeof(ICollection <short>),
                typeof(IReadOnlyCollection <ushort>),
                typeof(IReadOnlyList <long>),
                typeof(IList <ulong>),
                this.GetType(),                 // throw in something non enumerable
            })
            {
                var methods = EnumerableInfo.FindMethods(type);
                sb.AppendLine("# " + SchemaTest.HumanName(type));
                DescribeMethod(sb, () => methods.GetEnumerator);
                DescribeMethod(sb, () => methods.MoveNext);
                DescribeMethod(sb, () => methods.get_Current);
                DescribeMethod(sb, () => methods.get_Count);
                DescribeMethod(sb, () => methods.Dispose);

                sb.AppendLine();
            }

            ApprovalTests.Approvals.Verify(sb.ToString());
        }
示例#3
0
            public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
            {
                if (value == null)
                {
                    writer.WriteNull();
                    return;
                }

                writer.WriteValue(SchemaTest.HumanName((Type)value));
            }
        unsafe static int TrySerializer <T>(StringBuilder sb, params T[] samples)
        {
            int failures = 0;

            sb.AppendLine();
            sb.AppendLine("# " + SchemaTest.HumanName(typeof(T)));
            sb.AppendLine();

            var    emit = DelegateBuilder.CreateStream <T>(Schema.Reflect(typeof(T)));
            string instructions;
            var    del = (Serializer.WriteToStream <T>)emit.CreateDelegate(typeof(Serializer.WriteToStream <T>), out instructions);

            sb.AppendLine(emit.Instructions());
            sb.AppendLine();

            var ms     = new MemoryStream();
            var buffer = new byte[64];

            for (int i = 0; i < samples.Length; i++)
            {
                ms.Position = 0;
                fixed(byte *ptr = buffer)
                {
                    del.Invoke(ref samples[i], ptr, ms, buffer);
                }

                var newt = DelegateBuilderTest.GetNewtonsoft(samples[i]);
                var mine = ms.GetBuffer().Take((int)ms.Position);

                sb.AppendLine();
                sb.AppendLine("## Newtonsoft");
                Util.Hex.Dump(sb, newt);
                sb.AppendLine();

                sb.AppendLine();
                sb.AppendLine("## Cameronism.Json");
                Util.Hex.Dump(sb, mine);
                sb.AppendLine();
                sb.AppendLine();

                bool equal = newt.SequenceEqual(mine);
                sb.AppendLine("Equal: " + equal);
                sb.AppendLine();

                if (!equal)
                {
                    failures++;
                }
            }

            return(failures);
        }
        static bool Describe <T>(StringBuilder sb)
        {
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("# " + SchemaTest.HumanName(typeof(T)));

            var emit = Cameronism.Json.DelegateBuilder.CreatePointer <T>(Cameronism.Json.Schema.Reflect(typeof(T)));

            string instructions;

            try
            {
                var writer = emit.CreateDelegate <Serializer.WriteToPointer <T> >(out instructions);
                sb.AppendLine(instructions);
                return(true);
            }
            catch (Sigil.SigilVerificationException sve)
            {
                sb.AppendLine("## failed");
                sb.AppendLine(sve.ToString());

                return(false);
            }
        }
        public void ApproveConstants()
        {
            var methods = this.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);

            var sb = new StringBuilder();
            int failedEqualCount = 0;

            foreach (var mi in methods)
            {
                if (!mi.IsSpecialName || !mi.Name.StartsWith("get_"))
                {
                    continue;
                }

                var shouldBeConstant = mi.Name[4] == 'C';
                var actual           = mi.Invoke(this, null);
                var nJSON            = Newtonsoft.Json.JsonConvert.SerializeObject(actual);
                var constant         = ConstantMethods.TryGetJson(mi, this.GetType());

                var passed = shouldBeConstant ?
                             String.Equals(nJSON, constant, StringComparison.Ordinal) :
                             constant == null;

                sb.AppendLine("# " + mi.Name.Substring(4) + " " + SchemaTest.HumanName(mi.ReturnType));
                sb.AppendLine();
                sb.AppendLine("Expected constant: " + shouldBeConstant);
                if (!passed)
                {
                    if (ToleratedFailure(mi.ReturnType, nJSON))
                    {
                        sb.AppendLine("** SKIPPED ** - fix this");
                    }
                    else
                    {
                        failedEqualCount++;
                        sb.AppendLine("** FAILED **");
                    }
                }

                sb.AppendLine();
                sb.AppendLine("## Newtonsoft");
                Util.Hex.Dump(sb, Encoding.UTF8.GetBytes(nJSON));
                sb.AppendLine();

                sb.AppendLine();
                sb.AppendLine("## Cameronism.Json");
                if (constant != null)
                {
                    Util.Hex.Dump(sb, Encoding.UTF8.GetBytes(constant));
                }
                else
                {
                    sb.AppendLine();
                    sb.AppendLine("`null`");
                }
                sb.AppendLine();
                sb.AppendLine();
                sb.AppendLine();
            }

            ApprovalTests.Approvals.Verify(sb.ToString());
            Assert.True(failedEqualCount == 0, "Look at the approval, " + failedEqualCount + " failed");
        }
        internal static int SerializeValues <T>(StringBuilder sb, byte[] buffer, T[] values, bool approveIL)
        {
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine("# " + SchemaTest.HumanName(typeof(T)));

            Cameronism.Json.Serializer.WriteToPointer <T> writer;
            string instructions;

            try
            {
                var emit = Cameronism.Json.DelegateBuilder.CreatePointer <T>(Cameronism.Json.Schema.Reflect(typeof(T)));
                writer = emit.CreateDelegate <Serializer.WriteToPointer <T> >(out instructions);
            }
            catch (Sigil.SigilVerificationException sve)
            {
                sb.AppendLine("## failed");
                sb.AppendLine(sve.ToString());

                return(values.Length);
            }

            if (approveIL)
            {
                sb.AppendLine();
                sb.AppendLine("## il");
                sb.AppendLine(instructions);
                sb.AppendLine();
            }

            int failed = 0;

            for (int i = 0; i < values.Length; i++)
            {
                for (int j = 0; j < buffer.Length; j++)
                {
                    buffer[j] = 0;
                }

                var value = values[i];
                int myResult;
                var mine       = GetBytes(value, writer, buffer, out myResult);
                var newtonsoft = GetNewtonsoft(value);

                sb.AppendLine();
                sb.AppendLine();
                sb.AppendLine("## Newtonsoft " + i);
                Hex.Dump(sb, newtonsoft);

                sb.AppendLine();
                sb.AppendLine();
                sb.AppendLine("## Cameronism.Json " + i);
                Hex.Dump(sb, mine);

                sb.AppendLine();
                sb.AppendLine();
                bool equal = mine != null && Enumerable.SequenceEqual(newtonsoft, mine);
                if (equal)
                {
                    AssertStreamVersion(value, newtonsoft);
                }
                sb.AppendFormat("### Equal: {0}", equal);
                if (mine != null)
                {
                    int diff = CompareNewtonsoft.IndexOfDiff(newtonsoft, mine);
                    if (diff != -1)
                    {
                        sb.AppendFormat("### Difference at: {0:x}", diff);
                    }
                }
                else
                {
                    sb.AppendFormat("### result: {0}", myResult);
                }

                failed += equal ? 0 : 1;
            }

            if (failed > 0)
            {
                sb.AppendLine();
                sb.AppendLine(instructions);
            }

            return(failed);
        }
        void ApproveEnumCommon <T>(string label, StringBuilder sb, bool expectGenerated, GenerateLookup generateLookup, FindString findString)
        {
            var buffer = new byte[1024];

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 255;
            }
            var values = StringEnum.SortValues(typeof(T));

            fixed(byte *ptr = buffer)
            {
                var lookup = generateLookup(values, ptr, buffer.Length);

                if (!expectGenerated)
                {
                    Assert.False(lookup.HasValue, "lookup must not have been generated");
                    return;
                }

                Assert.True(lookup.HasValue, "lookup must have been generated");
                var stringStart = (int)(lookup.Value.StringStart - ptr);


                sb.AppendLine("# " + label + " " + SchemaTest.HumanName(typeof(T)));
                sb.AppendLine();
                sb.AppendLine(String.Format("GetLookupType: {0}", StringEnum.GetLookupType(values)));
                sb.AppendLine(String.Format("Values count: {0}", values.Length));
                sb.AppendLine(String.Format("Max value: {0}", values.LastOrDefault().Key));
                sb.AppendLine();
                sb.AppendLine("## Lookups");
                Util.Hex.Dump(sb, buffer.Take(stringStart));
                sb.AppendLine();
                sb.AppendLine();
                sb.AppendLine("## Strings");
                Util.Hex.Dump(sb, buffer.Skip(stringStart).Take((int)lookup.Value.StringLength));


                bool signed = false;

                switch (Type.GetTypeCode(typeof(T)))
                {
                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    signed = true;
                    break;
                }

                var chars = new char[64];

                foreach (T dow in Enum.GetValues(typeof(T)))
                {
                    byte *str;
                    int   len;
                    ulong enumValue = signed ? (ulong)Convert.ToInt64(dow) : Convert.ToUInt64(dow);
                    findString(lookup.Value, enumValue, out str, out len);
                    Assert.True(str != null, "string must not be null");
                    var dowString = dow.ToString();
                    Assert.Equal(Encoding.UTF8.GetByteCount(dowString), len);

                    string actualString;
                    fixed(char *ch = chars)
                    {
                        var strLen = Encoding.UTF8.GetChars(str, len, ch, chars.Length);

                        actualString = new string(ch, 0, strLen);
                    }

                    Assert.Equal(dowString, actualString);
                }
            }
        }