-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
107 lines (91 loc) · 2.3 KB
/
main_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
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
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"testing"
"time"
"unsafe"
"golang.org/x/sys/windows"
)
type MockProc struct {
CallFunc func(...uintptr) (uintptr, uintptr, error)
}
func (m *MockProc) Call(a ...uintptr) (uintptr, uintptr, error) {
return m.CallFunc(a...)
}
// モックを使用して getCursorPos と setCursorPos の動作を検証
type MockDLL struct {
Procs map[string]*MockProc
}
func (dll *MockDLL) NewProc(name string) *MockProc {
if proc, ok := dll.Procs[name]; ok {
return proc
}
return &MockProc{}
}
func TestMainLogic(t *testing.T) {
// モックDLLのセットアップ
mockDLL := &MockDLL{
Procs: map[string]*MockProc{
"GetCursorPos": {
CallFunc: func(a ...uintptr) (uintptr, uintptr, error) {
pt := (*POINT)(unsafe.Pointer(a[0]))
pt.X, pt.Y = 100, 200
return 0, 0, nil
},
},
"SetCursorPos": {
CallFunc: func(a ...uintptr) (uintptr, uintptr, error) {
return 0, 0, nil
},
},
},
}
// DLLをモックに置き換え
user32 = &windows.LazyDLL{}
procGetCursorPos = mockDLL.NewProc("GetCursorPos")
procSetCursorPos = mockDLL.NewProc("SetCursorPos")
// コマンドライン引数を設定
os.Args = []string{"cmd", "-interval=1", "-maxmove=1"}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// シグナルのモック
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT)
defer signal.Stop(sigChan)
// タイマーを短縮してテスト
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
go func() {
time.Sleep(2 * time.Second)
sigChan <- syscall.SIGINT
}()
// メインロジックのテスト
mainLoop := func() {
for {
select {
case <-ticker.C:
x, y := getCursorPos()
if x != 100 || y != 200 {
t.Fatalf("unexpected cursor position: (%d, %d)", x, y)
}
setCursorPos(x+1, y)
setCursorPos(x, y)
case <-sigChan:
return
}
}
}
mainLoop()
}
func TestGetCursorPos(t *testing.T) {
testX, testY := getCursorPos()
if testX != 100 || testY != 200 {
t.Errorf("Expected cursor position (100, 200), got (%d, %d)", testX, testY)
}
}
func TestSetCursorPos(t *testing.T) {
setCursorPos(300, 400)
// マウスカーソルの設定結果を検証するためのモックログを確認する手段がある場合に追加する
}