diff --git a/gitignore_generator/tests/test_validator.py b/gitignore_generator/tests/test_validator.py new file mode 100644 index 0000000..f43c981 --- /dev/null +++ b/gitignore_generator/tests/test_validator.py @@ -0,0 +1,107 @@ +"""Tests for validator.""" + +import pytest + +from gitignore_generator.validator import Validator, ValidationIssue + + +class TestValidator: + """Tests for Validator class.""" + + @pytest.fixture + def validator(self): + """Create validator instance.""" + return Validator() + + def test_valid_content(self, validator): + """Test validation of valid content.""" + content = "__pycache__/\n*.pyc\nnode_modules/\n" + summary = validator.get_summary(content) + assert summary["valid"] is True + assert summary["errors"] == 0 + + def test_trailing_slash_warning(self, validator): + """Test detection of trailing slashes.""" + content = "dir/\n" + summary = validator.get_summary(content) + assert summary["warnings"] >= 1 + has_trailing = any( + "trailing_slash" in issue.issue_type + for issue in summary["issues"] + ) + assert has_trailing + + def test_duplicate_pattern_warning(self, validator): + """Test detection of duplicate patterns.""" + content = "__pycache__\n__pycache__\n" + summary = validator.get_summary(content) + assert summary["warnings"] >= 1 + + def test_backslash_escape_error(self, validator): + """Test detection of invalid backslash escapes.""" + content = "test\\nfile\n" + summary = validator.get_summary(content) + assert summary["errors"] >= 1 + + def test_double_star_warning(self, validator): + """Test detection of problematic double star usage.""" + content = "test**/file\n" + summary = validator.get_summary(content) + assert summary["warnings"] >= 1 + + def test_double_negation_warning(self, validator): + """Test detection of consecutive negations.""" + content = "!file.txt\n!other.txt\n" + summary = validator.get_summary(content) + assert summary["warnings"] >= 1 + + def test_validate_pattern_valid(self, validator): + """Test validating a single valid pattern.""" + issue = validator.validate_pattern("*.pyc") + assert issue is None + + def test_validate_pattern_invalid(self, validator): + """Test validating an invalid pattern.""" + issue = validator.validate_pattern("test\\n") + assert issue is not None + assert issue.severity == "error" + + def test_comments_ignored(self, validator): + """Test that comments are ignored.""" + content = "# This is a comment\n__pycache__/\n" + summary = validator.get_summary(content) + assert summary["valid"] is True + + def test_empty_lines_ignored(self, validator): + """Test that empty lines are ignored.""" + content = "\n\n\n__pycache__/\n\n" + summary = validator.get_summary(content) + assert summary["valid"] is True + + def test_is_valid_method(self, validator): + """Test is_valid method.""" + assert validator.is_valid("*.pyc") is True + assert validator.is_valid("test\\n") is False + + def test_validation_issue_line_number(self, validator): + """Test that line numbers are correct in issues.""" + content = "valid_line.pyc\ntest\\n\n" + summary = validator.get_summary(content) + backslash_issue = next( + (i for i in summary["issues"] if i.issue_type == "invalid_escape"), + None + ) + assert backslash_issue is not None + assert backslash_issue.line_number == 2 + + def test_empty_content_valid(self, validator): + """Test that empty content is valid.""" + summary = validator.get_summary("") + assert summary["valid"] is True + assert summary["errors"] == 0 + + def test_only_comments_valid(self, validator): + """Test that only comments is valid.""" + content = "# Comment 1\n# Comment 2\n" + summary = validator.get_summary(content) + assert summary["valid"] is True