1#!/bin/bash
  2
  3CREP="./crep"
  4TEST_DIR="tests"
  5
  6if [ ! -f "$CREP" ]; then
  7    echo "Error: crep binary not found. Please run 'make' first."
  8    exit 1
  9fi
 10
 11failed=0
 12
 13run_test() {
 14    local label=$1
 15    local search_term=$2
 16    local file=$3
 17    local expected_pattern=$4
 18
 19    printf "Testing %-50s " "$label ($search_term)"
 20    output=$($CREP "$search_term" "$file")
 21    
 22    if echo "$output" | grep -q "$expected_pattern"; then
 23        echo "PASSED"
 24    else
 25        echo "FAILED"
 26        echo "  Expected pattern: $expected_pattern"
 27        echo "  Actual output: $output"
 28        failed=$((failed + 1))
 29    fi
 30}
 31
 32run_test_with_flags() {
 33    local label=$1
 34    local flags=$2
 35    local search_term=$3
 36    local file=$4
 37    local expected_pattern=$5
 38
 39    printf "Testing %-50s " "$label ($flags $search_term)"
 40    output=$($CREP $flags "$search_term" "$file")
 41
 42    if echo "$output" | grep -q "$expected_pattern"; then
 43        echo "PASSED"
 44    else
 45        echo "FAILED"
 46        echo "  Expected pattern: $expected_pattern"
 47        echo "  Actual output: $output"
 48        failed=$((failed + 1))
 49    fi
 50}
 51
 52echo "Starting tests..."
 53echo "----------------"
 54
 55# C Tests
 56run_test "C Function" "hello" "$TEST_DIR/test.c" "void hello ()"
 57run_test "C Params" "add" "$TEST_DIR/test.c" "int add (int a, int b)"
 58run_test "C Pointer" "get_pointer" "$TEST_DIR/test.c" "int get_pointer (int\* x)"
 59run_test "C Proto" "declared_only" "$TEST_DIR/test.c" "void declared_only (int x)"
 60
 61# Python Tests
 62run_test "Python Func" "hello" "$TEST_DIR/test.py" "def hello ()"
 63run_test "Python Params" "add" "$TEST_DIR/test.py" "def add (a, b)"
 64run_test "Python Complex" "complex_function" "$TEST_DIR/test.py" "def complex_function (a, b, c=None, \*args, \*\*kwargs)"
 65run_test "Python Method" "method_one" "$TEST_DIR/test.py" "def method_one (self)"
 66
 67# PHP Tests
 68run_test "PHP Func" "simple_function" "$TEST_DIR/test.php" "function simple_function"
 69run_test "PHP Method" "myMethod" "$TEST_DIR/test.php" "function myMethod"
 70run_test "PHP Class" "MyClass" "$TEST_DIR/test.php" "class MyClass"
 71run_test "PHP Const" "GLOBAL_CONST" "$TEST_DIR/test.php" "GLOBAL_CONST = 1"
 72
 73# Rust Tests
 74run_test "Rust Func" "add" "$TEST_DIR/test.rs" "fn add (a: i32, b: i32)"
 75run_test "Rust Struct" "Point" "$TEST_DIR/test.rs" "struct Point"
 76run_test "Rust Enum" "Direction" "$TEST_DIR/test.rs" "enum Direction"
 77run_test "Rust Trait" "Describe" "$TEST_DIR/test.rs" "trait Describe"
 78
 79# Go Tests
 80run_test "Go Func" "Hello" "$TEST_DIR/test.go" "func Hello ()"
 81run_test "Go Method" "Describe" "$TEST_DIR/test.go" "func Describe (p Point)"
 82run_test "Go Struct" "Point" "$TEST_DIR/test.go" "type Point struct"
 83run_test "Go Interface" "Describer" "$TEST_DIR/test.go" "type Describer interface"
 84run_test "Go Const" "MaxValue" "$TEST_DIR/test.go" "const MaxValue"
 85
 86# JavaScript Tests
 87run_test "JS Func" "hello" "$TEST_DIR/test.js" "function hello ()"
 88run_test "JS Params" "add" "$TEST_DIR/test.js" "function add (a, b)"
 89run_test "JS Class" "MyClass" "$TEST_DIR/test.js" "class MyClass"
 90run_test "JS Method" "myMethod" "$TEST_DIR/test.js" "myMethod (x)"
 91run_test "JS Object Method" "shortMethod" "$TEST_DIR/test.js" "shortMethod (a)"
 92
 93# C++ Tests
 94run_test "C++ Func" "global_hello" "$TEST_DIR/test.cpp" "void global_hello ()"
 95run_test "C++ Namespace" "my_namespace" "$TEST_DIR/test.cpp" "namespace my_namespace"
 96run_test "C++ Class" "MyClass" "$TEST_DIR/test.cpp" "class MyClass"
 97run_test "C++ Struct" "MyStruct" "$TEST_DIR/test.cpp" "struct MyStruct"
 98run_test "C++ Method" "myMethod" "$TEST_DIR/test.cpp" "void myMethod ()"
 99
