-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinybench_test.go
81 lines (74 loc) · 1.74 KB
/
tinybench_test.go
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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestTinyBench(t *testing.T) {
tmpDir := t.TempDir()
testCases := []struct {
name string
filePath string
fileContents string
errorMsg string
}{
{
name: "No arguments provided",
filePath: "",
errorMsg: "Please provide a path to a valid JS file.",
},
{
name: "No benchmark tags present",
fileContents: `
const foo = 'bar';
`,
filePath: filepath.Join(tmpDir, "no-benchmark-tags.js"),
errorMsg: `No benchmarks found. Please define at least one benchmark:`,
},
{
name: "Mismatched benchmark tags",
fileContents: `
// tinybench start
const foo = 'bar';
`,
filePath: filepath.Join(tmpDir, "mismatched-benchmark-tags.js"),
errorMsg: `No benchmarks found. Please define at least one benchmark:`,
},
{
name: "Valid benchmark",
fileContents: `
// tinybench start
const foo = 'bar';
// tinybench stop
`,
filePath: filepath.Join(tmpDir, "valid-benchmark.js"),
errorMsg: "",
},
}
for _, tc := range testCases {
if tc.fileContents != "" {
if err := os.WriteFile(tc.filePath, []byte(tc.fileContents), 0644); err != nil {
t.Fatal(err)
}
}
t.Run(tc.name, func(t *testing.T) {
os.Args = []string{"tinybench"}
if tc.filePath != "" {
os.Args = append(os.Args, tc.filePath)
}
err := run()
if tc.errorMsg == "" {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
} else {
if err == nil {
t.Errorf("Expected an error with message '%s', but got nil", tc.errorMsg)
} else if !strings.Contains(err.Error(), tc.errorMsg) {
t.Errorf("Expected error message '%s', but got '%s'", tc.errorMsg, err.Error())
}
}
})
}
}