70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestExecuteTypeScript(t *testing.T) {
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
err := executeTypeScript("test_data/test.ts", &stdout, &stderr)
|
|
|
|
assert.NoError(t, err, "Expected no error")
|
|
assert.Empty(t, stderr.String(), "Expected no error output")
|
|
|
|
output := stdout.String()
|
|
|
|
assert.Contains(t, output, "Hello, Alice!", "Should greet Alice")
|
|
assert.Contains(t, output, "You are 30 years old", "Should show age")
|
|
assert.Contains(t, output, "Email: alice@example.com", "Should show email")
|
|
assert.Contains(t, output, "Sum of 5 and 10 is: 15", "Should calculate sum correctly")
|
|
|
|
lines := strings.Split(strings.TrimSpace(output), "\n")
|
|
assert.GreaterOrEqual(t, len(lines), 3, "Should have at least 3 output lines")
|
|
}
|
|
|
|
func TestExecuteTypeScriptWithNonExistentFile(t *testing.T) {
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
err := executeTypeScript("non_existent_file.ts", &stdout, &stderr)
|
|
|
|
assert.Error(t, err, "Expected error for non-existent file")
|
|
assert.Contains(t, stderr.String(), "Error reading file", "Should show read error")
|
|
}
|
|
|
|
func TestExecuteTypeScriptWithSyntaxError(t *testing.T) {
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
tsContent := `
|
|
interface Person {
|
|
name: string;
|
|
age: number;
|
|
}
|
|
|
|
const user: Person = {
|
|
name: "Bob",
|
|
age: 25,
|
|
};
|
|
|
|
console.log(user.name)
|
|
console.log(user.age +);
|
|
`
|
|
|
|
err := os.WriteFile("test_data/invalid.ts", []byte(tsContent), 0644)
|
|
if err != nil {
|
|
t.Fatalf("Failed to write test file: %v", err)
|
|
}
|
|
defer func() {
|
|
_ = os.Remove("test_data/invalid.ts")
|
|
}()
|
|
|
|
err = executeTypeScript("test_data/invalid.ts", &stdout, &stderr)
|
|
|
|
assert.Error(t, err, "Expected error for invalid TypeScript")
|
|
}
|