OILS / spec / ysh-dict.test.sh View on Github | oilshell.org

91 lines, 57 significant
1#### "in" and "not in" on Dicts
2
3var d = {spam: 42, eggs: []}
4
5var b = 'spam' in d
6echo $b
7
8var b = 'zz' in d
9echo $b
10
11var b = 'zz' not in d
12echo $b
13
14var L = [1, 2, 3]
15var b = 3 in L # not allowed!
16
17echo should not get here
18
19## status: 3
20## STDOUT:
21true
22false
23true
24## END
25
26#### dict with 'bare word' keys
27var d0 = {}
28echo len=$[len(d0)]
29var d1 = {name: "hello"}
30echo len=$[len(d1)]
31var d2 = {name: "hello", other: 2}
32echo len=$[len(d2)]
33## STDOUT:
34len=0
35len=1
36len=2
37## END
38
39#### dict with expression keys
40var d1 = {['name']: "hello"}
41echo len=$[len(d1)]
42var v = d1['name']
43echo $v
44
45var key='k'
46var d2 = {["$key"]: "bar"}
47echo len=$[len(d2)]
48var v2 = d2['k']
49echo $v2
50
51## STDOUT:
52len=1
53hello
54len=1
55bar
56## END
57
58
59#### dict literal with implicit value
60var name = 'foo'
61var d1 = {name}
62echo len=$[len(d1)]
63var v1 = d1['name']
64echo $v1
65
66var d2 = {name, other: 'val'}
67echo len=$[len(d2)]
68var v2 = d2['name']
69echo $v2
70
71## STDOUT:
72len=1
73foo
74len=2
75foo
76## END
77
78#### Dict literal with string keys
79var d = {'sq': 123}
80var v = d['sq']
81echo $v
82
83var x = "q"
84var d2 = {"d$x": 456}
85var v2 = d2["dq"]
86echo $v2
87## STDOUT:
88123
89456
90## END
91