private IEnumerable <ImportGroupAddress> GetGa(string gaExport) { var lines = gaExport.GetLines().ToList(); lines.RemoveAt(0); lines.RemoveAt(lines.Count - 1); foreach (var line in lines) { ImportGroupAddress i = null; try { var c = line.GetColumns().ToArray(); var slices = c.Slice(1, 2); var ga = slices.Select(a => a.Replace("\"", string.Empty)).Select(Selector); var addressString = string.Join(" ", ga); if (GroupAddress.TryParse(addressString, out var gaa)) { i = new ImportGroupAddress() { GroupName = c[0], AddressString = addressString }; } } catch (Exception e) { } if (i != null) { yield return(i); } } }
static void TestParse(string text) { var buffer = Encoding.ASCII.GetBytes(text); GroupAddress group; try { Assert.IsTrue(GroupAddress.TryParse(text, out group), "GroupAddress.TryParse(string) should succeed."); } catch (Exception ex) { Assert.Fail("GroupAddress.TryParse(string) should not throw an exception: {0}", ex); } try { Assert.IsTrue(GroupAddress.TryParse(buffer, out group), "GroupAddress.TryParse(byte[]) should succeed."); } catch (Exception ex) { Assert.Fail("GroupAddress.TryParse(byte[]) should not throw an exception: {0}", ex); } try { Assert.IsTrue(GroupAddress.TryParse(buffer, 0, out group), "GroupAddress.TryParse(byte[], int) should succeed."); } catch (Exception ex) { Assert.Fail("GroupAddress.TryParse(byte[], int) should not throw an exception: {0}", ex); } try { Assert.IsTrue(GroupAddress.TryParse(buffer, 0, buffer.Length, out group), "GroupAddress.TryParse(byte[], int, int) should succeed."); } catch (Exception ex) { Assert.Fail("GroupAddress.TryParse(byte[], int, int) should not throw an exception: {0}", ex); } try { group = GroupAddress.Parse(text); } catch (Exception ex) { Assert.Fail("GroupAddress.Parse(string) should not throw an exception: {0}", ex); } try { group = GroupAddress.Parse(buffer); } catch (Exception ex) { Assert.Fail("GroupAddress.Parse(string) should not throw an exception: {0}", ex); } try { group = GroupAddress.Parse(buffer, 0); } catch (Exception ex) { Assert.Fail("GroupAddress.Parse(string) should not throw an exception: {0}", ex); } try { group = GroupAddress.Parse(buffer, 0, buffer.Length); } catch (Exception ex) { Assert.Fail("GroupAddress.Parse(string) should not throw an exception: {0}", ex); } }
static void TestParseFailure(string text, bool result, int tokenIndex, int errorIndex) { var buffer = text.Length > 0 ? Encoding.ASCII.GetBytes(text) : new byte[1]; GroupAddress group; Assert.AreEqual(result, GroupAddress.TryParse(text, out group), "GroupAddress.TryParse(string)"); Assert.AreEqual(result, GroupAddress.TryParse(buffer, out group), "GroupAddress.TryParse(byte[])"); Assert.AreEqual(result, GroupAddress.TryParse(buffer, 0, out group), "GroupAddress.TryParse(byte[], int)"); Assert.AreEqual(result, GroupAddress.TryParse(buffer, 0, buffer.Length, out group), "GroupAddress.TryParse(byte[], int, int)"); try { GroupAddress.Parse(text); Assert.Fail("GroupAddress.Parse(string) should fail."); } catch (ParseException ex) { Assert.AreEqual(tokenIndex, ex.TokenIndex, "ParseException did not have the correct token index."); Assert.AreEqual(errorIndex, ex.ErrorIndex, "ParseException did not have the error index."); } catch { Assert.Fail("GroupAddress.Parse(string) should throw ParseException."); } try { GroupAddress.Parse(buffer); Assert.Fail("GroupAddress.Parse(byte[]) should fail."); } catch (ParseException ex) { Assert.AreEqual(tokenIndex, ex.TokenIndex, "ParseException did not have the correct token index."); Assert.AreEqual(errorIndex, ex.ErrorIndex, "ParseException did not have the error index."); } catch { Assert.Fail("GroupAddress.Parse(new byte[]) should throw ParseException."); } try { GroupAddress.Parse(buffer, 0); Assert.Fail("GroupAddress.Parse(byte[], int) should fail."); } catch (ParseException ex) { Assert.AreEqual(tokenIndex, ex.TokenIndex, "ParseException did not have the correct token index."); Assert.AreEqual(errorIndex, ex.ErrorIndex, "ParseException did not have the error index."); } catch { Assert.Fail("GroupAddress.Parse(new byte[], int) should throw ParseException."); } try { GroupAddress.Parse(buffer, 0, buffer.Length); Assert.Fail("GroupAddress.Parse(byte[], int, int) should fail."); } catch (ParseException ex) { Assert.AreEqual(tokenIndex, ex.TokenIndex, "ParseException did not have the correct token index."); Assert.AreEqual(errorIndex, ex.ErrorIndex, "ParseException did not have the error index."); } catch { Assert.Fail("GroupAddress.Parse(new byte[], int, int) should throw ParseException."); } }