|
| 1 | +"""Tests for the Vyper contract parser.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from vyper_sphinx_docs.parser import VyperParser, Contract, Function, Parameter |
| 5 | + |
| 6 | + |
| 7 | +def test_parse_contracts(contracts_dir): |
| 8 | + """Test parsing multiple contracts.""" |
| 9 | + parser = VyperParser(str(contracts_dir)) |
| 10 | + contracts = parser.parse_contracts() |
| 11 | + |
| 12 | + assert len(contracts) == 2 |
| 13 | + assert any(c.name == "token" for c in contracts) |
| 14 | + assert any(c.name == "nested_token" for c in contracts) |
| 15 | + |
| 16 | + |
| 17 | +def test_contract_parsing(contracts_dir): |
| 18 | + """Test detailed contract parsing.""" |
| 19 | + parser = VyperParser(str(contracts_dir)) |
| 20 | + contracts = parser.parse_contracts() |
| 21 | + contract = next(c for c in contracts if c.name == "token") |
| 22 | + |
| 23 | + # Test contract properties |
| 24 | + assert isinstance(contract, Contract) |
| 25 | + assert contract.docstring is not None |
| 26 | + assert "ERC20 Token Implementation" in contract.docstring |
| 27 | + |
| 28 | + # Test functions |
| 29 | + assert len(contract.functions) == 2 |
| 30 | + transfer_func = next(f for f in contract.functions if f.name == "transfer") |
| 31 | + balance_func = next(f for f in contract.functions if f.name == "balance_of") |
| 32 | + |
| 33 | + # Test transfer function |
| 34 | + assert isinstance(transfer_func, Function) |
| 35 | + assert len(transfer_func.params) == 2 |
| 36 | + assert transfer_func.return_type == "bool" |
| 37 | + assert transfer_func.docstring is not None |
| 38 | + assert "Transfer tokens" in transfer_func.docstring |
| 39 | + |
| 40 | + # Test balance_of function |
| 41 | + assert isinstance(balance_func, Function) |
| 42 | + assert len(balance_func.params) == 1 |
| 43 | + assert balance_func.return_type == "uint256" |
| 44 | + assert balance_func.docstring is not None |
| 45 | + assert "Get the token balance" in balance_func.docstring |
| 46 | + |
| 47 | + |
| 48 | +def test_parameter_parsing(contracts_dir): |
| 49 | + """Test function parameter parsing.""" |
| 50 | + parser = VyperParser(str(contracts_dir)) |
| 51 | + contracts = parser.parse_contracts() |
| 52 | + contract = next(c for c in contracts if c.name == "token") |
| 53 | + transfer_func = next(f for f in contract.functions if f.name == "transfer") |
| 54 | + |
| 55 | + # Test parameters |
| 56 | + assert len(transfer_func.params) == 2 |
| 57 | + to_param = transfer_func.params[0] |
| 58 | + amount_param = transfer_func.params[1] |
| 59 | + |
| 60 | + assert isinstance(to_param, Parameter) |
| 61 | + assert to_param.name == "to" |
| 62 | + assert to_param.type == "address" |
| 63 | + |
| 64 | + assert isinstance(amount_param, Parameter) |
| 65 | + assert amount_param.name == "amount" |
| 66 | + assert amount_param.type == "uint256" |
| 67 | + |
| 68 | + |
| 69 | +def test_empty_contract(tmp_path): |
| 70 | + """Test parsing an empty contract.""" |
| 71 | + contracts_dir = tmp_path / "contracts" |
| 72 | + contracts_dir.mkdir() |
| 73 | + |
| 74 | + empty_contract = contracts_dir / "empty.vy" |
| 75 | + empty_contract.write_text("") |
| 76 | + |
| 77 | + parser = VyperParser(str(contracts_dir)) |
| 78 | + contracts = parser.parse_contracts() |
| 79 | + |
| 80 | + assert len(contracts) == 1 |
| 81 | + assert contracts[0].name == "empty" |
| 82 | + assert contracts[0].docstring is None |
| 83 | + assert len(contracts[0].functions) == 0 |
0 commit comments