82 lines
2.6 KiB
Python
82 lines
2.6 KiB
Python
"""Tests for ShellGenius explainer module."""
|
|
|
|
from shellgenius.explainer import ShellExplainer, explain_script
|
|
|
|
|
|
class TestShellExplainer:
|
|
"""Test shell script explainer."""
|
|
|
|
def test_explainer_initialization(self):
|
|
"""Test explainer creates properly."""
|
|
explainer = ShellExplainer()
|
|
assert explainer.parser is not None
|
|
|
|
def test_basic_explain(self):
|
|
"""Test basic script explanation."""
|
|
script = "#!/bin/bash
|
|
echo \"Hello, World!\"
|
|
ls -la
|
|
"
|
|
result = explain_script(script, detailed=False)
|
|
|
|
assert result.shell_type == "bash"
|
|
assert len(result.line_explanations) > 0
|
|
assert result.overall_purpose != ""
|
|
|
|
def test_detect_keywords(self):
|
|
"""Test keyword detection."""
|
|
explainer = ShellExplainer()
|
|
|
|
if_explanation = explainer._explain_line_basic("if [ -f file ]; then")
|
|
assert "conditional" in if_explanation.lower()
|
|
|
|
for_explanation = explainer._explain_line_basic("for i in 1 2 3; do")
|
|
assert "loop" in for_explanation.lower()
|
|
|
|
def test_common_patterns(self):
|
|
"""Test common pattern detection."""
|
|
explainer = ShellExplainer()
|
|
|
|
shebang_explanation = explainer._explain_line_basic("#!/bin/bash")
|
|
assert "shebang" in shebang_explanation.lower()
|
|
|
|
pipe_explanation = explainer._explain_line_basic("cat file | grep pattern")
|
|
assert "pipe" in pipe_explanation.lower()
|
|
|
|
def test_generate_summary(self):
|
|
"""Test summary generation."""
|
|
explainer = ShellExplainer()
|
|
|
|
from shellgenius.explainer import LineExplanation
|
|
|
|
explanations = [
|
|
LineExplanation(1, "cmd1", "command", True),
|
|
LineExplanation(2, "cmd2", "command", True),
|
|
LineExplanation(3, "function test()", "function", True),
|
|
]
|
|
|
|
summary = explainer._generate_summary(explanations, "bash")
|
|
assert "bash" in summary
|
|
assert "function" in summary.lower()
|
|
|
|
def test_detect_purpose(self):
|
|
"""Test purpose detection."""
|
|
explainer = ShellExplainer()
|
|
|
|
from shellgenius.explainer import LineExplanation
|
|
|
|
git_explanations = [
|
|
LineExplanation(1, "git status", "command", True),
|
|
LineExplanation(2, "git commit -m", "command", True),
|
|
]
|
|
|
|
purpose = explainer._detect_purpose(git_explanations)
|
|
assert "Git" in purpose
|
|
|
|
def test_explain_script_function(self):
|
|
"""Test convenience function."""
|
|
result = explain_script("echo test", detailed=False)
|
|
assert result is not None
|
|
assert hasattr(result, "shell_type")
|
|
assert hasattr(result, "line_explanations")
|