From 6e0701caa513e11f939899c4d6c258537796c0d3 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 2 Jul 2014 18:43:32 -0700 Subject: Add a test for _ => - translation --- src/serialization.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/serialization.rs b/src/serialization.rs index 94bd201..7c11fb5 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use std::mem; use std::fmt; -use std::str; use serialize; use {Value, Table, Array, String, Integer, Float, Boolean, Parser}; @@ -558,7 +557,7 @@ impl serialize::Decoder for Decoder { let toml = match self.toml { Some(Table(ref mut table)) => { table.pop(&field) - .or_else(|| table.pop(&hyphenate(f_name))) + .or_else(|| table.pop(&f_name.replace("_", "-"))) }, ref found => return Err(self.mismatch("table", found)), }; @@ -698,10 +697,6 @@ impl serialize::Decoder for Decoder { } } -fn hyphenate(string: &str) -> String { - str::replace(string, "_", "-") -} - impl fmt::Show for DecodeError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { try!(match self.kind { @@ -781,6 +776,20 @@ mod tests { assert_eq!(v, decode!(Table(encode!(v)))); } + #[test] + fn smoke_hyphen() { + #[deriving(Encodable, Decodable, PartialEq, Show)] + struct Foo { a_b: int } + + let v = Foo { a_b: 2 }; + assert_eq!(encode!(v), map! { a_b: Integer(2) }); + assert_eq!(v, decode!(Table(encode!(v)))); + + let mut m = HashMap::new(); + m.insert("a-b".to_string(), Integer(2)); + assert_eq!(v, decode!(Table(encode!(v)))); + } + #[test] fn nested() { #[deriving(Encodable, Decodable, PartialEq, Show)] -- cgit v1.2.3