csmlinterpreter 0.2.0

The CSML (Conversational Standard Meta Language) is a Domain-Specific Language developed for creating conversational experiences easily.
Documentation
mod support;

use csmlinterpreter::data::{Event, MessageData};
use csmlinterpreter::interpret;
use serde_json::Value;

use support::tools::{gen_context, gen_event, message_to_json_value, read_file};

fn format_message(event: Event, file: &str, step: &str) -> MessageData {
    let text = read_file(format!("CSML/basic_test/built-in/{}.csml", file)).unwrap();

    let context = gen_context(serde_json::json!({}), serde_json::json!({}));

    interpret(&text, step, context, &event, None)
}

#[test]
fn ok_button() {
    let data = r#" {
        "messages":[
            {
                "content": {
                    "accepts": ["toto", "hello"],
                    "button_type": "quick_button",
                    "payload": "hello",
                    "title": "hello",
                    "theme": "primary",

                    "content": {"payload": "hello", "title": "hello"},
                    "content_type": "button"
                },
                "content_type": "button"
            }
        ],
        "next_flow": null,
        "memories": [],
        "next_step": "end"
    }"#;

    let msg = format_message(gen_event(""), "question", "simple_0");

    let v1: Value = message_to_json_value(msg);
    let v2: Value = serde_json::from_str(data).unwrap();

    assert_eq!(v1, v2)
}

#[test]
fn ok_question() {
    let data = r#"
    {"messages":
        [ {
            "content": {
                "accepts": ["b1", "b2"],
                "buttons": [
                    {
                        "accepts": ["b1"],
                        "button_type": "quick_button",
                        "payload": "b1",
                        "title": "b1",
                        "theme": "primary",

                        "content": {"payload": "b1", "title": "b1"},
                        "content_type": "button"
                    },
                    {
                        "accepts": ["b2"],
                        "button_type": "quick_button",
                        "payload": "b2",
                        "title": "b2",
                        "theme": "primary",

                        "content": {"payload": "b2", "title": "b2"},
                        "content_type": "button"
                    }
                ],
                "title": "title"
            },
            "content_type":"question"
        } ],
    "next_flow":null,
    "memories":[],
    "next_step":"end"
    }"#;
    let msg = format_message(gen_event(""), "question", "start");

    let v1: Value = message_to_json_value(msg);
    let v2: Value = serde_json::from_str(data).unwrap();

    assert_eq!(v1, v2)
}

#[test]
fn ok_question_step1() {
    let data = r#"
    { "messages":
        [ {
            "content": {
                "accepts": ["b1", "b2"],
                "buttons": [
                    {
                        "accepts": ["b1"],
                        "button_type": "quick_button",
                        "payload": "b1",
                        "title": "b1",
                        "theme": "primary",

                        "content": {"payload": "b1", "title": "b1"},
                        "content_type": "button"
                    },
                    {
                        "accepts": ["b2"],
                        "button_type": "quick_button",
                        "payload": "b2",
                        "title": "b2",
                        "theme": "primary",

                        "content": {"payload": "b2", "title": "b2"},
                        "content_type": "button"
                    }
                ],
                "title": "title"
            },
            "content_type":"question"
        } ],
    "next_flow":null,
    "memories":[],
    "next_step":"end"
    }"#;
    let msg = format_message(gen_event(""), "question", "question1");

    let v1: Value = message_to_json_value(msg);
    let v2: Value = serde_json::from_str(data).unwrap();

    assert_eq!(v1, v2)
}

#[test]
fn ok_question_step2() {
    let data = r#"
    { "messages":
        [ {
            "content": {
                "accepts": ["b1", "b2"],
                "buttons": [
                    {
                        "accepts": ["b1"],
                        "button_type": "quick_button",
                        "payload": "b1",
                        "title": "b1",
                        "theme": "primary",

                        "content": {"payload": "b1", "title": "b1"},
                        "content_type": "button"
                    },
                    {
                        "accepts": ["b2"],
                        "button_type": "quick_button",
                        "payload": "b2",
                        "title": "b2",
                        "theme": "primary",

                        "content": {"payload": "b2", "title": "b2"},
                        "content_type": "button"
                    }
                ]
            },
            "content_type":"question"
        } ],
    "next_flow":null,
    "memories":[],
    "next_step":"end"
    }"#;
    let msg = format_message(gen_event(""), "question", "question2");

    let v1: Value = message_to_json_value(msg);
    let v2: Value = serde_json::from_str(data).unwrap();

    assert_eq!(v1, v2)
}