playwright_rs/protocol/
select_option.rs1#[derive(Debug, Clone, PartialEq)]
26#[non_exhaustive]
27pub enum SelectOption {
28 Value(String),
30 Label(String),
32 Index(usize),
34}
35
36impl SelectOption {
37 pub(crate) fn to_json(&self) -> serde_json::Value {
44 match self {
45 SelectOption::Value(v) => serde_json::json!({"value": v}),
46 SelectOption::Label(l) => serde_json::json!({"label": l}),
47 SelectOption::Index(i) => serde_json::json!({"index": i}),
48 }
49 }
50}
51
52impl From<&str> for SelectOption {
54 fn from(value: &str) -> Self {
55 SelectOption::Value(value.to_string())
56 }
57}
58
59impl From<String> for SelectOption {
61 fn from(value: String) -> Self {
62 SelectOption::Value(value)
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_select_option_value() {
72 let opt = SelectOption::Value("test-value".to_string());
73 let json = opt.to_json();
74
75 assert_eq!(json["value"], "test-value");
76 assert!(json["label"].is_null());
77 assert!(json["index"].is_null());
78 }
79
80 #[test]
81 fn test_select_option_label() {
82 let opt = SelectOption::Label("Test Label".to_string());
83 let json = opt.to_json();
84
85 assert_eq!(json["label"], "Test Label");
86 assert!(json["value"].is_null());
87 assert!(json["index"].is_null());
88 }
89
90 #[test]
91 fn test_select_option_index() {
92 let opt = SelectOption::Index(2);
93 let json = opt.to_json();
94
95 assert_eq!(json["index"], 2);
96 assert!(json["value"].is_null());
97 assert!(json["label"].is_null());
98 }
99
100 #[test]
101 fn test_from_str() {
102 let opt: SelectOption = "my-value".into();
103 assert_eq!(opt, SelectOption::Value("my-value".to_string()));
104 }
105
106 #[test]
107 fn test_from_string() {
108 let opt: SelectOption = String::from("my-value").into();
109 assert_eq!(opt, SelectOption::Value("my-value".to_string()));
110 }
111}