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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// This file is part of PathsHelper library.
//
// Copyright 2018-2025 Arduino AG (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package paths
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestListConstructors(t *testing.T) {
list0 := NewPathList()
require.Len(t, list0, 0)
list1 := NewPathList("test")
require.Len(t, list1, 1)
require.Equal(t, "[test]", fmt.Sprintf("%s", list1))
list3 := NewPathList("a", "b", "c")
require.Len(t, list3, 3)
require.Equal(t, "[a b c]", fmt.Sprintf("%s", list3))
require.False(t, list3.Contains(New("d")))
require.True(t, list3.Contains(New("a")))
require.False(t, list3.Contains(New("d/../a")))
require.False(t, list3.ContainsEquivalentTo(New("d")))
require.True(t, list3.ContainsEquivalentTo(New("a")))
require.True(t, list3.ContainsEquivalentTo(New("d/../a")))
list4 := list3.Clone()
require.True(t, list3.Equals(list4))
require.Equal(t, "[a b c]", fmt.Sprintf("%s", list4))
list4.AddIfMissing(New("d"))
require.Equal(t, "[a b c d]", fmt.Sprintf("%s", list4))
list4.AddIfMissing(New("b"))
require.Equal(t, "[a b c d]", fmt.Sprintf("%s", list4))
list4.AddAllMissing(NewPathList("a", "e", "i", "o", "u"))
require.False(t, list3.Equals(list4))
require.False(t, list4.Equals(list3))
require.Equal(t, "[a b c d e i o u]", fmt.Sprintf("%s", list4))
}
func TestListSorting(t *testing.T) {
list := NewPathList(
"pointless",
"spare",
"carve",
"unwieldy",
"empty",
"bow",
"tub",
"grease",
"error",
"energetic",
"depend",
"property")
require.Equal(t, "[pointless spare carve unwieldy empty bow tub grease error energetic depend property]", fmt.Sprintf("%s", list))
list.Sort()
require.Equal(t, "[bow carve depend empty energetic error grease pointless property spare tub unwieldy]", fmt.Sprintf("%s", list))
}
func TestListFilters(t *testing.T) {
list := NewPathList(
"aaaa",
"bbbb",
"cccc",
"dddd",
"eeff",
"aaaa/bbbb",
"eeee/ffff",
"gggg/hhhh",
)
l1 := list.Clone()
l1.FilterPrefix("a")
require.Equal(t, "[aaaa]", fmt.Sprintf("%s", l1))
l2 := list.Clone()
l2.FilterPrefix("b")
require.Equal(t, "[bbbb aaaa/bbbb]", fmt.Sprintf("%s", l2))
l3 := list.Clone()
l3.FilterOutPrefix("b")
require.Equal(t, "[aaaa cccc dddd eeff eeee/ffff gggg/hhhh]", fmt.Sprintf("%s", l3))
l4 := list.Clone()
l4.FilterPrefix("a", "b")
require.Equal(t, "[aaaa bbbb aaaa/bbbb]", fmt.Sprintf("%s", l4))
l5 := list.Clone()
l5.FilterPrefix("test")
require.Equal(t, "[]", fmt.Sprintf("%s", l5))
l6 := list.Clone()
l6.FilterOutPrefix("b", "c", "h")
require.Equal(t, "[aaaa dddd eeff eeee/ffff]", fmt.Sprintf("%s", l6))
l7 := list.Clone()
l7.FilterSuffix("a")
require.Equal(t, "[aaaa]", fmt.Sprintf("%s", l7))
l8 := list.Clone()
l8.FilterSuffix("a", "h")
require.Equal(t, "[aaaa gggg/hhhh]", fmt.Sprintf("%s", l8))
l9 := list.Clone()
l9.FilterSuffix("test")
require.Equal(t, "[]", fmt.Sprintf("%s", l9))
l10 := list.Clone()
l10.FilterOutSuffix("a")
require.Equal(t, "[bbbb cccc dddd eeff aaaa/bbbb eeee/ffff gggg/hhhh]", fmt.Sprintf("%s", l10))
l11 := list.Clone()
l11.FilterOutSuffix("a", "h")
require.Equal(t, "[bbbb cccc dddd eeff aaaa/bbbb eeee/ffff]", fmt.Sprintf("%s", l11))
l12 := list.Clone()
l12.FilterOutSuffix("test")
require.Equal(t, "[aaaa bbbb cccc dddd eeff aaaa/bbbb eeee/ffff gggg/hhhh]", fmt.Sprintf("%s", l12))
l13 := list.Clone()
l13.FilterOutSuffix()
require.Equal(t, "[aaaa bbbb cccc dddd eeff aaaa/bbbb eeee/ffff gggg/hhhh]", fmt.Sprintf("%s", l13))
l14 := list.Clone()
l14.FilterSuffix()
require.Equal(t, "[]", fmt.Sprintf("%s", l14))
l15 := list.Clone()
l15.FilterOutPrefix()
require.Equal(t, "[aaaa bbbb cccc dddd eeff aaaa/bbbb eeee/ffff gggg/hhhh]", fmt.Sprintf("%s", l15))
l16 := list.Clone()
l16.FilterPrefix()
require.Equal(t, "[]", fmt.Sprintf("%s", l16))
l17 := list.Clone()
l17.Filter(func(p *Path) bool {
return p.Base() == "bbbb"
})
require.Equal(t, "[bbbb aaaa/bbbb]", fmt.Sprintf("%s", l17))
}
|