@@ -1811,6 +1811,103 @@ func TestEval_nil_in_maps(t *testing.T) {
1811
1811
})
1812
1812
}
1813
1813
1814
+ // Test the use of env keyword. Forms env[] and env[”] are valid.
1815
+ // The enclosed identifier must be in the expression env.
1816
+ func TestEnv_keyword (t * testing.T ) {
1817
+ env := map [string ]interface {}{
1818
+ "space test" : "ok" ,
1819
+ "space_test" : "not ok" , // Seems to be some underscore substituting happening, check that.
1820
+ "Section 1-2a" : "ok" ,
1821
+ `c:\ndrive\2015 Information Table` : "ok" ,
1822
+ "%*worst function name ever!!" : func () string {
1823
+ return "ok"
1824
+ }(),
1825
+ "1" : "o" ,
1826
+ "2" : "k" ,
1827
+ "num" : 10 ,
1828
+ "mylist" : []int {1 , 2 , 3 , 4 , 5 },
1829
+ "MIN" : func (a , b int ) int {
1830
+ if a < b {
1831
+ return a
1832
+ } else {
1833
+ return b
1834
+ }
1835
+ },
1836
+ "red" : "n" ,
1837
+ "irect" : "um" ,
1838
+ "String Map" : map [string ]string {
1839
+ "one" : "two" ,
1840
+ "three" : "four" ,
1841
+ },
1842
+ "OtherMap" : map [string ]string {
1843
+ "a" : "b" ,
1844
+ "c" : "d" ,
1845
+ },
1846
+ }
1847
+
1848
+ // No error cases
1849
+ var tests = []struct {
1850
+ code string
1851
+ want interface {}
1852
+ }{
1853
+ {"env['space test']" , "ok" },
1854
+ {"env['Section 1-2a']" , "ok" },
1855
+ {`env["c:\\ndrive\\2015 Information Table"]` , "ok" },
1856
+ {"env['%*worst function name ever!!']" , "ok" },
1857
+ {"env['String Map'].one" , "two" },
1858
+ {"env['1'] + env['2']" , "ok" },
1859
+ {"1 + env['num'] + env['num']" , 21 },
1860
+ {"MIN(env['num'],0)" , 0 },
1861
+ {"env['nu' + 'm']" , 10 },
1862
+ {"env[red + irect]" , 10 },
1863
+ {"env['String Map']?.five" , "" },
1864
+ {"env.red" , "n" },
1865
+ {"env?.blue" , nil },
1866
+ {"env.mylist[1]" , 2 },
1867
+ {"env?.OtherMap?.a" , "b" },
1868
+ {"env?.OtherMap?.d" , "" },
1869
+ }
1870
+
1871
+ for _ , tt := range tests {
1872
+ t .Run (tt .code , func (t * testing.T ) {
1873
+
1874
+ program , err := expr .Compile (tt .code , expr .Env (env ))
1875
+ require .NoError (t , err , "compile error" )
1876
+
1877
+ got , err := expr .Run (program , env )
1878
+ require .NoError (t , err , "execution error" )
1879
+
1880
+ assert .Equal (t , tt .want , got , tt .code )
1881
+ })
1882
+ }
1883
+
1884
+ for _ , tt := range tests {
1885
+ t .Run (tt .code , func (t * testing.T ) {
1886
+ got , err := expr .Eval (tt .code , env )
1887
+ require .NoError (t , err , "eval error: " + tt .code )
1888
+
1889
+ assert .Equal (t , tt .want , got , "eval: " + tt .code )
1890
+ })
1891
+ }
1892
+
1893
+ // error cases
1894
+ tests = []struct {
1895
+ code string
1896
+ want interface {}
1897
+ }{
1898
+ {"env()" , "bad" },
1899
+ }
1900
+
1901
+ for _ , tt := range tests {
1902
+ t .Run (tt .code , func (t * testing.T ) {
1903
+ _ , err := expr .Eval (tt .code , expr .Env (env ))
1904
+ require .Error (t , err , "compile error" )
1905
+
1906
+ })
1907
+ }
1908
+
1909
+ }
1910
+
1814
1911
type Bar interface {
1815
1912
Bar () int
1816
1913
}
0 commit comments