summaryrefslogtreecommitdiff
path: root/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Mocks/ParserMock.cs
blob: 3dba8b2cb3aa7b9071feb3df10991ab9b6f247d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2009-2022 Josh Close
// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0.
// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0.
// https://github.com/JoshClose/CsvHelper
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using CsvHelper.Configuration;
using System.Threading.Tasks;
using System.Globalization;
using System.Linq;

namespace CsvHelper.Tests.Mocks
{
	public class ParserMock : IParser, IEnumerable<string[]>
	{
		private readonly Queue<string[]> records = new Queue<string[]>();
		private string[] record;
		private int row;

		public CsvContext Context { get; private set; }

		public IParserConfiguration Configuration { get; private set; }

		public int Count => record?.Length ?? 0;

		public string[] Record => record;

		public string RawRecord => string.Empty;

		public int Row => row;

		public int RawRow => row;

		public long ByteCount => 0;

		public long CharCount => 0;

		public string Delimiter => Configuration.Delimiter;

		public string this[int index] => record[index];

		public ParserMock() : this(new CsvConfiguration(CultureInfo.InvariantCulture)) { }

		public ParserMock(CsvConfiguration configuration)
		{
			Configuration = configuration;
			Context = new CsvContext(this);
		}

		public bool Read()
		{
			if (records.Count == 0)
			{
				return false;
			}

			row++;
			record = records.Dequeue();

			return true;
		}

		public Task<bool> ReadAsync()
		{
			row++;
			record = records.Dequeue();

			return Task.FromResult(records.Count > 0);
		}

		public void Dispose()
		{
		}

		#region Mock Methods

		public void Add(params string[] record)
		{
			records.Enqueue(record);
		}

		public IEnumerator<string[]> GetEnumerator()
		{
			return records.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion Mock Methods
	}
}