Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using NUnit.Framework;
  3.  
  4. namespace TableParser
  5. {
  6. [TestFixture]
  7. public class FieldParserTaskTests
  8. {
  9. public static void Test(string input, string[] expectedResult)
  10. {
  11. var actualResult = FieldsParserTask.ParseLine(input);
  12. Assert.AreEqual(expectedResult.Length, actualResult.Count);
  13. for (int i = 0; i < expectedResult.Length; ++i)
  14. {
  15. Assert.AreEqual(expectedResult[i], actualResult[i].Value);
  16. }
  17. }
  18.  
  19. [TestCase("text", new []{"text"})]
  20. [TestCase("hello world", new[] {"hello","world"})]
  21. [TestCase(" hello world ", new[] {"hello", "world"})]
  22. [TestCase("hello", new[] {"hello"})]
  23. [TestCase(" hello world ", new[] {"hello", "world"})]
  24. [TestCase("a 'bcd ef' 'x y'", new[] {"a","bcd ef","x y"})]
  25. [TestCase("' ", new [] {" "})]
  26. [TestCase("\"a 'b' 'c' d\"", new [] {"a 'b' 'c' d"})]
  27. [TestCase(@"'a ""b"" ""c"" d'", new [] {"a \"b\" \"c\" d"})]
  28. [TestCase("\"ab\"с", new [] {"ab","с"})]
  29. [TestCase(@"""\\""", new[] { "\\" })]
  30. [TestCase("", new string[0])]
  31. [TestCase("'a b'", new[] { "a b" })]
  32. [TestCase(@"""", new[] {""})]
  33. [TestCase("'\\\'a\\\''", new[]{"'a'"})]
  34. [TestCase("\"\\\"a\\\"\"", new []{"\"a\""})]
  35.  
  36. public static void RunTests(string input, string[] expectedOutput)
  37. {
  38. // Тело метода изменять не нужно
  39. Test(input, expectedOutput);
  40. }
  41. }
  42.  
  43. public class FieldsParserTask
  44. {
  45. // При решении этой задаче постарайтесь избежать создания методов, длиннее 10 строк.
  46. // Подумайте как можно использовать ReadQuotedField и Token в этой задаче.
  47. public static List<Token> ParseLine(string line)
  48. {
  49. var tokensList = new List<Token>();
  50. if (line[0] == '\"' || line[0] == '\'')
  51. tokensList.Add(ReadQuotedField(line, 0));
  52. else
  53. {
  54. var token = ReadField(line, 0);
  55. if (token.Value.Trim() != "")
  56. tokensList.Add(token);
  57. }
  58. while (tokensList[tokensList.Count - 1].GetIndexNextToToken() < line.Length - 1)
  59. {
  60. var index = tokensList[tokensList.Count - 1].GetIndexNextToToken();
  61. if (line[index] == '\"' || line[index] == '\'')
  62. tokensList.Add(ReadQuotedField(line, index));
  63. else
  64. tokensList.Add(ReadField(line, index));
  65. }
  66. for (var i = 0; i < tokensList.Count; i++)
  67. {
  68. if (string.IsNullOrEmpty(tokensList[i].Value))
  69. tokensList.RemoveAt(i);
  70. }
  71. return tokensList; // сокращенный синтаксис для инициализации коллекции.
  72. }
  73.  
  74. private static Token ReadField(string line, int startIndex)
  75. {
  76. var token = QuotedFieldTask.ReadQuotedField(line, startIndex);
  77. return new Token(token.Value.Trim(), token.Position, token.Length);
  78. }
  79.  
  80. public static Token ReadQuotedField(string line, int startIndex)
  81. {
  82. return QuotedFieldTask.ReadQuotedField(line, startIndex);
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement