Skip to content

Commit

Permalink
Py slang test (#29)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hyizhak and JJtan2002 authored Apr 12, 2024
1 parent 3f50a99 commit ef683df
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export function parsePythonToEstreeAst(code: string,
return translator.resolve(ast) as unknown as Program
}


export * from './errors';

// import {ParserErrors, ResolverErrors, TokenizerErrors} from "./errors";
Expand Down
194 changes: 182 additions & 12 deletions src/tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ else:
expect(toPythonAst(text)).toMatchObject({})
})
test('Nested ternary', () => {
const text = `1 if A else 2 if B else 3\n`;
const text = `1 if a else 2 if b else 3\n`;
expect(toPythonAst(text)).toMatchObject({})
})
});
Expand Down Expand Up @@ -113,6 +113,7 @@ def y(a, b, c):
`
expect(toPythonAst(text)).toMatchObject({});
});

// @TODO fix me
// test('Function definition empty lines', () => {
// const text = `\
Expand All @@ -136,18 +137,187 @@ def y(a, b, c):
});

describe('Conditional statements', () => {
test('If-elif-else', () => {
test('If-else statement', () => {
const text = `
if x:
pass
elif y:
pass
elif z:
pass
if x > 10:
print("x is greater than 10")
else:
pass
print("x is less than or equal to 10")
`;
expect(toPythonAst(text)).toMatchObject({})
})
})
})
});

test('If-elif-else statement', () => {
const text = `
if x > 10:
print("x is greater than 10")
elif x == 10:
print("x is equal to 10")
else:
print("x is less than 10")
`;
expect(toPythonAst(text)).toMatchObject({})
});
});

describe('N-base numbers', () => {
test('Binary number', () => {
const text = `0b101010\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Octal number', () => {
const text = `0o1234567\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Hexadecimal number', () => {
const text = `0xabcdef\n`;
expect(toPythonAst(text)).toMatchObject({})
});
});

describe('Binary operators', () => {
test('Addition', () => {
const text = `1 + 1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Large Number Addition', () => {
const text = `100000000 ** 100000000 + 1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Subtraction', () => {
const text = `1 - 1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Multiplication', () => {
const text = `1 * 1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Large Number Multiplication', () => {
const text = `100000000 ** 100000000 * 5\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Division', () => {
const text = `1 / 1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Modulus', () => {
const text = `1 % 1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Exponent', () => {
const text = `2 ** 2\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Less than', () => {
const text = `1 < 2\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Greater than', () => {
const text = `2 > 1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Less than or equal to', () => {
const text = `1 <= 2\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Greater than or equal to', () => {
const text = `2 >= 1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Equality', () => {
const text = `1 == 2\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Inequality', () => {
const text = `1 != 2\n`;
expect(toPythonAst(text)).toMatchObject({})
});
});

describe('Unary operators', () => {
test('Negation', () => {
const text = `-1\n`;
expect(toPythonAst(text)).toMatchObject({})
});

// TODO: FIX THIS
// test('Logical NOT', () => {
// const text = `not 1\n`;
// expect(toPythonAst(text)).toMatchObject({})
// });
});

describe('Binary logical operators', () => {
test('Logical AND', () => {
const text = `1 and 2\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Logical OR', () => {
const text = `1 or 2\n`;
expect(toPythonAst(text)).toMatchObject({})
});
});

describe('Complex expressions', () => {
test('Nested function call', () => {
const text = `
def f1(x, y):
return 1
def f2(x, y):
return y
f1(f2(1, 2), 2)
`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Binary operation with parentheses', () => {
const text = `(1 + 2) * 3\n`;
expect(toPythonAst(text)).toMatchObject({})
});
});

describe('Primitive expressions', () => {
test('Number literal', () => {
const text = `42\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Large Number literal', () => {
const text = `1000000000 ** 100000000\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Boolean literal True', () => {
const text = `True\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('Boolean literal False', () => {
const text = `False\n`;
expect(toPythonAst(text)).toMatchObject({})
});

test('String literal', () => {
const text = `"Hello, World!"\n`;
expect(toPythonAst(text)).toMatchObject({})
});
});
});

0 comments on commit ef683df

Please sign in to comment.