Skip to content

Commit 0f2acdd

Browse files
authored
Merge pull request #1 from test262-utils/parsing-fixes
Parsing fixes
2 parents cbd968f + 45782a2 commit 0f2acdd

File tree

5 files changed

+124
-28
lines changed

5 files changed

+124
-28
lines changed

src/_monkeyYaml.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
mYamlStringValue = re.compile(r"^('|\").*\1$")
1616

1717
def load(str):
18+
return myReadDict(str.splitlines())[1]
19+
20+
def myReadDict(lines, indent=""):
1821
dict = None
1922
key = None
2023
emptyLines = 0
2124

22-
lines = str.splitlines()
2325
while lines:
26+
if not lines[0].startswith(indent):
27+
break
28+
2429
line = lines.pop(0)
2530
if myIsAllSpaces(line):
2631
emptyLines += 1
@@ -32,7 +37,7 @@ def load(str):
3237
dict = {}
3338
key = result.group(1).strip()
3439
value = result.group(2).strip()
35-
(lines, value) = myReadValue(lines, value)
40+
(lines, value) = myReadValue(lines, value, indent)
3641
dict[key] = value
3742
else:
3843
if dict and key and key in dict:
@@ -41,17 +46,22 @@ def load(str):
4146
else:
4247
raise Exception("monkeyYaml is confused at " + line)
4348
emptyLines = 0
44-
return dict
49+
return lines, dict
4550

46-
def myReadValue(lines, value):
47-
if value == ">":
48-
(lines, value) = myMultiline(lines, value)
51+
def myReadValue(lines, value, indent):
52+
if value == ">" or value == "|":
53+
(lines, value) = myMultiline(lines, value == "|")
4954
value = value + "\n"
5055
return (lines, value)
51-
if lines and not value and myMaybeList(lines[0]):
52-
return myMultilineList(lines, value)
53-
else:
54-
return lines, myReadOneLine(value)
56+
if lines and not value:
57+
if myMaybeList(lines[0]):
58+
return myMultilineList(lines, value)
59+
indentMatch = re.match("(" + indent + r"\s+)", lines[0])
60+
if indentMatch:
61+
if ":" in lines[0]:
62+
return myReadDict(lines, indentMatch.group(1))
63+
return myMultiline(lines, False)
64+
return lines, myReadOneLine(value)
5565

5666
def myMaybeList(value):
5767
return mYamlMultilineList.match(value)
@@ -99,20 +109,35 @@ def myFlowList(value):
99109
values = result.group(1).split(",")
100110
return [myReadOneLine(v.strip()) for v in values]
101111

102-
def myMultiline(lines, value):
112+
def myMultiline(lines, preserveNewlines=False):
103113
# assume no explcit indentor (otherwise have to parse value)
104-
value = []
114+
value = ""
105115
indent = myLeadingSpaces(lines[0])
116+
wasEmpty = None
117+
106118
while lines:
107119
line = lines.pop(0)
108-
if myIsAllSpaces(line):
109-
value += ["\n"]
120+
isEmpty = myIsAllSpaces(line)
121+
122+
if isEmpty:
123+
if preserveNewlines:
124+
value += "\n"
110125
elif myLeadingSpaces(line) < indent:
111126
lines.insert(0, line)
112127
break;
113128
else:
114-
value += [line[(indent):]]
115-
value = " ".join(value)
129+
if preserveNewlines:
130+
if wasEmpty != None:
131+
value += "\n"
132+
else:
133+
if wasEmpty == False:
134+
value += " "
135+
elif wasEmpty == True:
136+
value += "\n"
137+
value += line[(indent):]
138+
139+
wasEmpty = isEmpty
140+
116141
return (lines, value)
117142

118143
def myIsAllSpaces(line):

src/test262.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def ReportOutcome(self, long_format):
146146
if self.HasUnexpectedOutcome():
147147
if self.case.IsNegative():
148148
print "=== %s was expected to fail in %s, but didn't ===" % (name, mode)
149-
print "--- expected error: %s ---\n" % self.case.GetNegative()
149+
print "--- expected error: %s ---\n" % self.case.GetNegativeType()
150150
else:
151151
if long_format:
152152
print "=== %s failed in %s ===" % (name, mode)
@@ -246,11 +246,21 @@ def __init__(self, suite, name, full_path, strict_mode):
246246
self.validate()
247247

248248
def NegativeMatch(self, stderr):
249-
neg = re.compile(self.GetNegative())
249+
neg = re.compile(self.GetNegativeType())
250250
return re.search(neg, stderr)
251251

252252
def GetNegative(self):
253-
return self.testRecord['negative']
253+
if not self.IsNegative():
254+
return None
255+
return self.testRecord["negative"]
256+
257+
def GetNegativeType(self):
258+
negative = self.GetNegative()
259+
return negative and negative["type"]
260+
261+
def GetNegativePhase(self):
262+
negative = self.GetNegative()
263+
return negative and negative["phase"]
254264

