Commit a978f227

a978f22747b6Merged

Chris Hasiński <[email protected]> · 6 months ago · 1 file · +1 added

Description

test(instance_eval): add bm_instance_eval covering the lifted block form

Seven sections exercise:

  • top-level recv.instance_eval with attr_accessor setters
  • multiple sequential instance_eval calls on the same receiver
  • methods called inside the block dispatching through self
  • instance_eval inside a top-level while
  • instance_eval inside a top-level if
  • interleaved calls on different objects of different classes
  • reassignment to another instance of the same class

Each section's output is checked against CRuby by make test.

Details

Commit
a978f22747b6
Parent
0caef283e0b1

test/bm_instance_eval.rbaddedView file

1+# Test instance_eval { block } block-form lifting.
2+# Each section's output is compared against CRuby by `make test`.
3+
4+class Config
5+ attr_accessor :port, :host, :debug
6+
7+ def initialize
8+ @port = 0
9+ @host = ""
10+ @debug = false
11+ end
12+end
13+
14+# ---- 1. Top-level basic form ----
15+cfg = Config.new
16+cfg.instance_eval do
17+ self.port = 8080
18+ self.host = "localhost"
19+ self.debug = true
20+end
21+puts cfg.port # 8080
22+puts cfg.host # localhost
23+puts cfg.debug # true
24+
25+# ---- 2. Two instance_eval calls in sequence (same object) ----
26+cfg.instance_eval do
27+ self.port = 9090
28+end
29+puts cfg.port # 9090
30+
31+# ---- 3. Methods called inside the block dispatch through self ----
32+class Routes
33+ attr_accessor :entries
34+
35+ def initialize
36+ @entries = "init".split(",") # StrArray hint
37+ @entries.pop # start empty
38+ end
39+
40+ def get(path)
41+ @entries.push("GET " + path)
42+ end
43+
44+ def post(path)
45+ @entries.push("POST " + path)
46+ end
47+end
48+
49+app = Routes.new
50+app.instance_eval do
51+ get("/")
52+ get("/about")
53+ post("/login")
54+end
55+i = 0
56+while i < app.entries.length
57+ puts app.entries[i]
58+ i = i + 1
59+end
60+
61+# ---- 4. instance_eval inside a top-level while loop ----
62+counter = Config.new
63+i = 0
64+while i < 3
65+ counter.instance_eval do
66+ self.port = self.port + 1
67+ end
68+ i = i + 1
69+end
70+puts counter.port # 3
71+
72+# ---- 5. instance_eval inside a top-level if branch ----
73+flag = Config.new
74+cond = 1
75+if cond > 0
76+ flag.instance_eval do
77+ self.debug = true
78+ end
79+end
80+puts flag.debug # true
81+
82+# ---- 6. Different objects of different classes interleaved ----
83+a = Config.new
84+b = Routes.new
85+a.instance_eval { self.port = 1 }
86+b.instance_eval { get("/x") }
87+a.instance_eval { self.port = 2 }
88+b.instance_eval { post("/y") }
89+puts a.port # 2
90+puts b.entries.length # 2
91+puts b.entries[0] # GET /x
92+puts b.entries[1] # POST /y
93+
94+# ---- 7. Reassignment to another instance of the same class ----
95+fresh = Config.new
96+fresh.instance_eval { self.port = 11 }
97+puts fresh.port # 11
98+fresh = Config.new
99+fresh.instance_eval { self.port = 22 }
100+puts fresh.port # 22