Skip to content

Commit

Permalink
fix!: Make order-preserving the default (#148)
Browse files Browse the repository at this point in the history
This will make things less surprising, especially as we add key order
preserving with dotted keys
  • Loading branch information
epage authored Sep 1, 2021
1 parent 4864b83 commit a7e1daf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl Display for Table {
impl Document {
/// Returns a string representation of the TOML document, attempting to keep
/// the table headers in their original order.
pub fn to_string_in_original_order(&self) -> String {
fn to_string_in_original_order(&self) -> String {
let mut string = String::new();
let mut path = Vec::new();
let mut last_position = 0;
Expand Down Expand Up @@ -230,8 +230,8 @@ impl Document {

impl Display for Document {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.as_table())?;
write!(f, "{}", self.trailing)
let s = self.to_string_in_original_order();
s.fmt(f)
}
}

Expand Down
67 changes: 18 additions & 49 deletions tests/test_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ mod tests {
PrettyString(&self.doc.to_string()));
self
}

fn produces_in_original_order(&self, expected: &str) -> &Self {
assert_eq!(
PrettyString(expected),
PrettyString(&self.doc.to_string_in_original_order()));
self
}

fn produces(&self, expected: &str) -> &Self {
self.produces_display(expected).produces_in_original_order(expected);
self
}
}

// insertion
Expand All @@ -94,7 +82,7 @@ fn test_insert_leaf_table() {
root["servers"]["beta"] = table();
root["servers"]["beta"]["ip"] = value("10.0.0.2");
root["servers"]["beta"]["dc"] = value("eqdc10");
}).produces(r#"
}).produces_display(r#"
[servers]
[servers.alpha]
Expand All @@ -121,14 +109,6 @@ fn test_inserted_leaf_table_goes_after_last_sibling() {
).running(|root| {
root["dependencies"]["newthing"] = table();
}).produces_display(r#"
[package]
[dependencies]
[dependencies.opencl]
[dependencies.newthing]
[[example]]
[dev-dependencies]
"#).produces_in_original_order(r#"
[package]
[dependencies]
[[example]]
Expand All @@ -146,7 +126,7 @@ fn test_inserting_tables_from_different_parsed_docs() {
).running(|root| {
let other = "[b]".parse::<Document>().unwrap();
root["b"] = other["b"].clone();
}).produces(
}).produces_display(
"[a]\n[b]\n"
);
}
Expand All @@ -159,7 +139,7 @@ fn test_insert_nonleaf_table() {
root["servers"]["alpha"] = table();
root["servers"]["alpha"]["ip"] = value("10.0.0.1");
root["servers"]["alpha"]["dc"] = value("eqdc10");
}).produces(r#"
}).produces_display(r#"
[other.table]
[servers]
Expand All @@ -185,7 +165,7 @@ fn test_insert_array() {
first["hello"] = value("world");
}
array.append(Table::new());
}).produces(r#"
}).produces_display(r#"
[package]
title = "withoutarray"
Expand All @@ -206,7 +186,7 @@ fn test_insert_values() {
root["tbl"]["key1"] = value("value1");
root["tbl"]["key2"] = value(42);
root["tbl"]["key3"] = value(8.1415926);
}).produces(r#"
}).produces_display(r#"
[tbl]
key1 = "value1"
key2 = 42
Expand Down Expand Up @@ -236,7 +216,7 @@ fn test_remove_leaf_table() {
let servers = root.entry("servers");
let servers = as_table!(servers);
assert!(servers.remove("alpha").is_some());
}).produces(r#"
}).produces_display(r#"
[servers]
[servers.beta]
Expand Down Expand Up @@ -283,7 +263,7 @@ fn test_remove_nonleaf_table() {
"#).running(|root| {
assert!(root.remove("a").is_some());
}).produces(r#"
}).produces_display(r#"
title = "not relevant"
# comment 2
[b] # comment 2.1
Expand Down Expand Up @@ -320,7 +300,7 @@ fn test_remove_array_entry() {
assert_eq!(dmp.len(), 2);
dmp.remove(1);
assert_eq!(dmp.len(), 1);
}).produces(r#"
}).produces_display(r#"
[package]
name = "hello"
version = "1.0.0"
Expand Down Expand Up @@ -354,7 +334,7 @@ fn test_remove_array() {
path = "src/bin/dmp/main.rs""#
).running(|root| {
assert!(root.remove("bin").is_some());
}).produces(r#"
}).produces_display(r#"
[package]
name = "hello"
version = "1.0.0"
Expand Down Expand Up @@ -382,7 +362,7 @@ fn test_remove_value() {
assert!(value.is_str());
let value = value.as_str().unwrap();
assert_eq!(value, "1.0.0");
}).produces(r#"
}).produces_display(r#"
name = "hello"
documentation = "https://docs.rs/hello"
"#
Expand All @@ -405,7 +385,7 @@ fn test_remove_last_value_from_implicit() {
assert!(value.is_value());
let value = value.as_value().unwrap();
assert_eq!(value.as_integer(), Some(1));
}).produces(r#""#);
}).produces_display(r#""#);
}

