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