-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptional_param_test.go
92 lines (81 loc) · 1.69 KB
/
optional_param_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
package main
import (
"errors"
"fmt"
"testing"
)
type Config struct {
Name string
Value int
}
type ConfigFunc func(*Config) error
func WithName(name string) ConfigFunc {
return func(cfg *Config) error {
if cfg == nil {
return errors.New("nil pointer")
}
cfg.Name = name
return nil
}
}
func WithValue(value int) ConfigFunc {
return func(cfg *Config) error {
if cfg == nil {
return errors.New("nil pointer")
}
if value < 0 {
return errors.New("invalid value")
}
cfg.Value = value
return nil
}
}
func ApplyConfig(cfg *Config, funcs ...ConfigFunc) error {
for _, f := range funcs {
err := f(cfg)
if err != nil {
return err
}
}
return nil
}
func TestPointerStruct(t *testing.T) {
// Contoh 1: Nil Pointer
var cfg *Config
err := ApplyConfig(cfg, WithName("John"))
if err != nil {
fmt.Println("Error:", err)
}
// Contoh 2: Penggunaan Nilai Default
cfg2 := &Config{Name: "Alice", Value: -1}
err = ApplyConfig(cfg2)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Config:", cfg2)
// Contoh 3: Aliasing dan Kesalahan Mutasi
cfg3 := &Config{Name: "Bob", Value: 10}
funcs := []ConfigFunc{
WithName("Charlie"),
WithValue(20),
}
err = ApplyConfig(cfg3, funcs...)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Config:", cfg3)
// Contoh 4: Ketidaksesuaian Tipe
cfg4 := &Config{Name: "Dave", Value: 30}
err = ApplyConfig(cfg4, WithName("42"))
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Config:", cfg4)
// Contoh 5: Kesalahan Logika
cfg5 := &Config{Name: "Eve", Value: 40}
err = ApplyConfig(cfg5, WithName("Eve"), WithValue(-1))
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Config:", cfg5)
}