diff --git a/adana-script/src/tests/struc.rs b/adana-script/src/tests/struc.rs index 05007dd..1a86144 100644 --- a/adana-script/src/tests/struc.rs +++ b/adana-script/src/tests/struc.rs @@ -402,3 +402,187 @@ fn test_struc_access_key10() { Primitive::String("whatever9".to_string()) ); } +#[test] +fn test_struc_access_key11() { + let mut ctx = BTreeMap::new(); + let expr = r#" + settings = struct { + static: struct {}, + middlewares: [ + struct { + path: "/hello/:name", + handler: (req) => { + println(req) + return struct { + status: 200, + body: struct { response: """hello ${req.params.name}!""" }, + headers: struct { "Content-Type": "application/json"} + } + }, + method: "GET" + }, + struct { + path: "/", + handler: (req) => { + println(req) + return "hello bro!" + }, + method: "GET" + } + ] + } + settings.middlewares[0].handler(struct {params: struct {name: "nordine"}}) + "#; + let r = compute(expr, &mut ctx, "N/A").unwrap(); + assert_eq!( + r, + Primitive::Struct(BTreeMap::from([ + ( + "body".to_string(), + Primitive::Struct(BTreeMap::from([( + "response".to_string(), + Primitive::String("hello nordine!".to_string()) + ),])) + ), + ("status".to_string(), Primitive::U8(200)), + ( + "headers".to_string(), + Primitive::Struct(BTreeMap::from([( + "Content-Type".to_string(), + Primitive::String("application/json".to_string()) + )])) + ) + ])) + ); +} + +#[test] +fn test_struc_access_key12() { + let mut ctx = BTreeMap::new(); + let expr = r#" + handler = (req) => { + println(req) + return struct { + status: 200, + body: struct { response: """hello ${req.params.name}!""" }, + headers: struct { "Content-Type": "application/json"} + } + + } + handler(struct {params: struct {name: "nordine"}}) + "#; + let r = compute(expr, &mut ctx, "N/A").unwrap(); + assert_eq!( + r, + Primitive::Struct(BTreeMap::from([ + ( + "body".to_string(), + Primitive::Struct(BTreeMap::from([( + "response".to_string(), + Primitive::String("hello nordine!".to_string()) + ),])) + ), + ("status".to_string(), Primitive::U8(200)), + ( + "headers".to_string(), + Primitive::Struct(BTreeMap::from([( + "Content-Type".to_string(), + Primitive::String("application/json".to_string()) + )])) + ) + ])) + ); +} + +#[test] +fn test_struc_access_key13() { + let mut ctx = BTreeMap::new(); + let expr = r#" + settings = struct { + static: struct {}, + middlewares: [ + struct { + path: "/hello/:name", + handler: (req) => { + println(req) + res= struct { + status: 200, + body: struct { response: """hello ${req.params.name}!""" }, + headers: struct { "Content-Type": "application/json"} + } + return res + }, + method: "GET" + }, + struct { + path: "/", + handler: (req) => { + println(req) + return "hello bro!" + }, + method: "GET" + } + ] + } + settings.middlewares[0].handler(struct {params: struct {name: "nordine"}}) + "#; + let r = compute(expr, &mut ctx, "N/A").unwrap(); + assert_eq!( + r, + Primitive::Struct(BTreeMap::from([ + ( + "body".to_string(), + Primitive::Struct(BTreeMap::from([( + "response".to_string(), + Primitive::String("hello nordine!".to_string()) + ),])) + ), + ("status".to_string(), Primitive::U8(200)), + ( + "headers".to_string(), + Primitive::Struct(BTreeMap::from([( + "Content-Type".to_string(), + Primitive::String("application/json".to_string()) + )])) + ) + ])) + ); +} +#[test] +fn test_struc_access_key14() { + let mut ctx = BTreeMap::new(); + let expr = r#" + handler = (req) => { + println(req) + res= struct { + status: 200, + body: struct { response: """hello ${req.params.name}!""" }, + headers: struct { "Content-Type": "application/json"} + } + return res + + } + handler(struct {params: struct {name: "nordine"}}) + "#; + let r = compute(expr, &mut ctx, "N/A").unwrap(); + assert_eq!( + r, + Primitive::Struct(BTreeMap::from([ + ( + "body".to_string(), + Primitive::Struct(BTreeMap::from([( + "response".to_string(), + Primitive::String("hello nordine!".to_string()) + ),])) + ), + ("status".to_string(), Primitive::U8(200)), + ( + "headers".to_string(), + Primitive::Struct(BTreeMap::from([( + "Content-Type".to_string(), + Primitive::String("application/json".to_string()) + )])) + ) + ])) + ); +}