mirror of
https://github.com/issadarkthing/gomu.git
synced 2025-04-28 13:48:53 +08:00
allow Get{Int,String,Bool} to access symbol inside Module
This commit is contained in:
parent
7ae79c4dfb
commit
fbd59e7d30
@ -42,7 +42,7 @@ func (a *Anko) Get(symbol string) (interface{}, error) {
|
|||||||
|
|
||||||
// GetInt gets int value from symbol, returns golang default value if not found.
|
// GetInt gets int value from symbol, returns golang default value if not found.
|
||||||
func (a *Anko) GetInt(symbol string) int {
|
func (a *Anko) GetInt(symbol string) int {
|
||||||
v, err := a.env.Get(symbol)
|
v, err := a.Execute(symbol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ func (a *Anko) GetInt(symbol string) int {
|
|||||||
// GetString gets string value from symbol, returns golang default value if not
|
// GetString gets string value from symbol, returns golang default value if not
|
||||||
// found.
|
// found.
|
||||||
func (a *Anko) GetString(symbol string) string {
|
func (a *Anko) GetString(symbol string) string {
|
||||||
v, err := a.env.Get(symbol)
|
v, err := a.Execute(symbol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ func (a *Anko) GetString(symbol string) string {
|
|||||||
// GetBool gets bool value from symbol, returns golang default value if not
|
// GetBool gets bool value from symbol, returns golang default value if not
|
||||||
// found.
|
// found.
|
||||||
func (a *Anko) GetBool(symbol string) bool {
|
func (a *Anko) GetBool(symbol string) bool {
|
||||||
v, err := a.env.Get(symbol)
|
v, err := a.Execute(symbol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,17 @@ func TestGetInt(t *testing.T) {
|
|||||||
got := a.GetInt("x")
|
got := a.GetInt("x")
|
||||||
assert.Equal(t, expect, got)
|
assert.Equal(t, expect, got)
|
||||||
|
|
||||||
|
_, err = a.Execute(`module S { x = 10 }`)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got = a.GetInt("S.x")
|
||||||
|
assert.Equal(t, expect, got)
|
||||||
|
|
||||||
|
got = a.GetInt("S.y")
|
||||||
|
assert.Equal(t, 0, got)
|
||||||
|
|
||||||
a.Define("z", expect)
|
a.Define("z", expect)
|
||||||
val := a.GetInt("z")
|
val := a.GetInt("z")
|
||||||
|
|
||||||
@ -80,6 +91,17 @@ func TestGetString(t *testing.T) {
|
|||||||
got := a.GetString("x")
|
got := a.GetString("x")
|
||||||
assert.Equal(t, expect, got)
|
assert.Equal(t, expect, got)
|
||||||
|
|
||||||
|
_, err = a.Execute(`module S { x = "bruhh" }`)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got = a.GetString("S.x")
|
||||||
|
assert.Equal(t, expect, got)
|
||||||
|
|
||||||
|
got = a.GetString("S.y")
|
||||||
|
assert.Equal(t, "", got)
|
||||||
|
|
||||||
a.Define("z", expect)
|
a.Define("z", expect)
|
||||||
val := a.GetString("z")
|
val := a.GetString("z")
|
||||||
|
|
||||||
@ -91,6 +113,17 @@ func TestGetBool(t *testing.T) {
|
|||||||
a := NewAnko()
|
a := NewAnko()
|
||||||
a.Define("x", expect)
|
a.Define("x", expect)
|
||||||
|
|
||||||
|
_, err := a.Execute(`module S { x = true }`)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := a.GetBool("S.x")
|
||||||
|
assert.Equal(t, expect, got)
|
||||||
|
|
||||||
|
got = a.GetBool("S.y")
|
||||||
|
assert.Equal(t, false, got)
|
||||||
|
|
||||||
result := a.GetBool("x")
|
result := a.GetBool("x")
|
||||||
assert.Equal(t, expect, result)
|
assert.Equal(t, expect, result)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user