Skip to content

Commit 141bcd6

Browse files
committed
finished adding tests
1 parent c54d2a9 commit 141bcd6

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

arduino/types/sketch_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to modify or
12+
// otherwise use the software for commercial activities involving the Arduino
13+
// software without disclosing the source code of your own applications. To purchase
14+
// a commercial license, send an email to [email protected].
15+
16+
package types_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/arduino/types"
22+
paths "github.com/arduino/go-paths-helper"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestSketchFileSortByName(t *testing.T) {
27+
sketchSort := types.SketchFileSortByName{
28+
types.SketchFile{Name: paths.New("foo")},
29+
types.SketchFile{Name: paths.New("bar")},
30+
}
31+
32+
assert.Len(t, sketchSort, 2)
33+
assert.False(t, sketchSort.Less(0, 1))
34+
assert.True(t, sketchSort.Less(1, 0))
35+
36+
sketchSort.Swap(0, 1)
37+
assert.True(t, sketchSort.Less(0, 1))
38+
assert.False(t, sketchSort.Less(1, 0))
39+
}

arduino/types/sourcefile_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to modify or
12+
// otherwise use the software for commercial activities involving the Arduino
13+
// software without disclosing the source code of your own applications. To purchase
14+
// a commercial license, send an email to [email protected].
15+
16+
package types_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/arduino/libraries"
22+
"github.com/arduino/arduino-cli/arduino/types"
23+
paths "github.com/arduino/go-paths-helper"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestNewSourceFileFromSketch(t *testing.T) {
28+
c := getContext()
29+
c.SketchBuildPath = paths.New("/absolute")
30+
origin := &types.Sketch{
31+
MainFile: types.SketchFile{Name: paths.New("/absolute/path/to/foo")},
32+
}
33+
34+
path := paths.New("relative")
35+
sf, err := types.NewSourceFile(c, origin, path)
36+
assert.Nil(t, err)
37+
assert.Equal(t, "relative", sf.RelativePath.String())
38+
39+
path = paths.New("/absolute")
40+
c.SketchBuildPath = paths.New("relative")
41+
sf, err = types.NewSourceFile(c, origin, path)
42+
assert.NotNil(t, err)
43+
}
44+
45+
func TestNewSourceFileFromLibrary(t *testing.T) {
46+
c := getContext()
47+
origin := &libraries.Library{
48+
SourceDir: paths.New("/absolute/path/to/foo"),
49+
}
50+
51+
path := paths.New("relative")
52+
sf, err := types.NewSourceFile(c, origin, path)
53+
assert.Nil(t, err)
54+
assert.Equal(t, "relative", sf.RelativePath.String())
55+
56+
path = paths.New("/absolute")
57+
origin.SourceDir = paths.New("relative")
58+
sf, err = types.NewSourceFile(c, origin, path)
59+
assert.NotNil(t, err)
60+
}
61+
62+
func TestGettersFromSketch(t *testing.T) {
63+
c := getContext()
64+
c.SketchBuildPath = paths.New("/absolute")
65+
origin := &types.Sketch{
66+
MainFile: types.SketchFile{Name: paths.New("/absolute/path/to/foo")},
67+
}
68+
path := paths.New("relative")
69+
sf, _ := types.NewSourceFile(c, origin, path)
70+
assert.Equal(t, "/absolute/relative", sf.SourcePath(c).String())
71+
assert.Equal(t, "/absolute/relative.o", sf.ObjectPath(c).String())
72+
assert.Equal(t, "/absolute/relative.d", sf.DepfilePath(c).String())
73+
}
74+
75+
func TestGettersFromLibrary(t *testing.T) {
76+
c := getContext()
77+
c.LibrariesBuildPath = paths.New("/absolute/path/to/foo")
78+
origin := &libraries.Library{
79+
SourceDir: paths.New("/absolute/path/to/foo"),
80+
}
81+
path := paths.New("relative")
82+
sf, _ := types.NewSourceFile(c, origin, path)
83+
assert.Equal(t, "/absolute/path/to/foo/relative", sf.SourcePath(c).String())
84+
assert.Equal(t, "/absolute/path/to/foo/relative.o", sf.ObjectPath(c).String())
85+
assert.Equal(t, "/absolute/path/to/foo/relative.d", sf.DepfilePath(c).String())
86+
}
87+
88+
func TestUniqueSourceFileQueue(t *testing.T) {
89+
c := getContext()
90+
origin := &libraries.Library{
91+
SourceDir: paths.New("/absolute/path/to/foo"),
92+
}
93+
94+
path1 := paths.New("path1")
95+
sf1, _ := types.NewSourceFile(c, origin, path1)
96+
97+
path2 := paths.New("path2")
98+
sf2, _ := types.NewSourceFile(c, origin, path2)
99+
100+
q := types.UniqueSourceFileQueue{sf1, sf2}
101+
102+
assert.Len(t, q, 2)
103+
assert.False(t, q.Less(0, 1)) // Less always returns false
104+
assert.False(t, q.Less(1, 0))
105+
assert.Panics(t, func() { q.Swap(0, 1) })
106+
107+
// push an item that's already there and ensure it's discarded
108+
q.Push(sf1)
109+
assert.Len(t, q, 2)
110+
111+
// push a new item
112+
path3 := paths.New("path3")
113+
sf3, _ := types.NewSourceFile(c, origin, path3)
114+
q.Push(sf3)
115+
assert.Len(t, q, 3)
116+
117+
// Pop the items (from the head)
118+
assert.Equal(t, sf1, q.Pop())
119+
assert.Equal(t, sf2, q.Pop())
120+
assert.Equal(t, sf3, q.Pop())
121+
122+
// assert the queue is empty
123+
assert.Len(t, q, 0)
124+
assert.True(t, q.Empty())
125+
}

arduino/types/types_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to modify or
12+
// otherwise use the software for commercial activities involving the Arduino
13+
// software without disclosing the source code of your own applications. To purchase
14+
// a commercial license, send an email to [email protected].
15+
16+
package types_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/arduino/types"
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestPrototypeString(t *testing.T) {
26+
p := &types.Prototype{
27+
FunctionName: "MyFunc",
28+
File: "A File",
29+
Prototype: "PrototypeA",
30+
Modifiers: "Imma modifier",
31+
Line: 42,
32+
}
33+
34+
assert.Equal(t, "Imma modifier PrototypeA @ 42", p.String())
35+
}

0 commit comments

Comments
 (0)