public void ToStringAutomaticUnitLength() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.IsAge = false; ts.From = baseDateTime; ts.Granularity = TimeSpanUnit.Years; ts.Threshold = TimeSpanUnit.Minutes; ts.To = baseDateTime.AddDays(2).AddHours(1).AddMinutes(5); ts.IsAge = false; string formattedTimeSpan = ts.ToString(TimeSpanUnitLength.Automatic, CultureInfo.CurrentCulture); Assert.AreEqual("2d 1hr 5min", formattedTimeSpan); ts.IsAge = true; formattedTimeSpan = ts.ToString(TimeSpanUnitLength.Automatic, CultureInfo.CurrentCulture); // IsAge=true, 2days < timespan < 4 weeks, granularity=days, threshold=days Assert.AreEqual("2d", formattedTimeSpan); ts.To = ts.To.AddYears(2); formattedTimeSpan = ts.ToString(TimeSpanUnitLength.Automatic, CultureInfo.CurrentCulture); // IsAge=true, 2 years < timespan < 18 years, granularity=years, threshold=months Assert.AreEqual("2years", formattedTimeSpan); }
public void ParseInvalidText() { NhsTimeSpan parsedTimeSpan; // Pass rubbish into TimeSpan Assert.IsFalse(NhsTimeSpan.TryParse("hsh sdoih", out parsedTimeSpan, CultureInfo.InvariantCulture)); }
public void Parse_Text() { NhsTimeSpan timeSpan = NhsTimeSpan.Parse("3d 17hrs 7min", CultureInfo.InvariantCulture); DateTime expectedTo = timeSpan.From.AddDays(3).AddHours(17).AddMinutes(7); Assert.AreEqual(timeSpan.To, expectedTo, "TimeSpan To does not match expected date"); }
/// <summary> /// Check if the ControlToValidate is valid /// </summary> /// <returns>True if control properties is valid</returns> protected override bool EvaluateIsValid() { string value = this.GetControlValidationValue(this.ControlToValidate); NhsTimeSpan tryParseResult; return(NhsTimeSpan.TryParse(value, out tryParseResult, System.Threading.Thread.CurrentThread.CurrentCulture, ((TimeSpanInputBox)this.NamingContainer.FindControl(this.ControlToValidate)).IsAge)); }
/// <summary> /// Default constructor /// </summary> public TimeSpanInputBoxExtender() { this.EnableClientState = true; this.state = new TimeSpanInputClientState(); this.value = new NhsTimeSpan(); this.ClientStateValuesLoaded += new EventHandler(this.TimeSpanInputBoxExtender_ClientStateValuesLoaded); }
/// <summary> /// Converts the provided dictionary into an object of the specified type /// </summary> /// <param name="dictionary">An IDictionary instance of property data stored as name/value pairs</param> /// <param name="type">The Type of the resulting object</param> /// <param name="serializer">The JavaScriptSerializer instance</param> /// <returns>The deserialized Object</returns> /// <remarks>KeyboardShortcut is never sent back up to the server so we do not support Deserialization</remarks> /// <exception cref="NotImplementedException">Always returns this exception</exception> public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer) { NhsTimeSpan deserialisationTarget = new NhsTimeSpan(); deserialisationTarget.From = (dictionary.ContainsKey("_from") ? ((DateTime)dictionary["_from"]).ToLocalTime() : ((DateTime)dictionary["from"]).ToLocalTime()); deserialisationTarget.To = (dictionary.ContainsKey("_to") ? ((DateTime)dictionary["_to"]).ToLocalTime() : ((DateTime)dictionary["to"]).ToLocalTime()); deserialisationTarget.Granularity = (dictionary.ContainsKey("_granularity") ? (TimeSpanUnit)dictionary["_granularity"] : (TimeSpanUnit)dictionary["granularity"]); deserialisationTarget.IsAge = (dictionary.ContainsKey("_isAge") ? (bool)dictionary["_isAge"] : (bool)dictionary["isAge"]); deserialisationTarget.Threshold = (dictionary.ContainsKey("_threshold") ? (TimeSpanUnit)dictionary["_threshold"] : (TimeSpanUnit)dictionary["threshold"]); return(deserialisationTarget); }
public void Parse_InvalidTextRepeatedValues() { DateTime baseDateTime = BaseDate; NhsTimeSpan timeSpan = new NhsTimeSpan(); timeSpan.To = baseDateTime.AddDays(3).AddHours(17); NhsTimeSpan parsedTimeSpan; // Pass a TimeSpan in twice to simulate multiple entries for the same unit e.g "3d 3d" Assert.IsFalse(NhsTimeSpan.TryParse(string.Format(CultureInfo.InvariantCulture, "{0} {1}", timeSpan.ToString(), timeSpan.ToString()), out parsedTimeSpan, CultureInfo.InvariantCulture)); }
public void ToString_IsAge_GreaterThan2HoursLessThan2Days() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.From = baseDateTime; ts.To = baseDateTime.AddDays(1).AddHours(2).AddMinutes(5); ts.IsAge = true; Assert.AreEqual("26hrs", ts.ToString()); }
public void ToString_IsAge_LessThan4WeeksGreaterThan2Days() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.From = baseDateTime; ts.To = baseDateTime.AddDays(3).AddHours(17).AddMinutes(7); ts.IsAge = true; Assert.AreEqual("3d", ts.ToString()); }
public void ToString_IsAge_LessThan2Hours() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.From = baseDateTime; ts.To = baseDateTime.AddMinutes(90); ts.IsAge = true; Assert.AreEqual("90min", ts.ToString()); }
public void ToString_IsAge_LessThan18YearsGreaterThan4Years() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.From = baseDateTime; ts.To = baseDateTime.AddYears(4).AddDays(39); ts.IsAge = true; Assert.AreEqual("4y 1m", ts.ToString()); }
public void ToString_IsAge_LessThan2YearsGreaterThan13Months() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = DateTime.Parse("21 January 2007", CultureInfo.CurrentUICulture); ts.From = baseDateTime; ts.To = baseDateTime.AddYears(1).AddDays(39).AddHours(5); ts.IsAge = true; Assert.AreEqual("13m 1w 1d", ts.ToString()); }
public void ToString_IsAge_LessThan2YearsGreaterThan1YearAndAWeek() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.From = baseDateTime; ts.To = baseDateTime.AddYears(1).AddDays(8).AddHours(5); ts.IsAge = true; Assert.AreEqual("12m 1w 1d", ts.ToString()); }
public void ToString_IsAge_LessThan1YearGreaterThan4WeeksAndADay() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.From = baseDateTime; ts.To = baseDateTime.AddDays(29).AddHours(5).AddMinutes(2); ts.IsAge = true; Assert.AreEqual("4w 1d", ts.ToString()); }
public void ToString_IsAge_LessThan1Month() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.From = baseDateTime; ts.To = baseDateTime.AddDays(27).AddHours(5).AddMinutes(2); ts.IsAge = true; Assert.AreEqual("27d", ts.ToString()); }
public void ToString_LessThan2Hours_HoursAndMinutes() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.From = baseDateTime; ts.To = baseDateTime.AddMinutes(90); ts.IsAge = false; ts.Granularity = TimeSpanUnit.Hours; ts.Threshold = TimeSpanUnit.Minutes; Assert.AreEqual("1hr 30min", ts.ToString()); }
public void ToString_3Days17Hours7Minutes() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.IsAge = false; ts.From = baseDateTime; ts.Granularity = TimeSpanUnit.Hours; ts.Threshold = TimeSpanUnit.Hours; ts.To = baseDateTime.AddDays(3).AddHours(17).AddMinutes(7); Assert.AreEqual("89hrs", ts.ToString()); }
public void ToString_GreaterThan2Hours_LessThan2Days_DaysToMinutes() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.IsAge = false; ts.From = baseDateTime; ts.Granularity = TimeSpanUnit.Days; ts.Threshold = TimeSpanUnit.Minutes; ts.To = baseDateTime.AddDays(1).AddHours(2).AddMinutes(5); Assert.AreEqual("1d 2hrs 5min", ts.ToString()); }
public void ToString_1Year1Day5Hours() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.IsAge = false; ts.From = baseDateTime; ts.Granularity = TimeSpanUnit.Months; ts.Threshold = TimeSpanUnit.Months; ts.To = baseDateTime.AddYears(4).AddDays(39); Assert.AreEqual("49m", ts.ToString()); }
/// <summary> /// Refresh the text displayed in the text box. /// </summary> /// <returns> /// Status of update. /// </returns> private bool ValidateAndUpdateValue() { bool status = true; this.SetValid(); this.txtInput.Text = this.txtInput.Text.Trim(); string preParsedValue; if (!string.IsNullOrEmpty(this.txtInput.Text)) { NhsTimeSpan tmp; if (TryPreParseUnits(this.txtInput.Text.Trim(), out preParsedValue)) { try { if (NhsTimeSpan.TryParse(preParsedValue, out tmp, CultureInfo.CurrentCulture, this.IsAge)) { this.Value.From = tmp.From; this.Value.To = tmp.To; this.RefreshDisplayText(); this.NotifyPropertyChanged("Value"); } else { this.SetInvalid(ref status); } } catch (ArgumentOutOfRangeException) { this.SetInvalid(ref status); } } else { this.SetInvalid(ref status); } } else { this.Value = new NhsTimeSpan(); this.RefreshDisplayText(); } return(status); }
public void ToStringLongUnitLength() { NhsTimeSpan ts = new NhsTimeSpan(); DateTime baseDateTime = BaseDate; ts.IsAge = false; ts.From = baseDateTime; ts.Granularity = TimeSpanUnit.Years; ts.Threshold = TimeSpanUnit.Minutes; ts.To = baseDateTime.AddYears(3).AddDays(1).AddHours(2).AddMinutes(5); string formattedTimeSpan = ts.ToString(TimeSpanUnitLength.Long, CultureInfo.CurrentCulture); Assert.AreEqual("3years 1day 2hours 5minutes", formattedTimeSpan); }
/// <summary> /// Builds a dictionary of name/value pairs /// </summary> /// <param name="obj">The Object to serialize</param> /// <param name="serializer">The JavaScriptSerializer responsible for the serialization</param> /// <returns>An IDictionary object that contains key/value pairs that represent the object�s data</returns> public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer) { NhsTimeSpan timeSpan = obj as NhsTimeSpan; if (timeSpan != null) { //// Create the representation Dictionary <string, object> result = new Dictionary <string, object>(); result.Add("from", timeSpan.From); result.Add("to", timeSpan.To); result.Add("granularity", timeSpan.Granularity); result.Add("isAge", timeSpan.IsAge); result.Add("threshold", timeSpan.Threshold); return(result); } return(new Dictionary <string, object>()); }
public void TimeSpanFromAndToConstructorTest() { int fromYear = 1980; int fromMonth = 6; int fromDay = 6; DateTime thisYearsBirthday = new DateTime(DateTime.Today.Year, fromMonth, fromDay); DateTime from = new DateTime(fromYear, fromMonth, fromDay); NhsTimeSpan ts = new NhsTimeSpan(from, thisYearsBirthday); ts.IsAge = true; int expectedAge = 0; while (thisYearsBirthday > from) { thisYearsBirthday = thisYearsBirthday.AddYears(-1); expectedAge++; } Assert.AreEqual(expectedAge, ts.Years, "Age returned does not match"); }
/// <summary> /// Handle loading of client state /// </summary> /// <param name="sender">sender</param> /// <param name="e">args</param> private void TimeSpanInputBoxExtender_ClientStateValuesLoaded(object sender, EventArgs e) { if (this.ClientState != null) { // this.state = new JavaScriptSerializer().Deserialize<TimeSpanInputClientState>(ClientState); // this.Value.From = DateTime.Parse(this.state.From, CultureInfo.CurrentCulture); // this.Value.Granularity = (TimeSpanUnit)this.state.Granularity; // this.Value.IsAge = (bool)this.state.IsAge; // this.text = this.state.Text; // this.Value.To = DateTime.Parse(this.state.To, CultureInfo.CurrentCulture); // this.Value.Threshold = (TimeSpanUnit)this.state.Threshold; // this.value = (NhsTimeSpan)this.state.Value; JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new NhsTimeSpanJavascriptConverter() }); this.state = jss.Deserialize <TimeSpanInputClientState>(ClientState); this.value = this.state.Value; this.unitLength = (TimeSpanUnitLength)this.state.UnitLength; } }
public void NullArgumentTimeSpanParseTest() { NhsTimeSpan.Parse(null, CultureInfo.InvariantCulture); }