public void TestPhoneBookEntryMin() { var testControl = "120,\"18008626190\",129,\"UMB MortgageServices\""; var testSubject = new PhoneBookEntry { Index = 120, Number = "18008626190", NumberType = new PhoneBookEntryType(129), Text = "UMB MortgageServices" }; var testResult = testSubject.ToString(); Assert.AreEqual(testControl,testResult); }
public void TestPhoneBookEntryParseOwnToString() { var testInput = new PhoneBookEntry { Index = 120, Number = "18008626190", NumberType = new PhoneBookEntryType(129), Text = "UMB MortgageServices", Hidden = false, Group = "Banking", AdditionalNumber = "18004445555", AdditionalNumberType = new PhoneBookEntryType(128), SecondText = "Direct Line", Email = "*****@*****.**", SipUri = "sip:+1-212-555-1212:[email protected];user=phone", TelUri = "tel:+1-212-555-1212" }; //get PhoneBookEntry as string, Hidden will appear at the end var testSubject = testInput.ToString(); PhoneBookEntry testOut; var testResult = PhoneBookEntry.TryParse(testSubject, out testOut); Assert.IsTrue(testResult); Assert.AreEqual(testInput.Index, testOut.Index); Assert.AreEqual(testInput.Number, testOut.Number); Assert.AreEqual(testInput.NumberType.ToString(), testOut.NumberType.ToString()); Assert.AreEqual(testInput.Text, testOut.Text); Assert.AreEqual(testInput.Hidden, testOut.Hidden); Assert.AreEqual(testInput.Group, testOut.Group); Assert.AreEqual(testInput.AdditionalNumber, testOut.AdditionalNumber); Assert.AreEqual(testInput.AdditionalNumberType.ToString(), testOut.AdditionalNumberType.ToString()); Assert.AreEqual(testInput.SecondText, testOut.SecondText); Assert.AreEqual(testInput.SipUri, testOut.SipUri); Assert.AreEqual(testInput.TelUri, testOut.TelUri); }
public void TestPhoneBookEntryToStringFull() { var testControl = "120,\"18008626190\",129,\"UMB MortgageServices\",\"Banking\",\"18004445555\",128,\"Direct Line\",\"[email protected]\",\"sip:+1-212-555-1212:[email protected];user=phone\",\"tel:+1-212-555-1212\",0"; var testSubject = new PhoneBookEntry { Index = 120, Number = "18008626190", NumberType = new PhoneBookEntryType(129), Text = "UMB MortgageServices", Hidden = false, Group = "Banking", AdditionalNumber = "18004445555", AdditionalNumberType = new PhoneBookEntryType(128), SecondText = "Direct Line", Email = "*****@*****.**", SipUri = "sip:+1-212-555-1212:[email protected];user=phone", TelUri = "tel:+1-212-555-1212" }; var testResult = testSubject.ToString(); Assert.AreEqual(testControl, testResult); }
/// <summary> /// Attempts to parse a single +CPBR: line into a <see cref="PhoneBookEntry"/>. /// </summary> /// <param name="cpbrLine">The line candidate.</param> /// <param name="phoneBookEntry">The outbound <see cref="PhoneBookEntry"/> which will be assigned the parsed values if successful.</param> /// <returns>True if string was parsed, False otherwise.</returns> public static bool TryParse(string cpbrLine, out PhoneBookEntry phoneBookEntry) { const string INDEX = @"([0-9]{1,4})"; const string TEXT = @"\x2C\x22(.*?)\x22"; const string TYPE = @"\x2C([0-9]{1,3})"; const string EMAIL = @"\x22?\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\x22?"; const string SESSION_INIT_PROTOCOL = @"\x22?sip\:.*"; const string TEL_PROTOCOL = @"\x22?tel\:.*"; const string CPBR_REGEX_PATTERN_REQUIRED = INDEX + TEXT + TYPE + TEXT; if(String.IsNullOrWhiteSpace(cpbrLine)) { phoneBookEntry = null; return false; } //strip off any headers cpbrLine = Utility.StripOffAtCommand(cpbrLine); try { var match = Regex.Match(cpbrLine, CPBR_REGEX_PATTERN_REQUIRED); if (!match.Success) { phoneBookEntry = null; return false; } phoneBookEntry = new PhoneBookEntry(); //index int index = 0; Int32.TryParse(match.Groups[1].Value, out index); phoneBookEntry.Index = index; //number phoneBookEntry.Number = match.Groups[2].Value; //phone type byte phoneType = 0; byte.TryParse(match.Groups[3].Value, out phoneType); phoneBookEntry.NumberType = new PhoneBookEntryType(phoneType); //text phoneBookEntry.Text = match.Groups[4].Value; //optional values are present if (cpbrLine.Split(',').Length > 4) { var startAt = match.Groups[4].Index + match.Groups[4].Length + 1; var subStringLength = cpbrLine.Length - startAt; var ov = cpbrLine.Substring(startAt, subStringLength).Split(','); var ovf = (from optionalValue in ov where !String.IsNullOrWhiteSpace(optionalValue) select optionalValue).ToArray(); //see 8.14 versus 8.12, when returned from CPBR Hidden is fifth, when entered in CPBW hidden is last foreach(var op in ovf) { //hidden if(op.Length == 1 && (op == "1" || op == "0")) { phoneBookEntry.Hidden = op == "1"; continue; } //additional phone type if(!op.StartsWith("\"") && Regex.IsMatch(op,"[0-9]{3}")) { byte adNumType = 0; if (byte.TryParse(op, out adNumType)) { phoneBookEntry.AdditionalNumberType = new PhoneBookEntryType(adNumType); continue; } } //sip uri if(Regex.IsMatch(op, SESSION_INIT_PROTOCOL)) { phoneBookEntry.SipUri = op.Replace("\"", ""); continue; } //tel uri if(Regex.IsMatch(op, TEL_PROTOCOL)) { phoneBookEntry.TelUri = op.Replace("\"", ""); continue; } //email if(Regex.IsMatch(op,EMAIL)) { phoneBookEntry.Email = op.Replace("\"", ""); continue; } //additional number if(op.ToCharArray().All(v => char.IsDigit(v) || v == '"' || v == '+')) { phoneBookEntry.AdditionalNumber = op.Replace("\"", ""); continue; } //take group first then second text if(string.IsNullOrWhiteSpace(phoneBookEntry.Group)) phoneBookEntry.Group = op.Replace("\"", ""); else phoneBookEntry.SecondText = op.Replace("\"", ""); } } return true; } catch { phoneBookEntry = null; } return false; }