255265
def GetName(self):
256266
return path.join(*self.name)
@@ -304,6 +314,10 @@ def GetSource(self):
304314
self.GetAdditionalIncludes() + \
305315
self.test + '\n'
306316

317+
if self.GetNegativePhase() == "early":
318+
source = ("throw 'Expected an early error, but code was executed.';\n" +
319+
source)
320+
307321
if self.strict_mode:
308322
source = '"use strict";\nvar strict_mode = true;\n' + source
309323
else:
@@ -363,6 +377,10 @@ def Print(self):
363377

364378
def validate(self):
365379
flags = self.testRecord.get("flags")
380+
phase = self.GetNegativePhase()
381+
382+
if phase not in [None, "early", "runtime"]:
383+
raise TypeError("Invalid value for negative phase: " + phase)
366384

367385
if not flags:
368386
return
@@ -578,7 +596,7 @@ def WriteLog(self, result):
578596
if result.HasUnexpectedOutcome():
579597
if result.case.IsNegative():
580598
self.logf.write("=== %s was expected to fail in %s, but didn't === \n" % (name, mode))
581-
self.logf.write("--- expected error: %s ---\n" % result.case.GetNegative())
599+
self.logf.write("--- expected error: %s ---\n" % result.case.GetNegativeType())
582600
result.WriteOutput(self.logf)
583601
else:
584602
self.logf.write("=== %s failed in %s === \n" % (name, mode))

test/fixtures/negative.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// fake copyright comment
2+
/*---
3+
info: >
4+
Sample test info
5+
description: Sample test description
6+
negative:
7+
phase: early
8+
type: SyntaxError
9+
---*/
10+
11+
???

test/test_monkeyYaml.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,21 @@ def test_Multiline_1(self):
4949

5050
def test_Multiline_2(self):
5151
lines = [" foo", " bar"]
52-
value = ">"
53-
y = "\n".join([value] + lines)
54-
(lines, value) = monkeyYaml.myMultiline(lines, value)
52+
y = "\n".join([">"] + lines)
53+
(lines, value) = monkeyYaml.myMultiline(lines)
5554
self.assertEqual(lines, [])
5655
self.assertEqual(value, yaml.load(y))
5756

5857
def test_Multiline_3(self):
5958
lines = [" foo", " bar"]
60-
value = ">"
61-
y = "\n".join([value] + lines)
62-
(lines, value) = monkeyYaml.myMultiline(lines, value)
59+
y = "\n".join([">"] + lines)
60+
(lines, value) = monkeyYaml.myMultiline(lines)
6361
self.assertEqual(lines, [])
6462
self.assertEqual(value, yaml.load(y))
6563

6664
def test_Multiline_4(self):
6765
lines = [" foo", " bar", " other: 42"]
68-
value = ">"
69-
(lines, value) = monkeyYaml.myMultiline(lines, value)
66+
(lines, value) = monkeyYaml.myMultiline(lines)
7067
self.assertEqual(lines, [" other: 42"])
7168
self.assertEqual(value, "foo bar")
7269

@@ -169,6 +166,45 @@ def test_line_folding_4(self):
169166
"""
170167
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
171168

169+
def test_no_folding(self):
170+
y = """
171+
description: |
172+
This is text that, naively parsed, would appear
173+
174+
to: have
175+
nested: data
176+
"""
177+
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
178+
179+
def test_value_multiline(self):
180+
y = """
181+
description:
182+
This is a multi-line value
183+
184+
whose trailing newline should be stripped
185+
"""
186+
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
187+
188+
def test_nested_1(self):
189+
y = """
190+
es61d: 19.1.2.1
191+
negative:
192+
stage: early
193+
type: ReferenceError
194+
description: foo
195+
"""
196+
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
197+
198+
def test_nested_2(self):
199+
y = """
200+
es61d: 19.1.2.1
201+
first:
202+
second_a:
203+
third: 1
204+
second_b: 3
205+
description: foo
206+
"""
207+
self.assertEqual(monkeyYaml.load(y), yaml.load(y))
172208

173209
if __name__ == '__main__':
174210
unittest.main()

test/test_parseTestRecord.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ def test_overview(self):
171171
172172
""", record['test'])
173173

174+
def test_negative(self):
175+
name = 'fixtures/negative.js'
176+
contents = slurpFile(name)
177+
record = parseTestRecord(contents, name)
174178

179+
self.assertEqual('early', record['negative']['phase'])
180+
self.assertEqual('SyntaxError', record['negative']['type'])
175181

176182
if __name__ == '__main__':
177183
unittest.main()

0 commit comments

Comments
 (0)