Browse Source

Unit tests: PosOf InsertElement

Simple unit tests for ensuring basic functionality of these utility
functions. Exported from the "config" package now for testing.

Change-Id: I5f7133d16c14f99ff0b3b6392e4f210357678a88
Tyler Cipriani 2 months ago
parent
commit
a4b12761b8
2 changed files with 56 additions and 5 deletions
  1. 5
    5
      config/python.go
  2. 51
    0
      config/python_test.go

+ 5
- 5
config/python.go View File

@@ -96,7 +96,7 @@ func (pc PythonConfig) InstructionsForPhase(phase build.Phase) []build.Instructi
96 96
 				if args := pc.RequirementsArgs(); len(args) > 0 {
97 97
 					installCmd := append([]string{"-m", "pip", "install", "--target"}, PythonSitePackages)
98 98
 					if pc.UseSystemFlag {
99
-						installCmd = insertElement(installCmd, "--system", posOf(installCmd, "install") + 1)
99
+						installCmd = InsertElement(installCmd, "--system", PosOf(installCmd, "install") + 1)
100 100
 					}
101 101
 					ins = append(ins, build.RunAll{[]build.Run{
102 102
 						{pc.version(), append([]string{"-m", "pip", "wheel"}, args...)},
@@ -145,16 +145,16 @@ func (pc PythonConfig) version() string {
145 145
 	return pc.Version
146 146
 }
147 147
 
148
-// insert el into slice at pos
149
-func insertElement(slice []string, el string, pos int) []string {
148
+// InsertElement - insert el into slice at pos
149
+func InsertElement(slice []string, el string, pos int) []string {
150 150
 	slice = append(slice, "")
151 151
 	copy(slice[pos+1:], slice[pos:])
152 152
 	slice[pos] = el
153 153
 	return slice
154 154
 }
155 155
 
156
-// find position of an element in a slice
157
-func posOf(slice []string, el string) int {
156
+// PosOf - find position of an element in a slice
157
+func PosOf(slice []string, el string) int {
158 158
 	for p, v := range slice {
159 159
 		if v == el {
160 160
 			return p

+ 51
- 0
config/python_test.go View File

@@ -1,6 +1,7 @@
1 1
 package config_test
2 2
 
3 3
 import (
4
+	"reflect"
4 5
 	"testing"
5 6
 
6 7
 	"github.com/stretchr/testify/assert"
@@ -247,3 +248,53 @@ func TestPythonConfigRequirementsArgs(t *testing.T) {
247 248
 		cfg.RequirementsArgs(),
248 249
 	)
249 250
 }
251
+
252
+func TestSliceInsert(t *testing.T) {
253
+	t.Run("test inserting an element", func(t *testing.T) {
254
+		got := config.InsertElement([]string{"Hello", "World"}, "Beautiful", 1)
255
+		expected := []string{"Hello", "Beautiful", "World"}
256
+
257
+		if ! reflect.DeepEqual(got, expected) {
258
+			t.Errorf("Expected '%v'; got '%v'", expected, got)
259
+		}
260
+	})
261
+
262
+	t.Run("test inserting an element at the end", func(t *testing.T) {
263
+		orig := []string{"Foo", "Bar", "Baz"}
264
+		got := config.InsertElement(orig, "Beautiful", len(orig))
265
+		expected := []string{"Foo", "Bar", "Baz", "Beautiful"}
266
+
267
+		if ! reflect.DeepEqual(got, expected) {
268
+			t.Errorf("Expected '%v'; got '%v'", expected, got)
269
+		}
270
+	})
271
+
272
+	t.Run("test inserting an element at the beginning", func(t *testing.T) {
273
+		orig := []string{"Foo", "Bar", "Baz"}
274
+		got := config.InsertElement(orig, "Beautiful", 0)
275
+		expected := []string{"Beautiful", "Foo", "Bar", "Baz"}
276
+
277
+		if ! reflect.DeepEqual(got, expected) {
278
+			t.Errorf("Expected '%v'; got '%v'", expected, got)
279
+		}
280
+	})
281
+}
282
+
283
+func TestPosFinding(t *testing.T) {
284
+	t.Run("test finding string in slice", func(t *testing.T) {
285
+		got := config.PosOf([]string{"foo", "bar"}, "foo")
286
+		expected := 0
287
+		if got != expected {
288
+			t.Errorf("Expected '%v'; got '%v'", expected, got)
289
+		}
290
+	})
291
+
292
+	t.Run("test finding string NOT in slice", func(t *testing.T) {
293
+		got := config.PosOf([]string{"foo", "bar"}, "baz")
294
+		expected := -1
295
+		if got != expected {
296
+			t.Errorf("Expected '%v'; got '%v'", expected, got)
297
+		}
298
+	})
299
+
300
+}

Loading…
Cancel
Save