|
| 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 | +} |
0 commit comments