123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- package env
- import (
- "reflect"
- "testing"
- )
- func TestString(t *testing.T) {
- t.Parallel()
- env := NewEnv()
- env.Define("a", "a")
- output := env.String()
- expected := `No parent
- a = "a"
- `
- if output != expected {
- t.Errorf("received: %v - expected: %v", output, expected)
- }
- env = env.NewEnv()
- env.Define("b", "b")
- output = env.String()
- expected = `Has parent
- b = "b"
- `
- if output != expected {
- t.Errorf("received: %v - expected: %v", output, expected)
- }
- env = NewEnv()
- env.Define("c", "c")
- env.DefineType("string", "a")
- output = env.String()
- expected = `No parent
- c = "c"
- string = string
- `
- if output != expected {
- t.Errorf("received: %v - expected: %v", output, expected)
- }
- }
- func TestGetEnvFromPath(t *testing.T) {
- t.Parallel()
- env := NewEnv()
- a, err := env.NewModule("a")
- if err != nil {
- t.Fatal("NewModule error:", err)
- }
- var b *Env
- b, err = a.NewModule("b")
- if err != nil {
- t.Fatal("NewModule error:", err)
- }
- var c *Env
- c, err = b.NewModule("c")
- if err != nil {
- t.Fatal("NewModule error:", err)
- }
- err = c.Define("d", "d")
- if err != nil {
- t.Fatal("Define error:", err)
- }
- e, err := env.GetEnvFromPath(nil)
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- e, err = env.GetEnvFromPath([]string{})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- e, err = env.GetEnvFromPath([]string{"a", "c"})
- expected := "no namespace called: c"
- if err == nil || err.Error() != expected {
- t.Fatalf("GetEnvFromPath error - received: %v - expected: %v", err, expected)
- }
- if e != nil {
- t.Fatal("GetEnvFromPath e not nil")
- }
- // a.b.c
- e, err = env.GetEnvFromPath([]string{"a", "b", "c"})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- var value interface{}
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok := value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- e, err = a.GetEnvFromPath([]string{"a", "b", "c"})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok = value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- e, err = b.GetEnvFromPath([]string{"a", "b", "c"})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok = value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- e, err = c.GetEnvFromPath([]string{"a", "b", "c"})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok = value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- // b.c
- e, err = env.GetEnvFromPath([]string{"b", "c"})
- expected = "no namespace called: b"
- if err == nil || err.Error() != expected {
- t.Fatalf("GetEnvFromPath error - received: %v - expected: %v", err, expected)
- }
- if e != nil {
- t.Fatal("GetEnvFromPath e not nil")
- }
- e, err = a.GetEnvFromPath([]string{"b", "c"})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok = value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- e, err = b.GetEnvFromPath([]string{"b", "c"})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok = value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- e, err = c.GetEnvFromPath([]string{"b", "c"})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok = value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- // c
- e, err = env.GetEnvFromPath([]string{"c"})
- expected = "no namespace called: c"
- if err == nil || err.Error() != expected {
- t.Fatalf("GetEnvFromPath error - received: %v - expected: %v", err, expected)
- }
- if e != nil {
- t.Fatal("GetEnvFromPath e not nil")
- }
- e, err = b.GetEnvFromPath([]string{"c"})
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok = value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- e, err = c.GetEnvFromPath(nil)
- if err != nil {
- t.Fatal("GetEnvFromPath error:", err)
- }
- if e == nil {
- t.Fatal("GetEnvFromPath e nil")
- }
- value, err = e.Get("d")
- if err != nil {
- t.Fatal("Get error:", err)
- }
- v, ok = value.(string)
- if !ok {
- t.Fatal("value not string")
- }
- if v != "d" {
- t.Errorf("value - received: %v - expected: %v", v, "d")
- }
- }
- func TestCopy(t *testing.T) {
- t.Parallel()
- parent := NewEnv()
- parent.Define("a", "a")
- parent.DefineType("b", []bool{})
- child := parent.NewEnv()
- child.Define("c", "c")
- child.DefineType("d", []int64{})
- copy := child.Copy()
- if v, e := copy.Get("a"); e != nil || v != "a" {
- t.Errorf("copy missing value")
- }
- if v, e := copy.Type("b"); e != nil || v != reflect.TypeOf([]bool{}) {
- t.Errorf("copy missing type")
- }
- if v, e := copy.Get("c"); e != nil || v != "c" {
- t.Errorf("copy missing value")
- }
- if v, e := copy.Type("d"); e != nil || v != reflect.TypeOf([]int64{}) {
- t.Errorf("copy missing type")
- }
- // TODO: add more get type tests
- copy.Set("a", "i")
- if v, e := child.Get("a"); e != nil || v != "i" {
- t.Errorf("parent was not modified")
- }
- if v, e := copy.Get("a"); e != nil || v != "i" {
- t.Errorf("copy did not get parent value")
- }
- copy.Set("c", "j")
- if v, e := child.Get("c"); e != nil || v != "c" {
- t.Errorf("child was not modified")
- }
- if v, e := copy.Get("c"); e != nil || v != "j" {
- t.Errorf("copy child was not modified")
- }
- child.Set("a", "x")
- if v, e := child.Get("a"); e != nil || v != "x" {
- t.Errorf("parent was not modified")
- }
- if v, e := copy.Get("a"); e != nil || v != "x" {
- t.Errorf("copy did not get parent value")
- }
- child.Set("c", "z")
- if v, e := child.Get("c"); e != nil || v != "z" {
- t.Errorf("child was not modified")
- }
- if v, e := copy.Get("c"); e != nil || v != "j" {
- t.Errorf("copy child was modified")
- }
- parent.Set("a", "m")
- if v, e := child.Get("a"); e != nil || v != "m" {
- t.Errorf("parent was not modified")
- }
- if v, e := copy.Get("a"); e != nil || v != "m" {
- t.Errorf("copy did not get parent value")
- }
- parent.Define("x", "n")
- if v, e := child.Get("x"); e != nil || v != "n" {
- t.Errorf("child did not get parent value")
- }
- if v, e := copy.Get("x"); e != nil || v != "n" {
- t.Errorf("copy did not get parent value")
- }
- }
- func TestDeepCopy(t *testing.T) {
- t.Parallel()
- parent := NewEnv()
- parent.Define("a", "a")
- env := parent.NewEnv()
- copy := env.DeepCopy()
- // TODO: add more/better tests, like above
- if v, e := copy.Get("a"); e != nil || v != "a" {
- t.Errorf("copy doesn't retain original values")
- }
- parent.Set("a", "b")
- if v, e := env.Get("a"); e != nil || v != "b" {
- t.Errorf("son was not modified")
- }
- if v, e := copy.Get("a"); e != nil || v != "a" {
- t.Errorf("copy got the new value")
- }
- parent.Set("a", "c")
- if v, e := env.Get("a"); e != nil || v != "c" {
- t.Errorf("original was not modified")
- }
- if v, e := copy.Get("a"); e != nil || v != "a" {
- t.Errorf("copy was modified")
- }
- parent.Define("b", "b")
- if _, e := copy.Get("b"); e == nil {
- t.Errorf("copy parent was modified")
- }
- }
|