summaryrefslogtreecommitdiff
path: root/tests.sh
blob: 9ed8e9e905ca7e027f69461f68832200da2f0414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash

CREP="./crep"
TEST_DIR="tests"

if [ ! -f "$CREP" ]; then
    echo "Error: crep binary not found. Please run 'make' first."
    exit 1
fi

failed=0

run_test() {
    local label=$1
    local search_term=$2
    local file=$3
    local expected_pattern=$4

    printf "Testing %-50s " "$label ($search_term)"
    output=$($CREP "$search_term" "$file")
    
    if echo "$output" | grep -q "$expected_pattern"; then
        echo "PASSED"
    else
        echo "FAILED"
        echo "  Expected pattern: $expected_pattern"
        echo "  Actual output: $output"
        failed=$((failed + 1))
    fi
}

run_test_with_flags() {
    local label=$1
    local flags=$2
    local search_term=$3
    local file=$4
    local expected_pattern=$5

    printf "Testing %-50s " "$label ($flags $search_term)"
    output=$($CREP $flags "$search_term" "$file")

    if echo "$output" | grep -q "$expected_pattern"; then
        echo "PASSED"
    else
        echo "FAILED"
        echo "  Expected pattern: $expected_pattern"
        echo "  Actual output: $output"
        failed=$((failed + 1))
    fi
}

echo "Starting tests..."
echo "----------------"

# C Tests
run_test "C Function" "hello" "$TEST_DIR/test.c" "void hello ()"
run_test "C Params" "add" "$TEST_DIR/test.c" "int add (int a, int b)"
run_test "C Pointer" "get_pointer" "$TEST_DIR/test.c" "int get_pointer (int\* x)"
run_test "C Proto" "declared_only" "$TEST_DIR/test.c" "void declared_only (int x)"

# Python Tests
run_test "Python Func" "hello" "$TEST_DIR/test.py" "def hello ()"
run_test "Python Params" "add" "$TEST_DIR/test.py" "def add (a, b)"
run_test "Python Complex" "complex_function" "$TEST_DIR/test.py" "def complex_function (a, b, c=None, \*args, \*\*kwargs)"
run_test "Python Method" "method_one" "$TEST_DIR/test.py" "def method_one (self)"

# PHP Tests
run_test "PHP Func" "simple_function" "$TEST_DIR/test.php" "function simple_function"
run_test "PHP Method" "myMethod" "$TEST_DIR/test.php" "function myMethod"
run_test "PHP Class" "MyClass" "$TEST_DIR/test.php" "class MyClass"
run_test "PHP Const" "GLOBAL_CONST" "$TEST_DIR/test.php" "GLOBAL_CONST = 1"

# Rust Tests
run_test "Rust Func" "add" "$TEST_DIR/test.rs" "fn add (a: i32, b: i32)"
run_test "Rust Struct" "Point" "$TEST_DIR/test.rs" "struct Point"
run_test "Rust Enum" "Direction" "$TEST_DIR/test.rs" "enum Direction"
run_test "Rust Trait" "Describe" "$TEST_DIR/test.rs" "trait Describe"

# Go Tests
run_test "Go Func" "Hello" "$TEST_DIR/test.go" "func Hello ()"
run_test "Go Method" "Describe" "$TEST_DIR/test.go" "func Describe (p Point)"
run_test "Go Struct" "Point" "$TEST_DIR/test.go" "type Point struct"
run_test "Go Interface" "Describer" "$TEST_DIR/test.go" "type Describer interface"
run_test "Go Const" "MaxValue" "$TEST_DIR/test.go" "const MaxValue"

# JavaScript Tests
run_test "JS Func" "hello" "$TEST_DIR/test.js" "function hello ()"
run_test "JS Params" "add" "$TEST_DIR/test.js" "function add (a, b)"
run_test "JS Class" "MyClass" "$TEST_DIR/test.js" "class MyClass"
run_test "JS Method" "myMethod" "$TEST_DIR/test.js" "myMethod (x)"
run_test "JS Object Method" "shortMethod" "$TEST_DIR/test.js" "shortMethod (a)"

# C++ Tests
run_test "C++ Func" "global_hello" "$TEST_DIR/test.cpp" "void global_hello ()"
run_test "C++ Namespace" "my_namespace" "$TEST_DIR/test.cpp" "namespace my_namespace"
run_test "C++ Class" "MyClass" "$TEST_DIR/test.cpp" "class MyClass"
run_test "C++ Struct" "MyStruct" "$TEST_DIR/test.cpp" "struct MyStruct"
run_test "C++ Method" "myMethod" "$TEST_DIR/test.cpp" "void myMethod ()"

# Case Sensitivity Tests
run_test "Default Case Insensitive" "foo" "tests/test.c" "void FooBar"
run_test_with_flags "Case Sensitive -c" "-c" "foobar" "tests/test.c" "void foobar"

# Depth Tests
run_test_with_flags "Depth 0 (root)" "-d 0" "level" "tests/depth_test" "void level0"
run_test_with_flags "Depth 1 (recursive)" "-d 1" "level" "tests/depth_test" "void level1"

# Levenshtein Distance Tests
run_test_with_flags "Levenshtein -l 1 match" "-l 1" "heelo" "$TEST_DIR/test.c" "void hello ()"
run_test_with_flags "Levenshtein -l 2 match" "-l 2" "heloo" "$TEST_DIR/test.c" "void hello ()"

echo "----------------"
if [ $failed -eq 0 ]; then
    echo "All tests passed!"
    exit 0
else
    echo "$failed tests failed."
    exit 1
fi