aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/question/strict_confirmation_question_test.rs
blob: de8911bbd3ca47e75a552775d11117ff269815f4 (plain)
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
//! ref: composer/tests/Composer/Test/Question/StrictConfirmationQuestionTest.php

use std::cell::RefCell;
use std::rc::Rc;

use shirabe::question::StrictConfirmationQuestion;
use shirabe_external_packages::symfony::console::helper::question_helper::QuestionHelper;
use shirabe_external_packages::symfony::console::input::array_input::ArrayInput;
use shirabe_external_packages::symfony::console::input::streamable_input_interface::StreamableInputInterface;
use shirabe_external_packages::symfony::console::output::output_interface::OutputInterface;
use shirabe_external_packages::symfony::console::output::stream_output::StreamOutput;
use shirabe_php_shim::PhpMixed;

const TRUE_ANSWER_REGEX: &str = "/^y(?:es)?$/i";
const FALSE_ANSWER_REGEX: &str = "/^no?$/i";

/// @return string[][]
fn get_ask_confirmation_bad_data() -> Vec<&'static str> {
    vec!["not correct", "no more", "yes please", "yellow"]
}

#[test]
fn test_ask_confirmation_bad_answer() {
    for answer in get_ask_confirmation_bad_data() {
        let (mut input, mut dialog) = create_input(&format!("{}\n", answer));

        let mut question = StrictConfirmationQuestion::new(
            "Do you like French fries?".to_string(),
            true,
            TRUE_ANSWER_REGEX.to_string(),
            FALSE_ANSWER_REGEX.to_string(),
        );
        question.inner_mut().set_max_attempts(Some(1)).unwrap();

        let error = dialog
            .ask(&mut input, create_output_interface(), question.inner())
            .expect_err("expected an InvalidArgumentException");
        assert_eq!(error.to_string(), "Please answer yes, y, no, or n.");
    }
}

#[test]
fn test_ask_confirmation() {
    for (question, expected, default) in get_ask_confirmation_data() {
        let (mut input, mut dialog) = create_input(&format!("{}\n", question));

        let question = StrictConfirmationQuestion::new(
            "Do you like French fries?".to_string(),
            default,
            TRUE_ANSWER_REGEX.to_string(),
            FALSE_ANSWER_REGEX.to_string(),
        );
        assert_eq!(
            dialog
                .ask(&mut input, create_output_interface(), question.inner())
                .unwrap()
                .unwrap(),
            PhpMixed::Bool(expected),
            "confirmation question should {}",
            if expected { "pass" } else { "cancel" }
        );
    }
}

/// @return mixed[][]
fn get_ask_confirmation_data() -> Vec<(&'static str, bool, bool)> {
    vec![
        ("", true, true),
        ("", false, false),
        ("y", true, true),
        ("yes", true, true),
        ("n", false, true),
        ("no", false, true),
    ]
}

#[test]
fn test_ask_confirmation_with_custom_true_and_false_answer() {
    let question = StrictConfirmationQuestion::new(
        "Do you like French fries?".to_string(),
        false,
        "/^ja$/i".to_string(),
        "/^nein$/i".to_string(),
    );

    let (mut input, mut dialog) = create_input("ja\n");
    assert_eq!(
        dialog
            .ask(&mut input, create_output_interface(), question.inner())
            .unwrap()
            .unwrap(),
        PhpMixed::Bool(true)
    );

    let (mut input, mut dialog) = create_input("nein\n");
    assert_eq!(
        dialog
            .ask(&mut input, create_output_interface(), question.inner())
            .unwrap()
            .unwrap(),
        PhpMixed::Bool(false)
    );
}

/// @return resource
fn get_input_stream(input: &str) -> shirabe_php_shim::PhpResource {
    let stream = shirabe_php_shim::php_fopen_resource("php://memory", "r+");

    shirabe_php_shim::fwrite_resource(&stream, input);
    shirabe_php_shim::rewind(&stream);

    stream
}

fn create_output_interface() -> Rc<RefCell<dyn OutputInterface>> {
    let stream = shirabe_php_shim::php_fopen_resource("php://memory", "r+");
    let output = StreamOutput::new(stream, None, None, None)
        .unwrap()
        .expect("php://memory is a valid stream");
    Rc::new(RefCell::new(output))
}

/// @return array{ArrayInput, QuestionHelper}
fn create_input(entry: &str) -> (ArrayInput, QuestionHelper) {
    let mut input = ArrayInput::new(
        vec![(
            PhpMixed::Int(0),
            PhpMixed::String("--no-interaction".to_string()),
        )],
        None,
    )
    .unwrap();
    input.set_stream(get_input_stream(entry));

    let dialog = QuestionHelper::default();

    (input, dialog)
}