forked from drewwalton19216801/rush-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable_tests.rs
More file actions
170 lines (133 loc) · 5.15 KB
/
variable_tests.rs
File metadata and controls
170 lines (133 loc) · 5.15 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Tests for variable scoping and management
use crate::state::ShellState;
#[test]
fn test_local_variable_scoping() {
let mut state = ShellState::new();
// Set a global variable
state.set_var("global_var", "global_value".to_string());
assert_eq!(
state.get_var("global_var"),
Some("global_value".to_string())
);
// Push local scope
state.push_local_scope();
// Set a local variable with the same name
state.set_local_var("global_var", "local_value".to_string());
assert_eq!(state.get_var("global_var"), Some("local_value".to_string()));
// Set another local variable
state.set_local_var("local_var", "local_only".to_string());
assert_eq!(state.get_var("local_var"), Some("local_only".to_string()));
// Pop local scope
state.pop_local_scope();
// Should be back to global variable
assert_eq!(
state.get_var("global_var"),
Some("global_value".to_string())
);
assert_eq!(state.get_var("local_var"), None);
}
#[test]
fn test_nested_local_scopes() {
let mut state = ShellState::new();
// Set global variable
state.set_var("test_var", "global".to_string());
// Push first local scope
state.push_local_scope();
state.set_local_var("test_var", "level1".to_string());
assert_eq!(state.get_var("test_var"), Some("level1".to_string()));
// Push second local scope
state.push_local_scope();
state.set_local_var("test_var", "level2".to_string());
assert_eq!(state.get_var("test_var"), Some("level2".to_string()));
// Pop second scope
state.pop_local_scope();
assert_eq!(state.get_var("test_var"), Some("level1".to_string()));
// Pop first scope
state.pop_local_scope();
assert_eq!(state.get_var("test_var"), Some("global".to_string()));
}
#[test]
fn test_variable_set_in_local_scope() {
let mut state = ShellState::new();
// No local scope initially
state.set_var("test_var", "global".to_string());
assert_eq!(state.get_var("test_var"), Some("global".to_string()));
// Push local scope and set local variable
state.push_local_scope();
state.set_local_var("test_var", "local".to_string());
assert_eq!(state.get_var("test_var"), Some("local".to_string()));
// Pop scope
state.pop_local_scope();
assert_eq!(state.get_var("test_var"), Some("global".to_string()));
}
#[test]
fn test_positional_parameters() {
let mut state = ShellState::new();
state.set_positional_params(vec![
"arg1".to_string(),
"arg2".to_string(),
"arg3".to_string(),
]);
assert_eq!(state.get_var("1"), Some("arg1".to_string()));
assert_eq!(state.get_var("2"), Some("arg2".to_string()));
assert_eq!(state.get_var("3"), Some("arg3".to_string()));
assert_eq!(state.get_var("4"), None);
assert_eq!(state.get_var("#"), Some("3".to_string()));
assert_eq!(state.get_var("*"), Some("arg1 arg2 arg3".to_string()));
assert_eq!(state.get_var("@"), Some("arg1 arg2 arg3".to_string()));
}
#[test]
fn test_positional_parameters_empty() {
let mut state = ShellState::new();
state.set_positional_params(vec![]);
assert_eq!(state.get_var("1"), None);
assert_eq!(state.get_var("#"), Some("0".to_string()));
assert_eq!(state.get_var("*"), Some("".to_string()));
assert_eq!(state.get_var("@"), Some("".to_string()));
}
#[test]
fn test_shift_positional_params() {
let mut state = ShellState::new();
state.set_positional_params(vec![
"arg1".to_string(),
"arg2".to_string(),
"arg3".to_string(),
]);
assert_eq!(state.get_var("1"), Some("arg1".to_string()));
assert_eq!(state.get_var("2"), Some("arg2".to_string()));
assert_eq!(state.get_var("3"), Some("arg3".to_string()));
state.shift_positional_params(1);
assert_eq!(state.get_var("1"), Some("arg2".to_string()));
assert_eq!(state.get_var("2"), Some("arg3".to_string()));
assert_eq!(state.get_var("3"), None);
assert_eq!(state.get_var("#"), Some("2".to_string()));
state.shift_positional_params(2);
assert_eq!(state.get_var("1"), None);
assert_eq!(state.get_var("#"), Some("0".to_string()));
}
#[test]
fn test_push_positional_param() {
let mut state = ShellState::new();
state.set_positional_params(vec!["arg1".to_string()]);
assert_eq!(state.get_var("1"), Some("arg1".to_string()));
assert_eq!(state.get_var("#"), Some("1".to_string()));
state.push_positional_param("arg2".to_string());
assert_eq!(state.get_var("1"), Some("arg1".to_string()));
assert_eq!(state.get_var("2"), Some("arg2".to_string()));
assert_eq!(state.get_var("#"), Some("2".to_string()));
}
#[test]
fn test_last_background_pid() {
let mut state = ShellState::new();
// Initially, $! should be None (expands to empty string)
assert_eq!(state.get_var("!"), None);
// Set last background PID
state.last_background_pid = Some(1234);
assert_eq!(state.get_var("!"), Some("1234".to_string()));
// Update to a different PID
state.last_background_pid = Some(5678);
assert_eq!(state.get_var("!"), Some("5678".to_string()));
// Clear it
state.last_background_pid = None;
assert_eq!(state.get_var("!"), None);
}