public void ReadSpecTest(SchemaSpecTest schemaSpecTest)
        {
            Console.WriteLine("Running reader JSON Schema {0} test {1}: {2}", schemaSpecTest.Version, schemaSpecTest.TestNumber, schemaSpecTest);

            IList<string> errorMessages = new List<string>();

            JSchemaPreloadedResolver resolver = GetResolver();

            JSchema s = JSchema.Load(schemaSpecTest.Schema.CreateReader(), resolver);

            JsonReader jsonReader = schemaSpecTest.Data.CreateReader();

            using (JSchemaValidatingReader reader = new JSchemaValidatingReader(jsonReader))
            {
                reader.Schema = s;
                reader.ValidationEventHandler += (sender, args) => errorMessages.Add(args.Message);

                while (reader.Read())
                {
                }
            }

            bool isValid = (errorMessages.Count == 0);

            Assert.AreEqual(schemaSpecTest.IsValid, isValid, schemaSpecTest.TestCaseDescription + " - " + schemaSpecTest.TestDescription + " - errors: " + StringHelpers.Join(", ", errorMessages));
        }
        public void ReadSpecTest(SchemaSpecTest schemaSpecTest)
        {
            Console.WriteLine("Running reader JSON Schema {0} test {1}: {2}", schemaSpecTest.Version, schemaSpecTest.TestNumber, schemaSpecTest);

            IList <string> errorMessages = new List <string>();

            JSchemaPreloadedResolver resolver = GetResolver();

            JSchema s = JSchema.Load(schemaSpecTest.Schema.CreateReader(), resolver);

            JsonReader jsonReader = schemaSpecTest.Data.CreateReader();

            using (JSchemaValidatingReader reader = new JSchemaValidatingReader(jsonReader))
            {
                reader.Schema = s;
                reader.ValidationEventHandler += (sender, args) => errorMessages.Add(args.Message);

                while (reader.Read())
                {
                }
            }

            bool isValid = (errorMessages.Count == 0);

            Assert.AreEqual(schemaSpecTest.IsValid, isValid, schemaSpecTest.TestCaseDescription + " - " + schemaSpecTest.TestDescription + " - errors: " + StringHelpers.Join(", ", errorMessages));
        }
        public IList <SchemaSpecTest> GetSpecTestDetails()
        {
            if (_specTests != null)
            {
                return(_specTests);
            }

            _specTests = new List <SchemaSpecTest>();

            // get test files location relative to the test project dll
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string baseTestPath  = Path.Combine(baseDirectory, Path.Combine("Specs", "tests")) + @"\";

            string[] testFiles = Directory.GetFiles(baseTestPath, "*.json", SearchOption.AllDirectories);

#if (PORTABLE || NET35)
            testFiles = testFiles.Where(f => !f.EndsWith("bignum.json")).ToArray();
#endif

            // read through each of the *.json test files and extract the test details
            foreach (string testFile in testFiles)
            {
                string relativePath = MakeRelativePath(baseTestPath, testFile);
                string version      = relativePath.Split('\\').First();
                string testJson     = System.IO.File.ReadAllText(testFile);

                JArray a = JArray.Parse(testJson);

                foreach (JObject testCase in a)
                {
                    foreach (JObject test in testCase["tests"])
                    {
                        SchemaSpecTest schemaSpecTest = new SchemaSpecTest();

                        schemaSpecTest.FileName            = Path.GetFileName(testFile);
                        schemaSpecTest.Version             = version;
                        schemaSpecTest.TestCaseDescription = (string)testCase["description"];
                        schemaSpecTest.Schema = (JObject)testCase["schema"];

                        schemaSpecTest.TestDescription = (string)test["description"];
                        schemaSpecTest.Data            = test["data"];
                        schemaSpecTest.IsValid         = (bool)test["valid"];
                        schemaSpecTest.TestNumber      = _specTests.Count(t => t.Version == schemaSpecTest.Version) + 1;

                        _specTests.Add(schemaSpecTest);
                    }
                }
            }

            return(_specTests);
        }
