void JsonTimeStampHandlesAllFormats() { string expectedJson, actualJson; DateTime expectedTime; ObjWithTimes actualObj = new ObjWithTimes(); for (int i = 0; i < _examples.Length; i++) { // Define the time deserialization expectation UUnitAssert.True(DateTime.TryParseExact(_examples[i], Util._defaultDateTimeFormats, CultureInfo.CurrentCulture, DateTimeStyles.RoundtripKind, out expectedTime), "Index: " + i + "/" + _examples.Length + ", " + _examples[i]); // De-serialize the time using json expectedJson = "{\"timestamp\":\"" + _examples[i] + "\"}"; // We are provided a json string with every random time format actualObj = SimpleJson.DeserializeObject <ObjWithTimes>(expectedJson, Util.ApiSerializerStrategy); actualJson = SimpleJson.SerializeObject(actualObj, Util.ApiSerializerStrategy); if (i == Util.DEFAULT_UTC_OUTPUT_INDEX) // This is the only case where the json input will match the json output { UUnitAssert.StringEquals(expectedJson, actualJson); } // Verify that the times match double diff = (expectedTime - actualObj.timestamp).TotalSeconds; // We expect that we have parsed the time correctly according to expectations UUnitAssert.True(diff < 1, "\nActual time: " + actualObj.timestamp + " vs expected time: " + expectedTime + ", diff: " + diff + "\nActual json: " + actualJson + " vs expected json: " + expectedJson ); } }
void JsonTimeStampHandlesAllFormats() { string expectedJson, actualJson; DateTime expectedTime; ObjWithTimes actualObj = new ObjWithTimes(); for (int i = 0; i < examples.Length; i++) { // Define the time deserialization expectation UUnitAssert.True(DateTime.TryParseExact(examples[i], IsoDateTimeConverter._defaultDateTimeFormats, CultureInfo.CurrentCulture, DateTimeStyles.RoundtripKind, out expectedTime), "Index: " + i + "/" + examples.Length + ", " + examples[i]); // De-serialize the time using json expectedJson = "{\"timestamp\":\"" + examples[i] + "\"}"; // We are provided a json string with every random time format JsonConvert.PopulateObject(expectedJson, actualObj, Util.JsonSettings); actualJson = JsonConvert.SerializeObject(actualObj, Util.JsonFormatting, Util.JsonSettings); if (i == IsoDateTimeConverter.DEFAULT_UTC_OUTPUT_INDEX) // This is the only case where the json input will match the json output UUnitAssert.Equals(expectedJson, actualJson); // Verify that the times match double diff = (expectedTime - actualObj.timestamp).TotalSeconds; // We expect that we have parsed the time correctly according to expectations UUnitAssert.True(diff < 1, "\nActual time: " + actualObj.timestamp + " vs expected time: " + expectedTime + ", diff: " + diff + "\nActual json: " + actualJson + " vs expected json: " + expectedJson ); } }
void JsonTimeStampHandlesAllFormats(UUnitTestContext testContext) { string expectedJson, actualJson; DateTime expectedTime; ObjWithTimes actualObj = new ObjWithTimes(); for (int i = 0; i < _examples.Length; i++) { // Define the time deserialization expectation testContext.True(DateTime.TryParseExact(_examples[i], PlayFabUtil._defaultDateTimeFormats, CultureInfo.CurrentCulture, DateTimeStyles.RoundtripKind, out expectedTime), "Index: " + i + "/" + _examples.Length + ", " + _examples[i]); // De-serialize the time using json expectedJson = "{\"timestamp\":\"" + _examples[i] + "\"}"; // We are provided a json string with every random time format actualObj = JsonWrapper.DeserializeObject<ObjWithTimes>(expectedJson, PlayFabUtil.ApiSerializerStrategy); actualJson = JsonWrapper.SerializeObject(actualObj, PlayFabUtil.ApiSerializerStrategy); if (i == PlayFabUtil.DEFAULT_UTC_OUTPUT_INDEX) // This is the only case where the json input will match the json output testContext.StringEquals(expectedJson, actualJson); // Verify that the times match double diff = (expectedTime - actualObj.timestamp).TotalSeconds; // We expect that we have parsed the time correctly according to expectations testContext.True(diff < 1, "\nActual time: " + actualObj.timestamp + " vs Expected time: " + expectedTime + ", diff: " + diff + "\nActual json: " + actualJson + " vs Expected json: " + expectedJson ); } testContext.EndTest(UUnitFinishState.PASSED, null); }