Skip to content

Commit fcbb91c

Browse files
authored
Merge pull request #498 from savi-lang/fix/trace-data-mutable-bool
Fix tracing of `Bool` in `TraceData.Mutable`.
2 parents 32e8caa + 1e4f91a commit fcbb91c

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

core/TraceData.savi

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@
328328
:: Set the new value from the yield block (if the mutator decides to call it).
329329
:fun ref replace_bool(value Bool) None
330330
:yields Bool for None // TODO: add a "without interruption" enforcement to the yield signature to ensure that the yield block isn't allowed to jump away.
331-
None
331+
@set_bool -> (v | yield v)
332332

333333
:: Allow the mutator the option to replace a `U64` at the current location,
334334
:: passing it the current value as an argument (to use as a reference).

spec/core/TraceData.Mutable.Spec.savi

+130
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,81 @@
4141
age: 0
4242
>>>
4343

44+
:it "can inject every type of primitive value"
45+
example = _ExampleMutableEachPrimitive.new
46+
47+
mut_bool = _TraceMutatorInject(Bool).new("bool", True)
48+
example.trace_data_mutable(mut_bool)
49+
assert: mut_bool.errors == []
50+
51+
mut_u64 = _TraceMutatorInject(U64).new("u64", 0x1234567890ABCDEF)
52+
example.trace_data_mutable(mut_u64)
53+
assert: mut_u64.errors == []
54+
55+
mut_u32 = _TraceMutatorInject(U32).new("u32", 0x12345678)
56+
example.trace_data_mutable(mut_u32)
57+
assert: mut_u32.errors == []
58+
59+
mut_u16 = _TraceMutatorInject(U16).new("u16", 0x1234)
60+
example.trace_data_mutable(mut_u16)
61+
assert: mut_u16.errors == []
62+
63+
mut_u8 = _TraceMutatorInject(U8).new("u8", 0x12)
64+
example.trace_data_mutable(mut_u8)
65+
assert: mut_u8.errors == []
66+
67+
mut_i64 = _TraceMutatorInject(I64).new("i64", -99)
68+
example.trace_data_mutable(mut_i64)
69+
assert: mut_i64.errors == []
70+
71+
mut_i32 = _TraceMutatorInject(I32).new("i32", -98)
72+
example.trace_data_mutable(mut_i32)
73+
assert: mut_i32.errors == []
74+
75+
mut_i16 = _TraceMutatorInject(I16).new("i16", -97)
76+
example.trace_data_mutable(mut_i16)
77+
assert: mut_i16.errors == []
78+
79+
mut_i8 = _TraceMutatorInject(I8).new("i8", -96)
80+
example.trace_data_mutable(mut_i8)
81+
assert: mut_i8.errors == []
82+
83+
mut_f64 = _TraceMutatorInject(F64).new("f64", 3.14159)
84+
example.trace_data_mutable(mut_f64)
85+
assert: mut_f64.errors == []
86+
87+
mut_f32 = _TraceMutatorInject(F32).new("f32", 1.5)
88+
example.trace_data_mutable(mut_f32)
89+
assert: mut_f32.errors == []
90+
91+
mut_bytes = _TraceMutatorInject(Bytes).new("bytes", b"Hello")
92+
example.trace_data_mutable(mut_bytes)
93+
assert: mut_bytes.errors == []
94+
95+
mut_string = _TraceMutatorInject(String).new("string", "World")
96+
example.trace_data_mutable(mut_string)
97+
assert: mut_string.errors == []
98+
99+
// The changes are reflected in the printed trace.
100+
example_printed = String.new
101+
example.trace_data(Inspect.TraceData.Printer.Deterministic.new(example_printed))
102+
assert: example_printed == <<<
103+
#1:
104+
bool: True
105+
u64: 1311768467294899695
106+
u32: 305419896
107+
u16: 4660
108+
u8: 18
109+
i64: -99
110+
i32: -98
111+
i16: -97
112+
i8: -96
113+
f64: 3.14159
114+
f32: 1.5
115+
bytes: b"Hello"
116+
string: "World"
117+
>>>
118+
44119
:: A test class demonstrating a simple data structure that can be mutated
45120
:: by any `TraceData.Mutator` using the `TraceData.Mutable` trait.
46121
:class _ExampleMutablePerson
@@ -71,6 +146,61 @@
71146
)
72147
)
73148

149+
:: A test class demonstrating a every primitive that can be mutably replaced.
150+
:class _ExampleMutableEachPrimitive
151+
:var bool Bool: False
152+
:var u64 U64: 0
153+
:var u32 U32: 0
154+
:var u16 U16: 0
155+
:var u8 U8: 0
156+
:var i64 I64: 0
157+
:var i32 I32: 0
158+
:var i16 I16: 0
159+
:var i8 I8: 0
160+
:var f64 F64: 0.0
161+
:var f32 F32: 0.0
162+
:var bytes Bytes: b""
163+
:var string String: ""
164+
165+
:is TraceData
166+
:fun trace_data(trace TraceData.Observer)
167+
trace.object(identity_digest_of @) -> (
168+
trace.property("bool", @bool)
169+
trace.property("u64", @u64)
170+
trace.property("u32", @u32)
171+
trace.property("u16", @u16)
172+
trace.property("u8", @u8)
173+
trace.property("i64", @i64)
174+
trace.property("i32", @i32)
175+
trace.property("i16", @i16)
176+
trace.property("i8", @i8)
177+
trace.property("f64", @f64)
178+
trace.property("f32", @f32)
179+
trace.property("bytes", @bytes)
180+
trace.property("string", @string)
181+
)
182+
183+
:is TraceData.Mutable
184+
:fun ref trace_data_mutable(trace TraceData.Mutator)
185+
trace.object(identity_digest_of @) -> (key |
186+
case key == (
187+
| "bool" | trace.replace_bool(@bool) -> (v | @bool = v)
188+
| "u64" | trace.replace_u64(@u64) -> (v | @u64 = v)
189+
| "u32" | trace.replace_u32(@u32) -> (v | @u32 = v)
190+
| "u16" | trace.replace_u16(@u16) -> (v | @u16 = v)
191+
| "u8" | trace.replace_u8(@u8) -> (v | @u8 = v)
192+
| "i64" | trace.replace_i64(@i64) -> (v | @i64 = v)
193+
| "i32" | trace.replace_i32(@i32) -> (v | @i32 = v)
194+
| "i16" | trace.replace_i16(@i16) -> (v | @i16 = v)
195+
| "i8" | trace.replace_i8(@i8) -> (v | @i8 = v)
196+
| "f64" | trace.replace_f64(@f64) -> (v | @f64 = v)
197+
| "f32" | trace.replace_f32(@f32) -> (v | @f32 = v)
198+
| "bytes" | trace.replace_bytes(@bytes) -> (v | @bytes = v)
199+
| "string" | trace.replace_string(@string) -> (v | @string = v)
200+
| trace.object_key_is_invalid
201+
)
202+
)
203+
74204
:: A test class used to demonstrate how a `TraceData.Mutator` implementation
75205
:: can be used to inject a single value into an arbitrary object tree path,
76206
:: as long as all the objects along that path implement `TraceData.Mutable`.

0 commit comments

Comments
 (0)