MCPcopy Index your code
hub / github.com/expr-lang/expr / TestEnv_keyword

Function TestEnv_keyword

expr_test.go:2199–2293  ·  view source on GitHub ↗

Test the use of env keyword. Forms env[] and env[”] are valid. The enclosed identifier must be in the expression env.

(t *testing.T)

Source from the content-addressed store, hash-verified

2197// Test the use of env keyword. Forms env[] and env[”] are valid.
2198// The enclosed identifier must be in the expression env.
2199func TestEnv_keyword(t *testing.T) {
2200 env := map[string]any{
2201 "space test": "ok",
2202 "space_test": "not ok", // Seems to be some underscore substituting happening, check that.
2203 "Section 1-2a": "ok",
2204 `c:\ndrive\2015 Information Table`: "ok",
2205 "%*worst function name ever!!": func() string {
2206 return "ok"
2207 }(),
2208 "1": "o",
2209 "2": "k",
2210 "num": 10,
2211 "mylist": []int{1, 2, 3, 4, 5},
2212 "MIN": func(a, b int) int {
2213 if a < b {
2214 return a
2215 } else {
2216 return b
2217 }
2218 },
2219 "red": "n",
2220 "irect": "um",
2221 "String Map": map[string]string{
2222 "one": "two",
2223 "three": "four",
2224 },
2225 "OtherMap": map[string]string{
2226 "a": "b",
2227 "c": "d",
2228 },
2229 }
2230
2231 // No error cases
2232 var tests = []struct {
2233 code string
2234 want any
2235 }{
2236 {"$env['space test']", "ok"},
2237 {"$env['Section 1-2a']", "ok"},
2238 {`$env["c:\\ndrive\\2015 Information Table"]`, "ok"},
2239 {"$env['%*worst function name ever!!']", "ok"},
2240 {"$env['String Map'].one", "two"},
2241 {"$env['1'] + $env['2']", "ok"},
2242 {"1 + $env['num'] + $env['num']", 21},
2243 {"MIN($env['num'],0)", 0},
2244 {"$env['nu' + 'm']", 10},
2245 {"$env[red + irect]", 10},
2246 {"$env['String Map']?.five", ""},
2247 {"$env.red", "n"},
2248 {"$env?.unknown", nil},
2249 {"$env.mylist[1]", 2},
2250 {"$env?.OtherMap?.a", "b"},
2251 {"$env?.OtherMap?.d", ""},
2252 {"'num' in $env", true},
2253 {"get($env, 'num')", 10},
2254 }
2255
2256 for _, tt := range tests {

Callers

nothing calls this directly

Calls 8

CompileFunction · 0.92
EnvStruct · 0.92
NoErrorFunction · 0.92
RunFunction · 0.92
EqualFunction · 0.92
EvalFunction · 0.92
ErrorFunction · 0.92
RunMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…