예제 #4
0
        public void WriteSpecTest(SchemaSpecTest schemaSpecTest)
        {
            Console.WriteLine("Running writer JSON Schema {0} test {1}: {2}", schemaSpecTest.Version, schemaSpecTest.TestNumber, schemaSpecTest);

            IList <string> errorMessages = new List <string>();

            JSchemaPreloadedResolver resolver = GetResolver();

            var schemaToken = schemaSpecTest.Schema.DeepClone();

            if (schemaToken.Type == JTokenType.Object)
            {
                schemaToken["$schema"] = GetSchemaUri(schemaSpecTest.Version);
            }

            JSchema s = JSchema.Load(schemaToken.CreateReader(), resolver);

            s.SchemaVersion = GetSchemaUri(schemaSpecTest.Version);

            JsonReader jsonReader = schemaSpecTest.Data.CreateReader();

            StringWriter   sw     = new StringWriter();
            JsonTextWriter writer = new JsonTextWriter(sw);

            using (JSchemaValidatingWriter validatingWriter = new JSchemaValidatingWriter(writer))
            {
                validatingWriter.Schema = s;
                validatingWriter.ValidationEventHandler += (sender, args) => errorMessages.Add(args.Message);

                while (jsonReader.Read())
                {
                    validatingWriter.WriteToken(jsonReader.TokenType, jsonReader.Value);
                }
            }

            bool isValid = (errorMessages.Count == 0);

            Assert.AreEqual(schemaSpecTest.IsValid, isValid, schemaSpecTest.TestCaseDescription + " - " + schemaSpecTest.TestDescription + " - errors: " + StringHelpers.Join(", ", errorMessages));
        }
        public IList<SchemaSpecTest> GetSpecTestDetails()
        {
            if (_specTests != null)
            {
                return _specTests;
            }

            _specTests = new List<SchemaSpecTest>();

            // get test files location relative to the test project dll
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string baseTestPath = Path.Combine(baseDirectory, Path.Combine("Specs", "tests")) + @"\";

            string[] testFiles = Directory.GetFiles(baseTestPath, "*.json", SearchOption.AllDirectories);

#if (PORTABLE || NET35)
            testFiles = testFiles.Where(f => !f.EndsWith("bignum.json")).ToArray();
#endif

            // read through each of the *.json test files and extract the test details
            foreach (string testFile in testFiles)
            {
                string relativePath = MakeRelativePath(baseTestPath, testFile);
                string version = relativePath.Split('\\').First();
                string testJson = System.IO.File.ReadAllText(testFile);

                JArray a = JArray.Parse(testJson);

                foreach (JObject testCase in a)
                {
                    foreach (JObject test in testCase["tests"])
                    {
                        SchemaSpecTest schemaSpecTest = new SchemaSpecTest();

                        schemaSpecTest.FileName = Path.GetFileName(testFile);
                        schemaSpecTest.Version = version;
                        schemaSpecTest.TestCaseDescription = (string)testCase["description"];
                        schemaSpecTest.Schema = (JObject)testCase["schema"];

                        schemaSpecTest.TestDescription = (string)test["description"];
                        schemaSpecTest.Data = test["data"];
                        schemaSpecTest.IsValid = (bool)test["valid"];
                        schemaSpecTest.TestNumber = _specTests.Count(t => t.Version == schemaSpecTest.Version) + 1;

                        _specTests.Add(schemaSpecTest);
                    }
                }
            }

            return _specTests;
        }
예제 #6
0
        public static IList <object[]> GetSpecTestDetails()
        {
            if (_specTests == null)
            {
                _specTests = new List <SchemaSpecTest>();

                // get test files location relative to the test project dll
                string baseTestPath = ResolvePath(Path.Combine("Specs", "tests")) + @"\";

                string[] testFiles = Directory.GetFiles(baseTestPath, "*.json", SearchOption.AllDirectories);

#if (PORTABLE || NET35)
                testFiles = testFiles.Where(f => !f.EndsWith("bignum.json")).ToArray();
                testFiles = testFiles.Where(f => !f.EndsWith("format.json")).ToArray();
#endif
                testFiles = testFiles.Where(f => !f.EndsWith("non-bmp-regex.json")).ToArray();
                testFiles = testFiles.Where(f => !f.EndsWith("ecmascript-regex.json")).ToArray();

                // todo - add support for all formats
                testFiles = testFiles.Where(f => !f.EndsWith("content.json") &&
                                            !f.EndsWith("idn-email.json") &&
                                            !f.EndsWith("idn-hostname.json") &&
                                            !f.EndsWith("iri-reference.json") &&
                                            !f.EndsWith("iri.json") &&
                                            !f.EndsWith("relative-json-pointer.json")).ToArray();

                // read through each of the *.json test files and extract the test details
                foreach (string testFile in testFiles)
                {
                    string relativePath = MakeRelativePath(baseTestPath, testFile);
                    string version      = relativePath.Split('\\').First();
                    string testJson     = System.IO.File.ReadAllText(testFile);

                    JsonTextReader testJsonReader = new JsonTextReader(new StringReader(testJson));
                    testJsonReader.FloatParseHandling = FloatParseHandling.Decimal;

                    JArray a = (JArray)JToken.ReadFrom(testJsonReader);

                    foreach (JObject testCase in a)
                    {
                        foreach (JObject test in testCase["tests"])
                        {
                            SchemaSpecTest schemaSpecTest = new SchemaSpecTest();

                            schemaSpecTest.FileName            = Path.GetFileName(testFile);
                            schemaSpecTest.Version             = version;
                            schemaSpecTest.TestCaseDescription = (string)testCase["description"];
                            schemaSpecTest.Schema = testCase["schema"];

                            schemaSpecTest.TestDescription = (string)test["description"];
                            schemaSpecTest.Data            = test["data"];
                            schemaSpecTest.IsValid         = (bool)test["valid"];
                            schemaSpecTest.TestNumber      = _specTests.Count(t => t.Version == schemaSpecTest.Version) + 1;

#if NET35
                            // Uri class in .NET Framework 2.0 doesn't like the uri in this test
                            if ((schemaSpecTest.FileName == "format.json" || schemaSpecTest.FileName == "uri.json") &&
                                schemaSpecTest.TestDescription == "a valid URL ")
                            {
                                continue;
                            }
#endif

                            _specTests.Add(schemaSpecTest);
                        }
                    }
                }
            }

            return(_specTests.Select(s => new object[] { s }).ToList());
        }