use crate::composition_objects::{Plain, Text, Workflow};
use crate::validators::*;
use serde::Serialize;
use slack_messaging_derive::Builder;
#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
#[serde(tag = "type", rename = "workflow_button")]
pub struct WorkflowButton {
#[builder(validate("required", "text_object::max_75"))]
pub(crate) text: Option<Text<Plain>>,
#[builder(validate("required", "text::max_255"))]
pub(crate) action_id: Option<String>,
#[builder(validate("required"))]
pub(crate) workflow: Option<Workflow>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(private_setter)]
pub(crate) style: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(validate("text::max_75"))]
pub(crate) accessibility_label: Option<String>,
}
impl WorkflowButtonBuilder {
pub fn primary(self) -> Self {
self.style("primary")
}
pub fn danger(self) -> Self {
self.style("danger")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::composition_objects::test_helpers::*;
use crate::errors::*;
#[test]
fn it_implements_builder() {
let expected = WorkflowButton {
text: Some(plain_text("Run Workflow")),
action_id: Some("workflow_button_0".into()),
workflow: Some(workflow()),
style: Some("primary"),
accessibility_label: Some("Run Workflow!".into()),
};
let val = WorkflowButton::builder()
.set_text(Some(plain_text("Run Workflow")))
.set_action_id(Some("workflow_button_0"))
.set_workflow(Some(workflow()))
.primary()
.set_accessibility_label(Some("Run Workflow!"))
.build()
.unwrap();
assert_eq!(val, expected);
let expected = WorkflowButton {
style: Some("danger"),
..expected
};
let val = WorkflowButton::builder()
.text(plain_text("Run Workflow"))
.action_id("workflow_button_0")
.workflow(workflow())
.danger()
.accessibility_label("Run Workflow!")
.build()
.unwrap();
assert_eq!(val, expected);
}
#[test]
fn it_requries_text_field() {
let err = WorkflowButton::builder()
.action_id("workflow_button_0")
.workflow(workflow())
.build()
.unwrap_err();
assert_eq!(err.object(), "WorkflowButton");
let errors = err.field("text");
assert!(errors.includes(ValidationErrorKind::Required));
}
#[test]
fn it_requires_text_length_less_than_75_characters_long() {
let err = WorkflowButton::builder()
.text(plain_text("a".repeat(76)))
.action_id("workflow_button_0")
.workflow(workflow())
.build()
.unwrap_err();
assert_eq!(err.object(), "WorkflowButton");
let errors = err.field("text");
assert!(errors.includes(ValidationErrorKind::MaxTextLength(75)));
}
#[test]
fn it_requries_action_id_field() {
let err = WorkflowButton::builder()
.text(plain_text("Run Workflow"))
.workflow(workflow())
.build()
.unwrap_err();
assert_eq!(err.object(), "WorkflowButton");
let errors = err.field("action_id");
assert!(errors.includes(ValidationErrorKind::Required));
}
#[test]
fn it_requries_action_id_less_than_255_characters_long() {
let err = WorkflowButton::builder()
.text(plain_text("Run Workflow"))
.action_id("a".repeat(256))
.workflow(workflow())
.build()
.unwrap_err();
assert_eq!(err.object(), "WorkflowButton");
let errors = err.field("action_id");
assert!(errors.includes(ValidationErrorKind::MaxTextLength(255)));
}
#[test]
fn it_requries_workflow_field() {
let err = WorkflowButton::builder()
.text(plain_text("Run Workflow"))
.action_id("workflow_button_0")
.build()
.unwrap_err();
assert_eq!(err.object(), "WorkflowButton");
let errors = err.field("workflow");
assert!(errors.includes(ValidationErrorKind::Required));
}
#[test]
fn it_requries_accessibility_label_less_than_75_characters_long() {
let err = WorkflowButton::builder()
.text(plain_text("Run Workflow"))
.action_id("workflow_button_0")
.workflow(workflow())
.accessibility_label("a".repeat(76))
.build()
.unwrap_err();
assert_eq!(err.object(), "WorkflowButton");
let errors = err.field("accessibility_label");
assert!(errors.includes(ValidationErrorKind::MaxTextLength(75)));
}
}