-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathnew_test.go
82 lines (73 loc) · 1.86 KB
/
new_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
package sketch
import (
"context"
"testing"
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/stretchr/testify/require"
)
func Test_SketchNameWrongPattern(t *testing.T) {
invalidNames := []string{
"&",
"",
".hello",
"_hello",
"-hello",
"hello*",
"||||||||||||||",
",`hack[}attempt{];",
}
for _, name := range invalidNames {
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
SketchName: name,
SketchDir: t.TempDir(),
})
require.NotNil(t, err)
require.Error(t, err, `Can't create sketch: invalid sketch name "%s". Required pattern %s`,
name,
sketchNameValidationRegex)
}
}
func Test_SketchNameEmpty(t *testing.T) {
emptyName := ""
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
SketchName: emptyName,
SketchDir: t.TempDir(),
})
require.NotNil(t, err)
require.Error(t, err, `Can't create sketch: sketch name cannot be empty`)
}
func Test_SketchNameTooLong(t *testing.T) {
tooLongName := make([]byte, sketchNameMaxLength+1)
for i := range tooLongName {
tooLongName[i] = 'a'
}
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
SketchName: string(tooLongName),
SketchDir: t.TempDir(),
})
require.NotNil(t, err)
require.Error(t, err, `Can't create sketch: sketch name too long (%d characters). Maximum allowed length is %d`,
len(tooLongName),
sketchNameMaxLength)
}
func Test_SketchNameOk(t *testing.T) {
lengthLimitName := make([]byte, sketchNameMaxLength)
for i := range lengthLimitName {
lengthLimitName[i] = 'a'
}
validNames := []string{
"h",
"h.ello",
"h..ello-world",
"h..ello-world.",
"hello_world__",
string(lengthLimitName),
}
for _, name := range validNames {
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
SketchName: name,
SketchDir: t.TempDir(),
})
require.Nil(t, err)
}
}