Files
Sheerka-Old/tests/core/test_ast_helper.py
kodjo cac2dad17f Implemented some enhancement request and fixed some bugs
Fixed #2 : Variables are not recognized when inside a rule token
Fixed #15 : Rule: rete attributes are lost when a new ontology is created
Fixed #14 : ReteNetwork: Format rules must not be added to Rete network
Fixed #16 : DefConcept: Variables are not recognized when they are keyword arguments
Fixed #4 : Comparison are not correctly set when comparison property is a concept
Fixed #14 : Parser: merge FunctionParser.NamesNode and ExpressionParser.NamesNode
Fixed #18 : Parser: Add SourceCodeNode test to UnrecognizedNodeParser
Fixed #20 : At startup Number concept is saved in db a numerous number of time
Fixed #21 : CacheManager: I can remove all elements from a ListIfNeededCache and fill it again
Fixed #22 : CacheManager: I can remove all elements from a SetCache and fill it again
Fixed #23 : HistoryManager: history() no longer works
Fixed #24 : HistoryManager: history() no longer works after creating an exec rule
Fixed #25 : SheerkaMemory: Use MemoryObject instead of sheerka.local
Fixed #26 : Debugger: add the list all available services..
Fixed #27 : CONCEPTS_GRAMMARS_ENTRY does not seems to be in use any more
Fixed #28 : Give order to services
2021-02-12 15:15:31 +01:00

46 lines
1.5 KiB
Python

import ast
import pytest
from core.ast_helpers import UnreferencedNamesVisitor, UnreferencedVariablesVisitor
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestAstHelper(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("source, expected", [
("a,b", {"a", "b"}),
("isinstance(a, int)", {"isinstance", "a", "int"}),
("date.today()", {"date"}),
("test()", {"test"}),
("sheerka.test()", {"sheerka"}),
("for i in range(10): pass", set()),
("func(x=a, y=b)", {"func", "a", "b"}),
])
def test_i_can_get_unreferenced_names_from_simple_expressions(self, source, expected):
context = self.get_context()
ast_ = ast.parse(source)
visitor = UnreferencedNamesVisitor(context)
visitor.visit(ast_)
assert visitor.names == expected
@pytest.mark.parametrize("source, expected", [
("a,b", {"a", "b"}),
("isinstance(a, int)", {"a", "int"}),
("date.today()", set()),
("test()", set()),
("sheerka.test()", set()),
("for i in range(10): pass", set()),
("func(x=a, y=b)", {"a", "b", "x", "y"}),
])
def test_i_can_get_unreferenced_variables_from_simple_expressions(self, source, expected):
context = self.get_context()
ast_ = ast.parse(source)
visitor = UnreferencedVariablesVisitor(context)
visitor.visit(ast_)
assert visitor.names == expected