Repository navigation
Expand file tree
/
Copy pathconditionals.rs
More file actions
275 lines (257 loc) · 10.6 KB
/
Copy pathconditionals.rs
File metadata and controls
275 lines (257 loc) · 10.6 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#[path = "support/native.rs"]
mod native;
use morphir_extension_sdk::{
prelude::*,
protocol::{ExtensionRequest, methods},
};
use morphir_python_binding::PythonExtension;
use serde_json::{Value, json};
fn request(source: &str) -> CompileRequest {
CompileRequest {
language_id: "python".into(),
sources: SourceSet {
root: None,
documents: vec![SourceDocument {
uri: "models.py".into(),
language_id: "python".into(),
version: 1,
text: source.into(),
}],
},
package: CompilePackage {
name: "acme/example".into(),
exposed_modules: None,
},
dependencies: vec![],
options: CompileOptions {
ir_version: "4".into(),
..Default::default()
},
baseline: None,
}
}
fn compile(source: &str) -> CompileResult {
let extension = native::an_initialized_extension();
let response = extension
.protocol()
.handle(ExtensionRequest::new(methods::COMPILE, request(source), 1).unwrap());
serde_json::from_value(response.result.unwrap()).unwrap()
}
fn generate(ir: Value) -> GenerateResult {
PythonExtension
.generate(GenerateRequest {
ir,
target: "python".into(),
options: Default::default(),
})
.unwrap()
}
fn roundtrip(source: &str) -> Value {
let compiled = compile(source);
assert!(compiled.success, "{:?}", compiled.diagnostics);
let ir = compiled.ir.unwrap();
let generated = generate(ir.clone());
assert!(generated.success, "{:?}", generated.diagnostics);
let again = compile(&generated.artifacts[0].content);
assert!(again.success, "{:?}", again.diagnostics);
assert_eq!(again.ir.as_ref(), Some(&ir));
ir
}
#[test]
fn conditionals_lower_to_canonical_if_then_else_and_roundtrip() {
let ir = roundtrip(include_str!("fixtures/conditionals.py"));
let choose = &ir["distribution"]["Library"]["def"]["modules"]["models"]["Public"]["values"]["choose"]
["Public"]["ExpressionBody"];
assert_eq!(
choose["inputTypes"],
json!({"flag": "morphir/SDK:basics#bool", "first": "morphir/SDK:basics#int", "second": "morphir/SDK:basics#int"})
);
assert_eq!(choose["outputType"], json!("morphir/SDK:basics#int"));
assert_eq!(
choose["body"],
json!({"IfThenElse": {"condition": {"Variable": "flag"}, "then": {"Variable": "first"}, "else": {"Variable": "second"}}})
);
}
#[test]
fn branches_can_select_adt_and_tuple_parameters() {
roundtrip(&format!(
"{}\ndef select(flag: bool, first: Decision, second: Decision) -> Decision:\n return first if flag else second\n\ndef pair(flag: bool, first: tuple[int, str], second: tuple[int, str]) -> tuple[int, str]:\n return first if flag else second\n",
include_str!("fixtures/models.py")
));
}
#[test]
fn tuple_types_and_values_have_distinct_ir_encodings() {
let ir = roundtrip(
"def pair(amount: int, label: str) -> tuple[int, str]:\n return amount, label\n",
);
let definition = &ir["distribution"]["Library"]["def"]["modules"]["models"]["Public"]["values"]
["pair"]["Public"]["ExpressionBody"];
assert_eq!(
definition["outputType"],
json!({"Tuple": ["morphir/SDK:basics#int", "morphir/SDK:string#string"]})
);
assert_eq!(
definition["body"],
json!({"Tuple": [{"Variable": "amount"}, {"Variable": "label"}]})
);
}
#[test]
fn named_and_nested_tuple_aliases_roundtrip() {
roundtrip(include_str!("fixtures/tuples.py"));
roundtrip(
"type Pair = tuple[int, str]\ndef select(flag: bool, pair: Pair) -> tuple[int, str]:\n return pair if flag else (0, 'none')\n",
);
roundtrip(
"type Pair = tuple[int, str]\ntype Nested = tuple[Pair, bool]\ndef make_nested(value: int) -> Nested:\n return ((value, 'value'), True)\n",
);
}
#[test]
fn invalid_tuple_types_and_values_are_rejected() {
for source in [
"def f() -> tuple[int, str]:\n return (1, 2)\n",
"def f() -> tuple[int, str]:\n return (1, 'a', True)\n",
"def f(flag: bool) -> tuple[int, str]:\n return (1, 'a') if flag else (2, False)\n",
"def f() -> tuple[int]:\n return (1,)\n",
"def f() -> tuple[()]:\n return ()\n",
"type Pair = tuple[int, ...]\n",
"type Pair = tuple[int, Pair]\n",
"type A = tuple[int, B]\ntype B = tuple[str, A]\n",
"type Pair = tuple[int, str]\ndef f() -> Pair:\n return [1, 'a']\n",
"def f(pair: tuple[int, str]) -> tuple[int, str]:\n return (*pair,)\n",
] {
let result = compile(source);
assert!(!result.success, "accepted {source}");
assert!(result.ir.is_none());
}
}
#[test]
fn backend_accepts_independent_tuple_value_and_rejects_wrong_elements() {
let mut ir = independent_ir(
json!({"Tuple": [{"Literal": {"IntegerLiteral": 1}}, {"Literal": {"StringLiteral": "one"}}]}),
);
ir["distribution"]["Library"]["def"]["modules"]["models"]["Public"]["values"]["select"]["Public"]
["ExpressionBody"]["outputType"] =
json!({"Tuple": ["morphir/SDK:basics#int", "morphir/SDK:string#string"]});
let generated = generate(ir.clone());
assert!(generated.success, "{:?}", generated.diagnostics);
assert_eq!(
compile(&generated.artifacts[0].content).ir,
Some(ir.clone())
);
ir["distribution"]["Library"]["def"]["modules"]["models"]["Public"]["values"]["select"]["Public"]
["ExpressionBody"]["body"]["Tuple"][1] = json!({"Literal": {"BoolLiteral": true}});
let invalid = generate(ir);
assert!(!invalid.success);
assert!(invalid.artifacts.is_empty());
}
#[test]
fn comparisons_and_literal_boundaries_roundtrip() {
for operator in ["==", "!=", "<", "<=", ">", ">="] {
roundtrip(&format!(
"def compare(left: int, right: int) -> str:\n return 'yes' if left {operator} right else 'no'\n"
));
}
roundtrip(
"def limits(flag: bool) -> int:\n return -9223372036854775808 if flag else 9223372036854775807\n",
);
roundtrip(
"def text(flag: bool) -> str:\n return 'line\\n\\x00\\U0001f600' if flag else '\"quoted\"'\n",
);
}
#[test]
fn arbitrary_precision_integers_preserve_exact_values() {
for (literal, decimal) in [
("9223372036854775808", "9223372036854775808"),
("18_446_744_073_709_551_616", "18446744073709551616"),
("0x1_0000_0000_0000_0000", "18446744073709551616"),
("0O2000000000000000000000", "18446744073709551616"),
(
"0B10000000000000000000000000000000000000000000000000000000000000000",
"18446744073709551616",
),
(
"1234567890123456789012345678901234567890",
"1234567890123456789012345678901234567890",
),
] {
for sign in ["", "-"] {
let ir = roundtrip(&format!(
"def number() -> int:\n return {sign}{literal}\n"
));
let value = &ir["distribution"]["Library"]["def"]["modules"]["models"]["Public"]["values"]
["number"]["Public"]["ExpressionBody"]["body"]["Literal"]["IntegerLiteral"];
assert_eq!(value.to_string(), format!("{sign}{decimal}"));
}
}
}
#[test]
fn invalid_functions_fail_without_partial_ir() {
for source in [
"def f(flag: bool) -> int:\n if flag:\n return 1\n",
"def f(flag: bool) -> int:\n return 1 if flag else 'no'\n",
"def f(flag: int) -> int:\n return 1 if flag else 2\n",
"def f(flag: bool) -> int:\n return missing if flag else 2\n",
"def f(flag: bool) -> int:\n if flag:\n print('side effect')\n return 1\n return 2\n",
"def f(flag: bool) -> int:\n result = 1\n return result\n",
"def f(flag: bool = True) -> int:\n return 1\n",
"def f(flag) -> int:\n return 1\n",
"def f(flag: bool):\n return 1\n",
"async def f(flag: bool) -> int:\n return 1\n",
"def f(*, flag: bool) -> int:\n return 1\n",
"def f(flag: bool, /) -> int:\n return 1\n",
"def f(a_b: bool, aB: bool) -> int:\n return 1\n",
"def f(Class: bool) -> int:\n return 1\n",
"def f(flag: bool) -> int:\n return\n",
"def f(flag: bool) -> int:\n return 1\n return 2\n",
"def f(flag: bool) -> float:\n return 1e400\n",
"def f(flag: bool) -> int:\n return 1 if flag < True else 2\n",
"def f(a: int, b: str) -> int:\n return 1 if a == b else 2\n",
"def f(flag: bool) -> int:\n return 1 if 0 < 1 < 2 else 2\n",
] {
let result = compile(source);
assert!(!result.success, "accepted {source}");
assert!(result.ir.is_none());
assert!(!result.diagnostics.is_empty());
}
}
#[test]
fn types_only_does_not_silently_emit_function_bodies() {
let mut request = request(include_str!("fixtures/conditionals.py"));
request.options.types_only = true;
let result = PythonExtension.compile(request).unwrap();
assert!(!result.success);
assert!(result.ir.is_none());
}
fn independent_ir(body: Value) -> Value {
let definition = json!({"ExpressionBody": {
"inputTypes": {"flag": "morphir/SDK:basics#bool"},
"outputType": "morphir/SDK:basics#int", "body": body
}});
json!({"formatVersion": 4, "distribution": {"Library": {
"packageName": "acme/example", "dependencies": {}, "def": {"modules": {
"models": {"Public": {"types": {}, "values": {"select": {"Public": definition}}}}
}}
}}})
}
#[test]
fn backend_accepts_independent_if_then_else_ir() {
let ir = independent_ir(
json!({"IfThenElse": {"condition": {"Variable": "flag"}, "then": {"Literal": {"IntegerLiteral": 1}}, "else": {"Literal": {"IntegerLiteral": 2}}}}),
);
let result = generate(ir.clone());
assert!(result.success, "{:?}", result.diagnostics);
assert_eq!(compile(&result.artifacts[0].content).ir, Some(ir));
}
#[test]
fn backend_rejects_invalid_bodies_without_artifacts() {
for body in [
json!({"IfThenElse": {"condition": {"Literal": {"IntegerLiteral": 1}}, "then": {"Literal": {"IntegerLiteral": 1}}, "else": {"Literal": {"IntegerLiteral": 2}}}}),
json!({"IfThenElse": {"condition": {"Variable": "flag"}, "then": {"Literal": {"IntegerLiteral": 1}}, "else": {"Literal": {"StringLiteral": "no"}}}}),
json!({"Variable": "unknown"}),
json!({"Reference": "acme/example:models#select"}),
] {
let result = generate(independent_ir(body));
assert!(!result.success);
assert!(result.artifacts.is_empty());
}
}