Unified · Split
| @@ -372,7 +372,7 @@ fn value_impl<'a>(i: &'a [u8], receive_event: &mut impl FnMut(Event<'a>)) -> IRe · 371 unmodified lines | |||
| 372 | } | 372 | } |
| 373 | 373 | ||
| 374 | let (i, remainder_value) = { | 374 | let (i, remainder_value) = { |
| 375 | let mut new_index = parsed_index; | 375 | let mut new_index = 0; |
| 376 | for index in (offset..parsed_index).rev() { | 376 | for index in (offset..parsed_index).rev() { |
| 377 | if !i[index].is_ascii_whitespace() { | 377 | if !i[index].is_ascii_whitespace() { |
| 378 | new_index = index + 1; | 378 | new_index = index + 1; |
| @@ -163,6 +163,7 @@ mod config_name { · 162 unmodified lines | |||
| 163 | } | 163 | } |
| 164 | 164 | ||
| 165 | mod section { | 165 | mod section { |
| 166 | use crate::parse::tests::util::newline_custom_event; | ||
| 166 | use crate::parse::{ | 167 | use crate::parse::{ |
| 167 | error::ParseNode, | 168 | error::ParseNode, |
| 168 | section, | 169 | section, |
| @@ -199,6 +200,54 @@ mod section { · 30 unmodified lines | |||
| 199 | }) | 200 | }) |
| 200 | } | 201 | } |
| 201 | 202 | ||
| 203 | #[test] | ||
| 204 | fn empty_value_with_windows_newlines() { | ||
| 205 | let mut node = ParseNode::SectionHeader; | ||
| 206 | assert_eq!( | ||
| 207 | section(b"[a] k = \r\n", &mut node).unwrap(), | ||
| 208 | fully_consumed(( | ||
| 209 | Section { | ||
| 210 | header: parsed_section_header("a", None), | ||
| 211 | events: vec![ | ||
| 212 | whitespace_event(" "), | ||
| 213 | name_event("k"), | ||
| 214 | whitespace_event(" "), | ||
| 215 | Event::KeyValueSeparator, | ||
| 216 | whitespace_event(" "), | ||
| 217 | value_event(""), | ||
| 218 | newline_custom_event("\r\n") | ||
| 219 | ] | ||
| 220 | .into(), | ||
| 221 | }, | ||
| 222 | 1 | ||
| 223 | )), | ||
| 224 | ); | ||
| 225 | } | ||
| 226 | |||
| 227 | #[test] | ||
| 228 | fn simple_value_with_windows_newlines() { | ||
| 229 | let mut node = ParseNode::SectionHeader; | ||
| 230 | assert_eq!( | ||
| 231 | section(b"[a] k = v\r\n", &mut node).unwrap(), | ||
| 232 | fully_consumed(( | ||
| 233 | Section { | ||
| 234 | header: parsed_section_header("a", None), | ||
| 235 | events: vec![ | ||
| 236 | whitespace_event(" "), | ||
| 237 | name_event("k"), | ||
| 238 | whitespace_event(" "), | ||
| 239 | Event::KeyValueSeparator, | ||
| 240 | whitespace_event(" "), | ||
| 241 | value_event("v"), | ||
| 242 | newline_custom_event("\r\n") | ||
| 243 | ] | ||
| 244 | .into(), | ||
| 245 | }, | ||
| 246 | 1 | ||
| 247 | )), | ||
| 248 | ); | ||
| 249 | } | ||
| 250 | |||
| 202 | #[test] | 251 | #[test] |
| 203 | fn empty_section() { | 252 | fn empty_section() { |
| 204 | let mut node = ParseNode::SectionHeader; | 253 | let mut node = ParseNode::SectionHeader; |
| @@ -128,18 +128,20 @@ mod push { · 127 unmodified lines | |||
| 128 | #[test] | 128 | #[test] |
| 129 | fn values_are_escaped() { | 129 | fn values_are_escaped() { |
| 130 | for (value, expected) in [ | 130 | for (value, expected) in [ |
| 131 | ("a b", "$head\tk = a b\n"), | 131 | ("a b", "$head\tk = a b$nl"), |
| 132 | (" a b", "$head\tk = \" a b\"\n"), | 132 | (" a b", "$head\tk = \" a b\"$nl"), |
| 133 | ("a b\t", "$head\tk = \"a b\\t\"\n"), | 133 | ("a b\t", "$head\tk = \"a b\\t\"$nl"), |
| 134 | (";c", "$head\tk = \";c\"\n"), | 134 | (";c", "$head\tk = \";c\"$nl"), |
| 135 | ("#c", "$head\tk = \"#c\"\n"), | 135 | ("#c", "$head\tk = \"#c\"$nl"), |
| 136 | ("a\nb\n\tc", "$head\tk = a\\nb\\n\\tc\n"), | 136 | ("a\nb\n\tc", "$head\tk = a\\nb\\n\\tc$nl"), |
| 137 | ] { | 137 | ] { |
| 138 | let mut config = git_config::File::default(); | 138 | let mut config = git_config::File::default(); |
| 139 | let mut section = config.new_section("a", None).unwrap(); | 139 | let mut section = config.new_section("a", None).unwrap(); |
| 140 | section.set_implicit_newline(false); | 140 | section.set_implicit_newline(false); |
| 141 | section.push(Key::try_from("k").unwrap(), value); | 141 | section.push(Key::try_from("k").unwrap(), value); |
| 142 | let expected = expected.replace("$head", &format!("[a]{nl}", nl = section.newline())); | 142 | let expected = expected |
| 143 | .replace("$head", &format!("[a]{nl}", nl = section.newline())) | ||
| 144 | .replace("$nl", §ion.newline().to_string()); | ||
| 143 | assert_eq!(config.to_bstring(), expected); | 145 | assert_eq!(config.to_bstring(), expected); |
| 144 | } | 146 | } |
| 145 | } | 147 | } |
| @@ -42,26 +42,32 @@ mod set_string { · 41 unmodified lines | |||
| 42 | use crate::file::mutable::value::init_config; | 42 | use crate::file::mutable::value::init_config; |
| 43 | 43 | ||
| 44 | fn assert_set_string(expected: &str) { | 44 | fn assert_set_string(expected: &str) { |
| 45 | let nl = git_config::File::default().detect_newline_style().to_string(); | ||
| 45 | for input in [ | 46 | for input in [ |
| 46 | "[a] k = v", | 47 | "[a] k = v", |
| 47 | "[a] k = ", | 48 | "[a] k = ", |
| 48 | "[a] k =", | 49 | "[a] k =", |
| 49 | "[a] k =\n", | 50 | "[a] k =$nl", |
| 50 | "[a] k ", | 51 | "[a] k ", |
| 51 | "[a] k\n", | 52 | "[a] k$nl", |
| 52 | "[a] k", | 53 | "[a] k", |
| 53 | ] { | 54 | ] { |
| 54 | let mut file: git_config::File = input.parse().unwrap(); | 55 | let mut file: git_config::File = input.replace("$nl", &nl).parse().unwrap(); |
| 55 | let mut v = file.raw_value_mut("a", None, "k").unwrap(); | 56 | let mut v = file.raw_value_mut("a", None, "k").unwrap(); |
| 56 | v.set_string(expected); | 57 | v.set_string(expected); |
| 57 | 58 | ||
| 58 | assert_eq!(v.get().unwrap().as_ref(), expected); | 59 | assert_eq!(v.get().unwrap().as_ref(), expected); |
| 59 | 60 | ||
| 60 | let file: git_config::File = match file.to_string().parse() { | 61 | let file_string = file.to_string(); |
| 62 | let file: git_config::File = match file_string.parse() { | ||
| 61 | Ok(f) => f, | 63 | Ok(f) => f, |
| 62 | Err(err) => panic!("{:?} failed with: {}", file.to_string(), err), | 64 | Err(err) => panic!("{:?} failed with: {}", file_string, err), |
| 63 | }; | 65 | }; |
| 64 | assert_eq!(file.raw_value("a", None, "k").expect("present").as_ref(), expected); | 66 | assert_eq!( |
| 67 | file.raw_value("a", None, "k").expect("present").as_ref(), | ||
| 68 | expected, | ||
| 69 | "{file_string:?}" | ||
| 70 | ); | ||
| 65 | } | 71 | } |
| 66 | } | 72 | } |
| 67 | 73 | ||