#!/usr/bin/env pwsh
# Config Manager Complete Test Script
# Contains all test cases and example programs
Write-Host "========================================" -ForegroundColor Green
Write-Host "Starting Config Manager Test Suite" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
$ErrorActionPreference = "Continue"
$testResults = @()
# Function: Run test and record results
function Run-Test {
param(
[string]$TestName,
[string]$Command,
[string]$Description
)
Write-Host "`n----------------------------------------" -ForegroundColor Yellow
Write-Host "Running: $Description" -ForegroundColor Yellow
Write-Host "Command: $Command" -ForegroundColor Gray
Write-Host "----------------------------------------" -ForegroundColor Yellow
$startTime = Get-Date
try {
Invoke-Expression $Command
$exitCode = $LASTEXITCODE
$endTime = Get-Date
$duration = ($endTime - $startTime).TotalSeconds
if ($exitCode -eq 0) {
Write-Host "PASS $TestName - Duration: $([math]::Round($duration, 2))s" -ForegroundColor Green
$script:testResults += [PSCustomObject]@{
Name = $TestName
Status = "PASS"
Duration = $duration
Description = $Description
}
} else {
Write-Host "FAIL $TestName - Exit code: $exitCode" -ForegroundColor Red
$script:testResults += [PSCustomObject]@{
Name = $TestName
Status = "FAIL"
Duration = $duration
Description = $Description
}
}
} catch {
$endTime = Get-Date
$duration = ($endTime - $startTime).TotalSeconds
Write-Host "ERROR $TestName - Exception: $($_.Exception.Message)" -ForegroundColor Red
$script:testResults += [PSCustomObject]@{
Name = $TestName
Status = "ERROR"
Duration = $duration
Description = $Description
}
}
}
# 1. Run main program (main.rs)
Run-Test "Main Program" "cargo run --bin config_rw_test" "Run main program in src/main.rs, test basic config manager functionality"
# 2. Run individual test functions in demo.rs
Run-Test "demo_config_manager" "cargo test demo_config_manager" "Test basic config manager functionality demo"
Run-Test "test_config_demo" "cargo test test_config_demo" "Test config read/write and modification"
Run-Test "test_cross_module_access" "cargo test test_cross_module_access" "Test cross-module config access"
Run-Test "test_complex_config_operations" "cargo test test_complex_config_operations" "Test complex config operations (arrays, objects, etc.)"
# 3. Run test modules in main.rs
Run-Test "Priority Test" "cargo test test_demo_priority" "Test config priority functionality"
Run-Test "Auto Save Test" "cargo test test_demo_auto_save" "Test auto save functionality"
# 4. Run library unit tests
Run-Test "Library Unit Tests" "cargo test --lib" "Run all unit tests in src/lib.rs"
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "Test Results Summary" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
$passedTests = $testResults | Where-Object { $_.Status -eq "PASS" }
$failedTests = $testResults | Where-Object { $_.Status -ne "PASS" }
Write-Host "`nTest Statistics:" -ForegroundColor Cyan
Write-Host "Total Tests: $($testResults.Count)" -ForegroundColor White
Write-Host "Passed: $($passedTests.Count)" -ForegroundColor Green
Write-Host "Failed: $($failedTests.Count)" -ForegroundColor Red
if ($failedTests.Count -gt 0) {
Write-Host "`nFailed Tests:" -ForegroundColor Red
$failedTests | ForEach-Object {
Write-Host "FAIL $($_.Name) - $($_.Status)" -ForegroundColor Red
}
}
Write-Host "`nDetailed Results:" -ForegroundColor Cyan
$testResults | Format-Table -Property Name, Status, @{Name="Duration(s)"; Expression={[math]::Round($_.Duration, 2)}}, Description -AutoSize
$totalDuration = ($testResults | Measure-Object -Property Duration -Sum).Sum
Write-Host "Total Duration: $([math]::Round($totalDuration, 2))s" -ForegroundColor Cyan
if ($failedTests.Count -eq 0) {
Write-Host "`nAll tests passed!" -ForegroundColor Green
exit 0
} else {
Write-Host "`nWarning: $($failedTests.Count) tests failed" -ForegroundColor Red
exit 1
}