Beispiel #1
0
        public void TestJsonSchemaCommon(IYamlSchema schema)
        {
            Assert.Equal(SchemaBase.StrShortTag, schema.GetDefaultTag(new Scalar(null, null, "true", ScalarStyle.DoubleQuoted, false, false)));
            Assert.Equal(JsonSchema.BoolShortTag, schema.GetDefaultTag(new Scalar("true")));
            Assert.Equal(JsonSchema.NullShortTag, schema.GetDefaultTag(new Scalar("null")));
            Assert.Equal(JsonSchema.IntShortTag, schema.GetDefaultTag(new Scalar("5")));
            Assert.Equal(JsonSchema.FloatShortTag, schema.GetDefaultTag(new Scalar("5.5")));

            Assert.Equal(JsonSchema.NullLongTag, schema.ExpandTag("!!null"));
            Assert.Equal(JsonSchema.BoolLongTag, schema.ExpandTag("!!bool"));
            Assert.Equal(JsonSchema.IntLongTag, schema.ExpandTag("!!int"));
            Assert.Equal(JsonSchema.FloatLongTag, schema.ExpandTag("!!float"));

            Assert.Equal("!!null", schema.ShortenTag(JsonSchema.NullLongTag));
            Assert.Equal("!!bool", schema.ShortenTag(JsonSchema.BoolLongTag));
            Assert.Equal("!!int", schema.ShortenTag(JsonSchema.IntLongTag));
            Assert.Equal("!!float", schema.ShortenTag(JsonSchema.FloatLongTag));

            TryParse(schema, "null", JsonSchema.NullShortTag, null);
            TryParse(schema, "true", JsonSchema.BoolShortTag, true);
            TryParse(schema, "false", JsonSchema.BoolShortTag, false);
            TryParse(schema, "5", JsonSchema.IntShortTag, 5);
            TryParse(schema, "5.5", JsonSchema.FloatShortTag, 5.5);
            TryParse(schema, ".inf", JsonSchema.FloatShortTag, double.PositiveInfinity);
        }
Beispiel #2
0
        public void TestFailsafeSchemaCommon(IYamlSchema schema)
        {
            Assert.Equal(SchemaBase.StrShortTag, schema.GetDefaultTag(new Scalar("true")));
            Assert.Equal(SchemaBase.StrShortTag, schema.GetDefaultTag(new Scalar("custom", "boom")));
            Assert.Equal(FailsafeSchema.MapShortTag, schema.GetDefaultTag(new MappingStart()));
            Assert.Equal(FailsafeSchema.SeqShortTag, schema.GetDefaultTag(new SequenceStart()));

            Assert.Equal(FailsafeSchema.MapLongTag, schema.ExpandTag("!!map"));
            Assert.Equal(FailsafeSchema.SeqLongTag, schema.ExpandTag("!!seq"));
            Assert.Equal(SchemaBase.StrLongTag, schema.ExpandTag("!!str"));

            Assert.Equal("!!map", schema.ShortenTag(FailsafeSchema.MapLongTag));
            Assert.Equal("!!seq", schema.ShortenTag(FailsafeSchema.SeqLongTag));
            Assert.Equal("!!str", schema.ShortenTag(SchemaBase.StrLongTag));

            TryParse(schema, "true", SchemaBase.StrShortTag, "true");
        }
Beispiel #3
0
        public virtual Type TypeFromTag(string tag)
        {
            if (tag == null)
            {
                return(null);
            }

            // Get the default schema type if there is any
            var  shortTag = schema.ShortenTag(tag);
            Type type;

            if (shortTag != tag)
            {
                type = schema.GetTypeForDefaultTag(shortTag);
                if (type != null)
                {
                    return(type);
                }
            }

            // un-escape tag
            shortTag = Uri.UnescapeDataString(shortTag);

            // Else try to find a registered alias
            if (tagToType.TryGetValue(shortTag, out type))
            {
                return(type);
            }

            // Else resolve type from assembly
            var tagAsType = shortTag.StartsWith("!") ? shortTag.Substring(1) : shortTag;

            // Try to resolve the type from registered assemblies
            type = ResolveType(tagAsType);

            // Register a type that was found
            tagToType.Add(shortTag, type);
            if (type != null && !typeToTag.ContainsKey(type))
            {
                typeToTag.Add(type, shortTag);
            }

            return(type);
        }
        public void TestFailsafeSchemaCommon(IYamlSchema schema)
        {
            Assert.AreEqual(SchemaBase.StrShortTag, schema.GetDefaultTag(new Scalar("true")));
            Assert.AreEqual(SchemaBase.StrShortTag, schema.GetDefaultTag(new Scalar("custom", "boom")));
            Assert.AreEqual(FailsafeSchema.MapShortTag, schema.GetDefaultTag(new MappingStart()));
            Assert.AreEqual(FailsafeSchema.SeqShortTag, schema.GetDefaultTag(new SequenceStart()));

            Assert.AreEqual(FailsafeSchema.MapLongTag, schema.ExpandTag("!!map"));
            Assert.AreEqual(FailsafeSchema.SeqLongTag, schema.ExpandTag("!!seq"));
            Assert.AreEqual(SchemaBase.StrLongTag, schema.ExpandTag("!!str"));

            Assert.AreEqual("!!map", schema.ShortenTag(FailsafeSchema.MapLongTag));
            Assert.AreEqual("!!seq", schema.ShortenTag(FailsafeSchema.SeqLongTag));
            Assert.AreEqual("!!str", schema.ShortenTag(SchemaBase.StrLongTag));

            TryParse(schema, "true", SchemaBase.StrShortTag, "true");
        }
        public void TestJsonSchemaCommon(IYamlSchema schema)
        {
            Assert.AreEqual(SchemaBase.StrShortTag, schema.GetDefaultTag(new Scalar(null, null, "true", ScalarStyle.DoubleQuoted, false, false)));
            Assert.AreEqual(JsonSchema.BoolShortTag, schema.GetDefaultTag(new Scalar("true")));
            Assert.AreEqual(JsonSchema.NullShortTag, schema.GetDefaultTag(new Scalar("null")));
            Assert.AreEqual(JsonSchema.IntShortTag, schema.GetDefaultTag(new Scalar("5")));
            Assert.AreEqual(JsonSchema.FloatShortTag, schema.GetDefaultTag(new Scalar("5.5")));

            Assert.AreEqual(JsonSchema.NullLongTag, schema.ExpandTag("!!null"));
            Assert.AreEqual(JsonSchema.BoolLongTag, schema.ExpandTag("!!bool"));
            Assert.AreEqual(JsonSchema.IntLongTag, schema.ExpandTag("!!int"));
            Assert.AreEqual(JsonSchema.FloatLongTag, schema.ExpandTag("!!float"));

            Assert.AreEqual("!!null", schema.ShortenTag(JsonSchema.NullLongTag));
            Assert.AreEqual("!!bool", schema.ShortenTag(JsonSchema.BoolLongTag));
            Assert.AreEqual("!!int", schema.ShortenTag(JsonSchema.IntLongTag));
            Assert.AreEqual("!!float", schema.ShortenTag(JsonSchema.FloatLongTag));

            TryParse(schema, "null", JsonSchema.NullShortTag, null);
            TryParse(schema, "true", JsonSchema.BoolShortTag, true);
            TryParse(schema, "false", JsonSchema.BoolShortTag, false);
            TryParse(schema, "5", JsonSchema.IntShortTag, 5);
            TryParse(schema, "5.5", JsonSchema.FloatShortTag, 5.5);
            TryParse(schema, ".inf", JsonSchema.FloatShortTag, double.PositiveInfinity);
        }