Back to Repositories

Testing PCRE Regex Pattern Conversion in github-linguist

This test suite validates the PCRE regex pattern conversion functionality in the github-linguist compiler package. It specifically focuses on the fixRegex function which handles hexadecimal character class transformations and escape sequence processing.

Test Coverage Overview

The test coverage encompasses comprehensive validation of regex pattern transformations, particularly focusing on the \h hexadecimal character class conversion.

  • Tests basic string patterns without modifications
  • Validates hexadecimal character class conversions
  • Covers escape sequence handling
  • Tests multiple pattern combinations and positions

Implementation Analysis

The testing approach utilizes Go’s table-driven testing pattern to systematically verify regex transformations. Each test case defines an input regex pattern and its expected output after processing.

  • Uses struct-based test table definition
  • Implements direct comparison validation
  • Leverages Go’s testing package features
  • Employs error reporting with detailed output comparison

Technical Details

  • Testing Framework: Go testing package
  • Test Structure: Table-driven tests
  • Validation Method: Direct string comparison
  • Error Handling: t.Errorf with formatted output
  • Test Environment: Local Go test runner

Best Practices Demonstrated

The test implementation showcases several Go testing best practices, ensuring maintainable and reliable test code.

  • Clear test case organization
  • Descriptive test data structure
  • Consistent error messaging format
  • Isolated test cases
  • Comprehensive edge case coverage

github-linguist/linguist

tools/grammars/compiler/pcre_test.go

            
package compiler

import (
	"testing"
)

func Test_fixRegex(t *testing.T) {
	tests := []struct {
		re   string
		want string
	}{
		{"foobar", "foobar"},
		{`testing\h`, "testing[[:xdigit:]]"},
		{`\htest`, `[[:xdigit:]]test`},
		{`abc\hdef`, `abc[[:xdigit:]]def`},
		{`\\\htest`, `\\[[:xdigit:]]test`},
		{`\\htest`, `\\htest`},
		{`\h\h\h\h`, `[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]`},
		{`abc\hdef\hghi\h`, `abc[[:xdigit:]]def[[:xdigit:]]ghi[[:xdigit:]]`},
	}
	for _, tt := range tests {
		got, _ := fixRegex(tt.re)
		if got != tt.want {
			t.Errorf("fixRegex() got = %v, want %v", got, tt.want)
		}
	}
}