-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathexpression_spec.rs
148 lines (127 loc) · 5.25 KB
/
expression_spec.rs
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
#[cfg(test)]
mod tests {
use async_graphql::Value;
use pretty_assertions::assert_eq;
use serde_json::json;
use tailcall::core::blueprint::{Blueprint, DynamicValue};
use tailcall::core::http::RequestContext;
use tailcall::core::ir::model::IR;
use tailcall::core::ir::{EmptyResolverContext, Error, EvalContext};
use tailcall::core::mustache::Mustache;
async fn eval(expr: &IR) -> Result<Value, Error> {
let runtime = tailcall::cli::runtime::init(&Blueprint::default());
let req_ctx = RequestContext::new(runtime);
let res_ctx = EmptyResolverContext {};
let mut eval_ctx = EvalContext::new(&req_ctx, &res_ctx);
expr.eval(&mut eval_ctx).await
}
#[tokio::test]
async fn test_and_then() {
let abcde = DynamicValue::try_from(&json!({"a": {"b": {"c": {"d": "e"}}}})).unwrap();
let expr = IR::Dynamic(abcde)
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.a}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.b}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.c}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.d}}",
))));
let actual = eval(&expr).await.unwrap();
let expected = Value::from_json(json!("e")).unwrap();
assert_eq!(actual, expected);
}
#[tokio::test]
async fn test_with_args() {
let expr =
IR::Dynamic(DynamicValue::try_from(&json!({"a": {"b": {"c": {"d": "e"}}}})).unwrap())
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.a.b.c.d}}",
))));
let actual = eval(&expr).await.unwrap();
let expected = Value::from_json(json!("e")).unwrap();
assert_eq!(actual, expected);
}
#[tokio::test]
async fn test_with_args_piping() {
let expr =
IR::Dynamic(DynamicValue::try_from(&json!({"a": {"b": {"c": {"d": "e"}}}})).unwrap())
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.a}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.b}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.c}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.d}}",
))));
let actual = eval(&expr).await.unwrap();
let expected = Value::from_json(json!("e")).unwrap();
assert_eq!(actual, expected);
}
#[tokio::test]
async fn test_optional_dot_in_expression() {
let args =
IR::Dynamic(DynamicValue::try_from(&json!({"a": {"b": {"c": {"d": "e"}}}})).unwrap());
let expr_with_dot =
args.clone()
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{.args.a.b.c.d}}",
))));
let expr_without_dot = args.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.a.b.c.d}}",
))));
let actual_with_dot = eval(&expr_with_dot).await.unwrap();
let actual_without_dot = eval(&expr_without_dot).await.unwrap();
let expected = Value::from_json(json!("e")).unwrap();
assert_eq!(actual_with_dot, expected);
assert_eq!(actual_without_dot, expected);
}
#[tokio::test]
async fn test_optional_dot_piping() {
let expr =
IR::Dynamic(DynamicValue::try_from(&json!({"a": {"b": {"c": {"d": "e"}}}})).unwrap())
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{.args.a}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{.args.b}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{.args.c}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{.args.d}}",
))));
let actual = eval(&expr).await.unwrap();
let expected = Value::from_json(json!("e")).unwrap();
assert_eq!(actual, expected);
}
#[tokio::test]
async fn test_mixed_dot_usages() {
let expr =
IR::Dynamic(DynamicValue::try_from(&json!({"a": {"b": {"c": {"d": "e"}}}})).unwrap())
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{.args.a}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.b}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{.args.c}}",
))))
.pipe(IR::Dynamic(DynamicValue::Mustache(Mustache::parse(
"{{args.d}}",
))));
let actual = eval(&expr).await.unwrap();
let expected = Value::from_json(json!("e")).unwrap();
assert_eq!(actual, expected);
}
}