100# Lua Tests
101run_test "Lua Func" "hello" "$TEST_DIR/test.lua" "function hello"
102run_test "Lua Local Func" "secret_formula" "$TEST_DIR/test.lua" "function secret_formula"
103run_test "Lua Assignment" "myfunc" "$TEST_DIR/test.lua" "function myfunc"
104run_test "Lua Method" "greet" "$TEST_DIR/test.lua" "function MyTable:greet"
105run_test "Lua Nested" "inner" "$TEST_DIR/test.lua" "function inner"
106
107# Zig Tests
108run_test "Zig Func" "main" "$TEST_DIR/test.zig" " main "
109run_test "Zig Params" "add" "$TEST_DIR/test.zig" " add "
110run_test "Zig Struct" "Point" "$TEST_DIR/test.zig" " Point "
111run_test "Zig Enum" "Color" "$TEST_DIR/test.zig" " Color "
112run_test "Zig Error" "MyError" "$TEST_DIR/test.zig" " MyError "
113
114# Kotlin Tests
115run_test "Kotlin Func" "hello" "$TEST_DIR/test.kt" "fun hello ()"
116run_test "Kotlin Params" "add" "$TEST_DIR/test.kt" "fun add (a: Int, b: Int)"
117run_test "Kotlin Class" "MyClass" "$TEST_DIR/test.kt" "class MyClass"
118run_test "Kotlin Method" "myMethod" "$TEST_DIR/test.kt" "fun myMethod ()"
119run_test "Kotlin Extension" "extensionFunc" "$TEST_DIR/test.kt" "fun extensionFunc ()"
120
121# Odin Tests
122run_test "Odin Proc" "hello" "$TEST_DIR/test.odin" "proc hello"
123run_test "Odin Params" "add" "$TEST_DIR/test.odin" "proc add (a, b: int)"
124run_test "Odin Struct" "Point" "$TEST_DIR/test.odin" "struct Point"
125run_test "Odin Enum" "Color" "$TEST_DIR/test.odin" "enum Color"
126run_test "Odin Variable Proc" "complex_proc" "$TEST_DIR/test.odin" "proc complex_proc (name: string, age: int, flags: ..bool)"
127
128# Tcl Tests
129run_test "Tcl Proc" "hello" "$TEST_DIR/test.tcl" "proc hello"
130run_test "Tcl Params" "add" "$TEST_DIR/test.tcl" "proc add"
131run_test "Tcl Complex" "complex_proc" "$TEST_DIR/test.tcl" "proc complex_proc"
132
133# GLSL Tests
134run_test "GLSL Func" "main" "$TEST_DIR/test.glsl" "void main"
135run_test "GLSL Params" "add_vectors" "$TEST_DIR/test.glsl" "vec3 add_vectors (vec3 a, vec3 b)"
136run_test "GLSL Struct" "Point" "$TEST_DIR/test.glsl" "struct Point"
137
138# CUDA Tests
139run_test "CUDA Global" "vectorAdd" "$TEST_DIR/test.cu" "void vectorAdd"
140run_test "CUDA Device" "get_thread_id" "$TEST_DIR/test.cu" "int get_thread_id"
141run_test "CUDA Host" "init_vectors" "$TEST_DIR/test.cu" "void init_vectors"
142run_test "CUDA Struct" "Point" "$TEST_DIR/test.cuh" "struct Point"
143run_test "CUDA Class" "Dim3" "$TEST_DIR/test.cuh" "class Dim3"
144
145# Case Sensitivity Tests
146run_test "Default Case Insensitive" "foo" "tests/test.c" "void FooBar"
147run_test_with_flags "Case Sensitive -c" "-c" "foobar" "tests/test.c" "void foobar"
148
149# Depth Tests
150run_test_with_flags "Depth 0 (root)" "-d 0" "level" "tests/depth_test" "void level0"
151run_test_with_flags "Depth 1 (recursive)" "-d 1" "level" "tests/depth_test" "void level1"
152
153# Levenshtein Distance Tests
154run_test_with_flags "Levenshtein -l 1 match" "-l 1" "heelo" "$TEST_DIR/test.c" "void hello ()"
155run_test_with_flags "Levenshtein -l 2 match" "-l 2" "heloo" "$TEST_DIR/test.c" "void hello ()"
156
157echo "----------------"
158if [ $failed -eq 0 ]; then
159    echo "All tests passed!"
160    exit 0
161else
162    echo "$failed tests failed."
163    exit 1
164fi