-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.zig
More file actions
83 lines (64 loc) · 2.27 KB
/
Copy pathvariable.zig
File metadata and controls
83 lines (64 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const std = @import("std");
const Allocator = std.mem.Allocator;
const Value = @import("value.zig").Value;
/// ============================================================================
/// Variable - Named mutable value container
/// ============================================================================
pub const Variable = struct {
name: []const u8,
value: Value,
pub fn init(name: []const u8, value: Value) Variable {
return Variable{
.name = name,
.value = value,
};
}
pub fn deinit(self: *Variable, allocator: Allocator) void {
self.value.deinit(allocator);
// Don't free self.name - it's owned by the HashMap key
}
pub fn getName(self: *const Variable) []const u8 {
return self.name;
}
pub fn setValue(self: *Variable, value: Value) void {
self.value = value;
}
pub fn getValue(self: *const Variable) Value {
return self.value;
}
pub fn dup(self: *const Variable) Variable {
return Variable{
.name = self.name,
.value = self.value,
};
}
};
// ============================================================================
// Tests
// ============================================================================
test "Variable: basic operations" {
const allocator = std.testing.allocator;
_ = allocator;
const value = Value.initInt(42);
var variable = Variable.init("my_var", value);
try std.testing.expectEqualStrings("my_var", variable.getName());
try std.testing.expectEqual(value, variable.getValue());
}
test "Variable: set value" {
const allocator = std.testing.allocator;
_ = allocator;
const val1 = Value.initInt(42);
const val2 = Value.initInt(99);
var variable = Variable.init("my_var", val1);
variable.setValue(val2);
try std.testing.expectEqual(val2, variable.getValue());
}
test "Variable: duplicate" {
const allocator = std.testing.allocator;
_ = allocator;
const value = Value.initInt(42);
const variable = Variable.init("my_var", value);
const dup_var = variable.dup();
try std.testing.expectEqualStrings(variable.getName(), dup_var.getName());
try std.testing.expectEqual(variable.getValue(), dup_var.getValue());
}