Debian Codegraph @

expand/environ_test.go

raw
1 // Copyright (c) 2018, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package expand
5
6 import (
7 "testing"
8
9 "github.com/go-quicktest/qt"
10 )
11
12 func TestListEnviron(t *testing.T) {
13 tests := []struct {
14 name string
15 insensitive bool
16 pairs []string
17 want []string
18 }{
19 {
20 name: "Empty",
21 pairs: nil,
22 want: nil,
23 },
24 {
25 name: "Simple",
26 pairs: []string{"A=b", "c="},
27 want: []string{"A=b", "c="},
28 },
29 {
30 name: "MissingEqual",
31 pairs: []string{"A=b", "invalid", "c="},
32 want: []string{"A=b", "c="},
33 },
34 {
35 name: "DuplicateNames",
36 pairs: []string{"A=x", "A=b", "c=", "c=y"},
37 want: []string{"A=b", "c=y"},
38 },
39 {
40 name: "NoName",
41 pairs: []string{"=b", "=c"},
42 want: []string{},
43 },
44 {
45 name: "EmptyElements",
46 pairs: []string{"A=b", "", "", "c="},
47 want: []string{"A=b", "c="},
48 },
49 {
50 name: "MixedCaseNoInsensitive",
51 pairs: []string{"A=b1", "Path=foo", "a=b2"},
52 want: []string{"A=b1", "Path=foo", "a=b2"},
53 },
54 {
55 name: "MixedCaseInsensitive",
56 insensitive: true,
57 pairs: []string{"A=b1", "Path=foo", "a=b2"},
58 want: []string{"a=b2", "Path=foo"},
59 },
60 }
61 for _, tc := range tests {
62 t.Run(tc.name, func(t *testing.T) {
63 gotEnv := listEnviron_(tc.insensitive, tc.pairs...)
64 got := gotEnv.(listEnviron).pairs
65 qt.Assert(t, qt.DeepEquals(got, tc.want))
66 })
67 }
68 }
69
70 func TestGetWithSameSubPrefix(t *testing.T) {
71 gotEnv := ListEnviron("GREETING=text1", "GREETING2=text2")
72 got := gotEnv.Get("GREETING2").String()
73 if got != "text2" {
74 t.Fatalf("ListEnviron.Get(GREETING2) wanted text2, got %q", got)
75 }
76 got = gotEnv.Get("GREETING").String()
77 if got != "text1" {
78 t.Fatalf("ListEnviron.Get(GREETING) wanted text1, got %q", got)
79 }
80 }