Skip to content

Commit ccd4ebf

Browse files
authored
Add iterator tests for ParseErrorElementDictionary (#182)
1 parent cabaf1a commit ccd4ebf

3 files changed

Lines changed: 101 additions & 5 deletions

File tree

Sources/SynKit/Libraries/Parser/ParseErrorElementDictionary.cs

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public ParseErrorElementDictionary(string key, ParseErrorElement value)
3535
? this.elements is null
3636
? throw new KeyNotFoundException($"The key {key} was not found in the dictionary")
3737
: this.elements[key]
38-
: this.firstItem!;
38+
: this.firstKey == key
39+
? this.firstItem!
40+
: throw new KeyNotFoundException($"The key {key} was not found in the dictionary");
3941

4042
public IEnumerable<string> Keys => this.firstKey is null ? this.elements!.Keys : new[] { this.firstKey };
4143

@@ -44,8 +46,27 @@ public ParseErrorElementDictionary(string key, ParseErrorElement value)
4446
public int Count => this.firstKey is null ? this.elements is null ? 0 : this.elements.Count : 1;
4547

4648
public bool ContainsKey(string key) => this.firstKey is null ? this.elements is null ? false : this.elements.ContainsKey(key) : this.firstKey == key;
47-
public IEnumerator<KeyValuePair<string, ParseErrorElement>> GetEnumerator() => throw new NotImplementedException();
48-
public bool TryGetValue(string key, out ParseErrorElement value) => throw new NotImplementedException();
49+
public IEnumerator<KeyValuePair<string, ParseErrorElement>> GetEnumerator() =>
50+
this.elements is null
51+
? new Enumerator(this.firstKey!, this.firstItem!)
52+
: this.elements.GetEnumerator();
53+
public bool TryGetValue(string key, out ParseErrorElement value)
54+
{
55+
if (this.elements is null)
56+
{
57+
if (this.firstKey == key)
58+
{
59+
value = firstItem;
60+
return true;
61+
}
62+
63+
value = default;
64+
return false;
65+
}
66+
67+
return this.elements.TryGetValue(key, out value);
68+
}
69+
4970
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
5071

5172
public ParseErrorElementDictionary Merge(ParseErrorElementDictionary other)
@@ -91,4 +112,48 @@ public ParseErrorElementDictionary Merge(ParseErrorElementDictionary other)
91112

92113
return new (elements);
93114
}
115+
116+
private struct Enumerator : IEnumerator<KeyValuePair<string, ParseErrorElement>>
117+
{
118+
private bool valid;
119+
private KeyValuePair<string, ParseErrorElement> _current;
120+
public Enumerator(string key, ParseErrorElement value)
121+
{
122+
this._current = new KeyValuePair<string, ParseErrorElement>(key, value);
123+
}
124+
125+
public KeyValuePair<string, ParseErrorElement> Current
126+
{
127+
get
128+
{
129+
if (!valid)
130+
{
131+
throw new InvalidOperationException("The enumerator is not valid.");
132+
}
133+
134+
return _current;
135+
}
136+
}
137+
138+
object IEnumerator.Current => this.Current;
139+
140+
public void Dispose() {}
141+
public bool MoveNext()
142+
{
143+
if (!this.valid)
144+
{
145+
this.valid = true;
146+
return true;
147+
}
148+
else
149+
{
150+
this.valid = false;
151+
return false;
152+
}
153+
}
154+
public void Reset()
155+
{
156+
this.valid = false;
157+
}
158+
}
94159
}

Sources/SynKit/Tests/Parser.Tests/ExpressionParserTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ internal partial class Parser
110110
[InlineData(1, "(2 * 3 + 1 << 1 > 1 == 1 & 123 | 22 && 1 || 0) || (2 * 3 + 1 << 1 > 1 == 1 & 123 | 22 && 1 || 0)")]
111111
public void Tests(int value, string text) => Assert.Equal(value, Eval(text));
112112

113-
[Fact(Timeout = 1000, Skip = "In order for this test to pass, BNF desuar should be refactored")]
113+
[Fact]
114114
public async System.Threading.Tasks.Task LongExpressionParsedWithinExpectedTime()
115115
{
116116
await Task.Yield();
117117
var text = "(((((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))+(((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))))))))+((((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))+(((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))))))))) + (((((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))+(((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))))))))+((((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))+(((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))))";
118-
Assert.Equal(1536, Eval(text));
118+
var result = Eval(text);
119+
Assert.Equal(1536, result);
119120
}
120121
}

Sources/SynKit/Tests/Parser.Tests/ParseErrorTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33
// Source repository: https://github.com/LanguageDev/Yoakke
44

5+
using System.Linq;
56
using Xunit;
67

78
namespace Yoakke.SynKit.Parser.Tests;
@@ -82,4 +83,33 @@ public void MergeTwoErrorsFromDifferentExpressions()
8283
Assert.Equal(1, result.Elements["other_expression"].Expected.Count);
8384
Assert.True(result.Elements["other_expression"].Expected.Contains("^"));
8485
}
86+
87+
[Fact]
88+
public void EnumerateWhenSingleElement()
89+
{
90+
var firstError = new ParseError("^", null, 12, "expression");
91+
92+
var result = firstError.Elements.Single();
93+
94+
Assert.Equal("expression", result.Key);
95+
Assert.Equal("expression", result.Value.Context);
96+
Assert.True(result.Value.Expected.Contains("^"));
97+
}
98+
99+
[Fact]
100+
public void EnumerateTwoElements()
101+
{
102+
var firstError = new ParseError("^", null, 12, "expression");
103+
var secondError = new ParseError("^", null, 12, "other_expression");
104+
var mergedError = firstError | secondError;
105+
106+
var result = mergedError.Elements.ToList();
107+
108+
Assert.Equal("expression", result[0].Key);
109+
Assert.Equal("expression", result[0].Value.Context);
110+
Assert.True(result[0].Value.Expected.Contains("^"));
111+
Assert.Equal("other_expression", result[1].Key);
112+
Assert.Equal("other_expression", result[1].Value.Context);
113+
Assert.True(result[1].Value.Expected.Contains("^"));
114+
}
85115
}

0 commit comments

Comments
 (0)