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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#[cfg(test)]
use super::minifier;


#[cfg(test)]
fn test_minify(test_code: &str,expected: &str,level: usize) {
	let mut minifier = minifier::Minifier::new();
	minifier.set_level(level);
	let actual = minifier.minify(test_code).expect("minify failed");
	assert_eq!(actual,String::from(expected)+"\n");
}

mod minify_vars {
    #[test]
	fn lower_case_long_var() {
		let test_code = "10 HOME\r\n20 PRINT hello";
		let expected = "10HOME\n20PRINThe";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn upper_case_long_variable() {
		let test_code = "10 HOME\n20 PRINT HELLO";
		let expected = "10HOME\n20PRINTHE";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn lower_case_long_string() {
		let test_code = "10 HOME\n20 PRINT hello$";
		let expected = "10HOME\n20PRINThe$";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn upper_case_long_string() {
		let test_code = "10 HOME\n20 PRINT HELLO$";
		let expected = "10HOME\n20PRINTHE$";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn lower_case_long_int() {
		let test_code = "10 HOME\n20 PRINT hello%";
		let expected = "10HOME\n20PRINThe%";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn upper_case_long_int() {
		let test_code = "10 HOME\n20 PRINT HELLO%";
		let expected = "10HOME\n20PRINTHE%";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn lower_case_long_array_name() {
		let test_code = "10 print aero(xa1b2,ya2b1)";
		let expected = "10printae(xa,ya)";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn upper_case_long_array_name() {
		let test_code = "10 PRINT AERO(XA1B2,YA2B1)";
		let expected = "10PRINTAE(XA,YA)";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn lower_case_long_string_array() {
		let test_code = "10 print aero$(xa1b2,ya2b1)";
		let expected = "10printae$(xa,ya)";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn upper_case_long_string_array() {
		let test_code = "10 PRINT AERO$(XA1B2,YA2B1)";
		let expected = "10PRINTAE$(XA,YA)";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn lower_case_long_int_array() {
		let test_code = "10 print aero%(xa1b2,ya2b1)";
		let expected = "10printae%(xa,ya)";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn upper_case_long_int_array() {
		let test_code = "10 PRINT AERO%(XA1B2,YA2B1)";
		let expected = "10PRINTAE%(XA,YA)";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn short_variables_only () {
		let test_code = "10 PRINT A%(X,Y) A$(X%,Y%)";
		let expected = "10PRINTA%(X,Y)A$(X%,Y%)";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn amp_func_vars() {
		let test_code = "10 & MYFUNC (HELLO+Y,AERO%(XA1B2,YA2B1),aero$(x1ab2,y1ab1))";
		let expected = "10& MYFUNC (HELLO+Y,AERO%(XA1B2,YA2B1),aero$(x1ab2,y1ab1))";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn amp_expr_list() {
		let test_code = "10 & (\"cmd\",HELLO+Y,AERO%(XA1B2,YA2B1),aero$(x1ab2,y1ab1))";
		let expected = "10&(\"cmd\",HE+Y,AE%(XA,YA),ae$(x1,y1))";
		super::test_minify(test_code ,expected, 3);
	}
    #[test]
	fn amp_overloaded_toks() {
		let test_code = "10 & draw \"subcmd\" at HELLO+Y,AERO%(XA1B2,YA2B1) and aero%(x1ab2,y1ab1)";
		let expected = "10& draw \"subcmd\" at HELLO+Y,AERO%(XA1B2,YA2B1) and aero%(x1ab2,y1ab1)";
		super::test_minify(test_code, expected, 1);
	}
}

mod minify_vars_with_guards {
    #[test]
	fn to_and_step_guards() {
		let test_code = "10 for x = ca12345 t o abracadabra step 5";
		let expected = "10forx=(ca)to(ab)step5";
		// TODO: could save 2 bytes by minifying as `10forx=cato(ab)step5`
		// (Apple tokenizer will resolve ATO correctly)
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn atn_guard() {
		let test_code = "10 hlin x,xrght at n";
		let expected = "10hlinx,xrat n";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn ato_guard() {
		let test_code = "10 draw br at o1,o2";
		let expected = "10drawbrat o1,o2";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn logic_guards() {
		let test_code = "10 if hf123 or it123 and nobody then 100";
		let expected = "10if(hf)or(it)and(no)then100";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn logic_non_guards() {
		let test_code = "10 if hf123% or it123% and nobody% then 100";
		let expected = "10ifhf%orit%andno%then100";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn negated_logic_guards() {
		let test_code = "10 if not hf123 or not it123 and not nobody then 100";
		let expected = "10ifnot(hf)ornot(it)andnot(no)then100";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn not_worth_shortening() {
		let test_code = "10 if not hf12 or not it12 and not nobo then 100";
		let expected = "10ifnothf12ornotit12andnotnobothen100";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn spaces_thrown_in() {
		let test_code = "10 for x = ca1  23 45 to abrac adabra step 5";
		let expected = "10forx=(ca)to(ab)step5";
		super::test_minify(test_code, expected, 1);
	}
}

mod minify_funcs {
    #[test]
	fn lower_case_long_function() {
		let test_code = "10 DEF FN abcd(x12) = x12^2\n20 PRINT FN abcd(x12)";
		let expected = "10DEFFNab(x1)=x1^2\n20PRINTFNab(x1)";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn upper_case_long_function() {
		let test_code = "10 DEF FN ABCD(X12) = X12^2\n20 PRINT FN ABCD(X12)";
		let expected = "10DEFFNAB(X1)=X1^2\n20PRINTFNAB(X1)";
		super::test_minify(test_code, expected, 1);
	}
}

mod minify_separators {
    #[test]
	fn unnecessary_unquote() {
		let test_code = "10 HOME\n20 PRINT \"HELLO\"";
		let expected = "10HOME\n20PRINT\"HELLO";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn unnecessary_unquote_sexpr() {
		let test_code = "10 HOME\n20 A$ = A$ + B$ + \"HELLO\"";
		let expected = "10HOME\n20A$=A$+B$+\"HELLO";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn print_with_nulls() {
		let test_code = "10 print a,b, ,c;d$;;;e$";
		let expected = "10printa,b,,c;d$;;;e$";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn trailing_colon() {
		let test_code = "10 goto 10:";
		let expected = "10goto10";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn extra_colons() {
		let test_code = "10 goto 10::goto 20:::goto 30::::";
		let expected = "10goto10:goto20:goto30";
		super::test_minify(test_code, expected, 1);
	}
    #[test]
	fn trailing_colon_after_unquote() {
		let test_code = "10 print \"1\": print \"2\":";
		let expected = "10print\"1\":print\"2";
		super::test_minify(test_code, expected, 1);
	}
}