示例#1
0
        /// <summary>
        /// Tests each of the given JSON samples via serialization/deserialization. If an error occurs when testing a
        /// sample, the error is logged and then the remaining samples are tested. Once all samples have been tested,
        /// an exception would be thrown if any of the samples failed.
        /// </summary>
        /// <typeparam name="TSample">JSON sample class</typeparam>
        /// <typeparam name="TResource">JSON sample type</typeparam>
        public void TestJsonSamples <TSample, TResource>(ITestOutputHelper logger)
        {
            int failureCount = 0;
            int sampleCount  = 0;
            IEnumerable <JsonSampleInfo> samples = JsonSampleCommon.GetJsonSamplesFromType <TSample>();

            foreach (JsonSampleInfo sampleInfo in samples)
            {
                string sampleName     = sampleInfo.Name;
                string serializedJson = string.Empty;
                sampleCount++;
                logger.WriteLine
                (
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Testing JSON sample #{0}: {1}",
                        sampleCount,
                        sampleInfo.Name));

                try
                {
                    TestJsonSample <TResource>(sampleInfo);
                    logger.WriteLine(sampleName + " PASSED");
                }
                catch (Exception ex)
                {
                    // When a sample test fails, log the exception and then continue testing the remaining samples.
                    logger.WriteLine(
                        string.Format(
                            "{0} FAILED: Exception: {1}{2}{3} JSON:{4}{5}{6}",
                            sampleName,
                            ex,
                            Environment.NewLine,
                            sampleName,
                            Environment.NewLine,
                            sampleInfo.Json,
                            serializedJson));
                    failureCount++;
                }
            }

            // Fail the test if any of the samples failed.
            Assert.False(failureCount > 0, string.Format(
                             CultureInfo.InvariantCulture,
                             "{0} of {1} samples failed. See test output for details.",
                             failureCount,
                             sampleCount));
        }
示例#2
0
 public IEnumerator <object[]> GetEnumerator()
 {
     return(JsonSampleCommon.GetJsonSamplesFromType <T>()
            .Select(sample => new object[] { sample })
            .GetEnumerator());
 }