Skip to content

Commit ef683df

Browse files
hyizhakJJtan2002
andauthored
Py slang test (#29)
* scoping rules in resolver implemented * Tests for big number, n-base number and other behaviors in the spec * minor fix * cleanup * Update parser.test.ts --------- Co-authored-by: JJtan <95962077+JJtan2002@users.noreply.github.com>
1 parent 3f50a99 commit ef683df

File tree

2 files changed

+183
-12
lines changed

2 files changed

+183
-12
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export function parsePythonToEstreeAst(code: string,
150150
return translator.resolve(ast) as unknown as Program
151151
}
152152

153+
153154
export * from './errors';
154155

155156
// import {ParserErrors, ResolverErrors, TokenizerErrors} from "./errors";

src/tests/parser.test.ts

Lines changed: 182 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ else:
7171
expect(toPythonAst(text)).toMatchObject({})
7272
})
7373
test('Nested ternary', () => {
74-
const text = `1 if A else 2 if B else 3\n`;
74+
const text = `1 if a else 2 if b else 3\n`;
7575
expect(toPythonAst(text)).toMatchObject({})
7676
})
7777
});
@@ -113,6 +113,7 @@ def y(a, b, c):
113113
`
114114
expect(toPythonAst(text)).toMatchObject({});
115115
});
116+
116117
// @TODO fix me
117118
// test('Function definition empty lines', () => {
118119
// const text = `\
@@ -136,18 +137,187 @@ def y(a, b, c):
136137
});
137138

138139
describe('Conditional statements', () => {
139-
test('If-elif-else', () => {
140+
test('If-else statement', () => {
140141
const text = `
141-
if x:
142-
pass
143-
elif y:
144-
pass
145-
elif z:
146-
pass
142+
if x > 10:
143+
print("x is greater than 10")
147144
else:
148-
pass
145+
print("x is less than or equal to 10")
149146
`;
150147
expect(toPythonAst(text)).toMatchObject({})
151-
})
152-
})
153-
})
148+
});
149+
150+
test('If-elif-else statement', () => {
151+
const text = `
152+
if x > 10:
153+
print("x is greater than 10")
154+
elif x == 10:
155+
print("x is equal to 10")
156+
else:
157+
print("x is less than 10")
158+
`;
159+
expect(toPythonAst(text)).toMatchObject({})
160+
});
161+
});
162+
163+
describe('N-base numbers', () => {
164+
test('Binary number', () => {
165+
const text = `0b101010\n`;
166+
expect(toPythonAst(text)).toMatchObject({})
167+
});
168+
169+
test('Octal number', () => {
170+
const text = `0o1234567\n`;
171+
expect(toPythonAst(text)).toMatchObject({})
172+
});
173+
174+
test('Hexadecimal number', () => {
175+
const text = `0xabcdef\n`;
176+
expect(toPythonAst(text)).toMatchObject({})
177+
});
178+
});
179+
180+
describe('Binary operators', () => {
181+
test('Addition', () => {
182+
const text = `1 + 1\n`;
183+
expect(toPythonAst(text)).toMatchObject({})
184+
});
185+
186+
test('Large Number Addition', () => {
187+
const text = `100000000 ** 100000000 + 1\n`;
188+
expect(toPythonAst(text)).toMatchObject({})
189+
});
190+
191+
test('Subtraction', () => {
192+
const text = `1 - 1\n`;
193+
expect(toPythonAst(text)).toMatchObject({})
194+
});
195+
196+
test('Multiplication', () => {
197+
const text = `1 * 1\n`;
198+
expect(toPythonAst(text)).toMatchObject({})
199+
});
200+
201+
test('Large Number Multiplication', () => {
202+
const text = `100000000 ** 100000000 * 5\n`;
203+
expect(toPythonAst(text)).toMatchObject({})
204+
});
205+
206+
test('Division', () => {
207+
const text = `1 / 1\n`;
208+
expect(toPythonAst(text)).toMatchObject({})
209+
});
210+
211+
test('Modulus', () => {
212+
const text = `1 % 1\n`;
213+
expect(toPythonAst(text)).toMatchObject({})
214+
});
215+
216+
test('Exponent', () => {
217+
const text = `2 ** 2\n`;
218+
expect(toPythonAst(text)).toMatchObject({})
219+
});
220+
221+
test('Less than', () => {
222+
const text = `1 < 2\n`;
223+
expect(toPythonAst(text)).toMatchObject({})
224+
});
225+
226+
test('Greater than', () => {
227+
const text = `2 > 1\n`;
228+
expect(toPythonAst(text)).toMatchObject({})
229+
});
230+
231+
test('Less than or equal to', () => {
232+
const text = `1 <= 2\n`;
233+
expect(toPythonAst(text)).toMatchObject({})
234+
});
235+
236+
test('Greater than or equal to', () => {
237+
const text = `2 >= 1\n`;
238+
expect(toPythonAst(text)).toMatchObject({})
239+
});
240+
241+
test('Equality', () => {
242+
const text = `1 == 2\n`;
243+
expect(toPythonAst(text)).toMatchObject({})
244+
});
245+
246+
test('Inequality', () => {
247+
const text = `1 != 2\n`;
248+
expect(toPythonAst(text)).toMatchObject({})
249+
});
250+
});
251+
252+
describe('Unary operators', () => {
253+
test('Negation', () => {
254+
const text = `-1\n`;
255+
expect(toPythonAst(text)).toMatchObject({})
256+
});
257+
258+
// TODO: FIX THIS
259+
// test('Logical NOT', () => {
260+
// const text = `not 1\n`;
261+
// expect(toPythonAst(text)).toMatchObject({})
262+
// });
263+
});
264+
265+
describe('Binary logical operators', () => {
266+
test('Logical AND', () => {
267+
const text = `1 and 2\n`;
268+
expect(toPythonAst(text)).toMatchObject({})
269+
});
270+
271+
test('Logical OR', () => {
272+
const text = `1 or 2\n`;
273+
expect(toPythonAst(text)).toMatchObject({})
274+
});
275+
});
276+
277+
describe('Complex expressions', () => {
278+
test('Nested function call', () => {
279+
const text = `
280+
def f1(x, y):
281+
return 1
282+
283+
def f2(x, y):
284+
return y
285+
286+
f1(f2(1, 2), 2)
287+
`;
288+
expect(toPythonAst(text)).toMatchObject({})
289+
});
290+
291+
test('Binary operation with parentheses', () => {
292+
const text = `(1 + 2) * 3\n`;
293+
expect(toPythonAst(text)).toMatchObject({})
294+
});
295+
});
296+
297+
describe('Primitive expressions', () => {
298+
test('Number literal', () => {
299+
const text = `42\n`;
300+
expect(toPythonAst(text)).toMatchObject({})
301+
});
302+
303+
test('Large Number literal', () => {
304+
const text = `1000000000 ** 100000000\n`;
305+
expect(toPythonAst(text)).toMatchObject({})
306+
});
307+
308+
test('Boolean literal True', () => {
309+
const text = `True\n`;
310+
expect(toPythonAst(text)).toMatchObject({})
311+
});
312+
313+
test('Boolean literal False', () => {
314+
const text = `False\n`;
315+
expect(toPythonAst(text)).toMatchObject({})
316+
});
317+
318+
test('String literal', () => {
319+
const text = `"Hello, World!"\n`;
320+
expect(toPythonAst(text)).toMatchObject({})
321+
});
322+
});
323+
});

0 commit comments

Comments
 (0)