// values
Expand All @@ -427,17 +407,6 @@ fn test_sort_values() {
let a = as_table!(a);
a.sort_values();
}).produces_display(r#"
[a]
a = 1
# this comment is attached to b
b = 2 # as well as this
c = 3
[a.z]
[a.y]
"#
).produces_in_original_order(r#"
[a.z]
[a]
Expand Down Expand Up @@ -466,7 +435,7 @@ fn test_set_position() {
as_table!(segmented).set_position(5)
}
}
}).produces_in_original_order(r#" [dependencies]
}).produces_display(r#" [dependencies]
[package]
[dev-dependencies]
Expand All @@ -486,7 +455,7 @@ fn test_multiple_zero_positions() {
for (_, table) in root.iter_mut() {
as_table!(table).set_position(0)
}
}).produces_in_original_order(r#"
}).produces_display(r#"
[package]
[dependencies]
[dev-dependencies]
Expand All @@ -507,7 +476,7 @@ fn test_multiple_max_usize_positions() {
for (_, table) in root.iter_mut() {
as_table!(table).set_position(usize::MAX)
}
}).produces_in_original_order(r#" [dependencies.opencl]
}).produces_display(r#" [dependencies.opencl]
a=""
[package]
Expand Down Expand Up @@ -559,7 +528,7 @@ fn test_insert_replace_into_array() {
// This should replace formatting.
assert_eq!(b.replace_formatted(4, Value::from("yikes").decorated(" ", "")).as_str(), Some("test"));

}).produces(r#"
}).produces_display(r#"
a = [1, 2, 3, 4]
b = ["hello", "beep", "zoink" ,
"world"
Expand All @@ -586,7 +555,7 @@ fn test_remove_from_array() {
assert_eq!(b.len(), 1);
assert!(b.remove(0).is_str());
assert!(b.is_empty());
}).produces(r#"
}).produces_display(r#"
a = [1, 2, 3]
b = []
"#
Expand Down Expand Up @@ -625,7 +594,7 @@ fn test_insert_into_inline_table() {
b.get_or_insert("hello", "world");
assert_eq!(b.len(), 1);
b.fmt()
}).produces(r#"
}).produces_display(r#"
a = { a = 2, c = 3, b = 42 }
b = { hello = "world" }
"#
Expand All @@ -650,7 +619,7 @@ fn test_remove_from_inline_table() {
assert_eq!(b.len(), 1);
assert!(b.remove("hello").is_some());
assert!(b.is_empty());
}).produces(r#"
}).produces_display(r#"
a = {a=2, b = 42}
b = {}
"#
Expand Down
31 changes: 0 additions & 31 deletions tests/test_valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,6 @@ macro_rules! t(
)
);

#[test]
fn test_table_reordering() {
let toml = r#"
[[bin]] # bin 1
[a.b.c.e]
[a]
[other.table]
[[bin]] # bin 2
[a.b.c.d]
[a.b.c]
[[bin]] # bin 3
"#;
let expected = r#"
[[bin]] # bin 1
[[bin]] # bin 2
[[bin]] # bin 3
[a]
[a.b.c]
[a.b.c.e]
[a.b.c.d]
[other.table]
"#;
let doc = toml.parse::<Document>();
assert!(doc.is_ok());
let doc = doc.unwrap();

assert_eq!(doc.to_string(), expected);
assert_eq!(doc.to_string_in_original_order(), toml);
}

#[test]
fn test_key_unification() {
let toml = r#"
Expand All @@ -130,7 +100,6 @@ fn test_key_unification() {
let doc = doc.unwrap();

assert_eq!(doc.to_string(), expected);
assert_eq!(doc.to_string_in_original_order(), expected);
}

t!(
Expand Down

0 comments on commit a7e1daf

Please sign in to comment.