Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pyslang/tests/test_syntax_tree_equality.py Python tests #1214

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions pyslang/tests/test_syntax_tree_equality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Basic tests checking that syntax tree equality checks work as expected."""

import pyslang


def is_verilog_equal(verilog_left: str, verilog_right: str) -> bool:
"""Check if two Verilog modules are equivalent.

Helper function for repeated check in upcoming tests.
"""
ast_left = pyslang.SyntaxTree.fromText(verilog_left)
ast_right = pyslang.SyntaxTree.fromText(verilog_right)
return (ast_left.root).isEquivalentTo(ast_right.root)


def test_is_verilog_equal_when_equal() -> None:
"""Assert `is_verilog_equal` when the two Verilog modules are equal."""
input_1 = """
module and_gate (
input wire x,
input wire y,
output wire z
);
assign z = x & y;
endmodule
"""

input_2 = """
module and_gate (
input wire x,
input wire y,
output wire z
);
assign z = x & y;
// Comment here
endmodule
"""

assert is_verilog_equal(input_1, input_2) is True


def test_is_verilog_equal_when_not_equal() -> None:
"""Assert `is_verilog_equal` when the two Verilog modules are not equal."""
input_1 = """
module and_gate (
input wire x,
input wire y,
output wire z
);
assign z = x & y;
endmodule
"""

input_2 = """
module or_gate (
input wire x,
input wire y,
output wire z
);
assign z = x | y;
endmodule
"""

assert is_verilog_equal(input_1, input_2) is False


def test_verilog_equality() -> None:
"""Test that two Verilog modules with minor differences are evaluated as equal."""
input_1 = """
module and_gate (
input wire x,
input wire y,
output wire z
);
assign z = x & y;
endmodule
"""

input_2 = """
module and_gate (
input wire x,
input wire y, // Comment here
output wire z
);
// Comment here
assign z = x & y;
/* Another comment here */
endmodule
"""

st1 = pyslang.SyntaxTree.fromText(input_1)
st1_again = pyslang.SyntaxTree.fromText(input_1)
st1_stripped = pyslang.SyntaxTree.fromText(input_1.strip())

assert st1.root.isEquivalentTo(st1_again.root)
assert st1.root.isEquivalentTo(st1_stripped.root)
assert st1_again.root.isEquivalentTo(st1_stripped.root)

st2 = pyslang.SyntaxTree.fromText(input_2)
st2_stripped = pyslang.SyntaxTree.fromText(input_2.strip())

assert st1_stripped.root.isEquivalentTo(st2.root)
assert st1.root.isEquivalentTo(st2.root)
assert st1.root.isEquivalentTo(st2_stripped.root)
Loading