add tests, deal with pipes, newlines, tabs for to nuon (#6391)

* remove unnecessary FlatShape

* add proptest

* remove files that belonged in another PR

* more tests, more chars

* add exception for parser error unrelated ot PR
This commit is contained in:
pwygab 2022-09-01 20:08:19 +08:00 committed by GitHub
parent fbe9d6f529
commit 34e58bc5d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 123 additions and 6 deletions

50
Cargo.lock generated
View File

@ -2723,6 +2723,7 @@ dependencies = [
"pathdiff",
"polars",
"powierza-coefficient",
"proptest",
"quick-xml 0.23.0",
"quickcheck",
"quickcheck_macros",
@ -3795,6 +3796,26 @@ dependencies = [
"rustix",
]
[[package]]
name = "proptest"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0d9cc07f18492d879586c92b485def06bc850da3118075cd45d50e9c95b0e5"
dependencies = [
"bit-set",
"bitflags",
"byteorder",
"lazy_static",
"num-traits",
"quick-error 2.0.1",
"rand 0.8.5",
"rand_chacha 0.3.1",
"rand_xorshift",
"regex-syntax",
"rusty-fork",
"tempfile",
]
[[package]]
name = "pure-rust-locales"
version = "0.5.6"
@ -3817,6 +3838,12 @@ version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quick-error"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3"
[[package]]
name = "quick-xml"
version = "0.19.0"
@ -3986,6 +4013,15 @@ dependencies = [
"rand_core 0.5.1",
]
[[package]]
name = "rand_xorshift"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f"
dependencies = [
"rand_core 0.6.3",
]
[[package]]
name = "rayon"
version = "1.5.3"
@ -4330,6 +4366,18 @@ version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24c8ad4f0c00e1eb5bc7614d236a7f1300e3dbd76b68cac8e06fb00b015ad8d8"
[[package]]
name = "rusty-fork"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb3dcc6e454c328bb824492db107ab7c0ae8fcffe4ad210136ef014458c1bc4f"
dependencies = [
"fnv",
"quick-error 1.2.3",
"tempfile",
"wait-timeout",
]
[[package]]
name = "ryu"
version = "1.0.10"
@ -4885,7 +4933,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36e39da5d30887b5690e29de4c5ebb8ddff64ebd9933f98a01daaa4fd11b36ea"
dependencies = [
"peresil",
"quick-error",
"quick-error 1.2.3",
"sxd-document",
]

View File

@ -152,6 +152,7 @@ shadow-rs = { version = "0.16.1", default-features = false }
[dev-dependencies]
hamcrest2 = "0.3.0"
dirs-next = "2.0.0"
proptest = "1.0.0"
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
rstest = {version = "0.15.0", default-features = false}

View File

@ -0,0 +1,23 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 96a80ecd19729fb43a7b7bb2766b37d6083ba73b16abb97075875e3cfcdc763f # shrinks to c = '"'
cc 4146602559ea717a02bcef3c6d73cdf613c30d0c3f92c48e26c79b9a1544e027 # shrinks to c = '\\'
cc 80532a0ee73df456a719b9e3cce1ae5f3d26009dde819cbaf16f8e0cb6709705 # shrinks to c = ':'
cc cdb88505686eea3c74c36f282fd29b2b68bc118ded4ebfc36f9838d174bd7653 # shrinks to c = '`'
cc 0f534d55f9771e8810b9c4252a4168abfaec1a35e1b0cac12dbaf726d295a08c # shrinks to c = '\0'
cc 5d31bcbab722acd1f4e23ca3a4f95ff309a636b45a73ca8ae9f820d93ff57acc # shrinks to c = '{'
cc 5afec063bc96160d681d77f90041b67ef5cfdea4dcbd12d984fd828fbeb4b421 # shrinks to c = '#'
cc f919beb3ee5c70e756a15635d65ded7d44f3ae58b5e86b6c09e814d5d8cdd506 # shrinks to c = ';'
cc ec00f39b8d45dfd8808947a56af5e50ba5a0ef7c951723b45377815a02e515b1 # shrinks to c = '('
cc 25b773cdf4c24179151fa86244c7de4136e05df9e94e6ee77a336ebfd8764444 # shrinks to c = '|'
cc 94dc0d54b97d59e1c0f4cb11bdccb3823a1bb908cbc3fd643ee8f067169fad72 # shrinks to c = '0'
cc c9d0051fb1e5a8bdc1d4f5a3dceac1b4b465827d1dff4fc3a3755baae6a7bb48 # shrinks to c = '$'
cc 14ec40d2eb5bd2663e9b11bb49fb2120852f9ea71678c69d732161412b55a3ec # shrinks to s = ""
cc d4afccc51ed9d421bdb7e1723e273dfb6e77c3a449489671a496db234e87c5ed # shrinks to c = '\r'
cc 515a56d73eb1b69290ef4c714b629421989879aebd57991bd2c2bf11294353b1 # shrinks to s = "\\\\𐊀{"
cc 111566990fffa432acd2dbc845141b0e7870f97125c7621e3ddf142204568246 # shrinks to s = "'\":`"
cc 0424c33000d9188be96b3049046eb052770b2158bf5ebb0c98656d7145e8aca9 # shrinks to s = "0"

View File

@ -192,15 +192,23 @@ fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
fn needs_quotes(string: &str) -> bool {
string.contains(' ')
|| string.contains(',')
|| string.contains('[')
|| string.contains(']')
|| string.contains(':')
|| string.contains('`')
|| string.contains('{')
|| string.contains('}')
|| string.contains('#')
|| string.contains('\'')
|| string.contains(';')
|| string.contains('(')
|| string.contains(')')
|| string.contains('[')
|| string.contains(']')
|| string.contains('{')
|| string.contains('}')
|| string.contains('|')
|| string.contains('$')
|| string.contains(',')
|| string.contains('\t')
|| string.contains('\n')
|| string.contains('\r')
}
#[cfg(test)]

View File

@ -1,4 +1,5 @@
use nu_test_support::{nu, pipeline};
use proptest::prelude::*;
#[test]
fn to_nuon_correct_compaction() {
@ -267,3 +268,39 @@ fn to_nuon_does_not_quote_unnecessarily() {
));
assert_eq!(actual.out, "{\"ro name\": sam, rank: 10}");
}
proptest! {
#[test]
fn to_nuon_from_nuon(c: char) {
if c != '\0' && c!='\r' {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
format!(r#"
{{"prop{0}test": "sam"}} | to nuon | from nuon;
[ [ "prop{0}test" ]; [ 'test' ] ] | to nuon | from nuon;
[ [ "{0}" ]; [ 'test' ] ] | to nuon | from nuon;
{{"{0}": "sam"}} | to nuon | from nuon;
"#, c).as_ref()
));
assert!(actual.err.is_empty() || actual.err.contains("Unexpected end of code") || actual.err.contains("only strings can be keys"));
// The second is for weird escapes due to backslashes
// The third is for chars like '0'
}
}
#[test]
fn to_nuon_from_nuon_string(s: String) {
if s != "\\0" && s!= "" && !s.contains('\\') && !s.contains('"'){
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
format!(r#"
{{"prop{0}test": "sam"}} | to nuon | from nuon;
[ [ "prop{0}test" ]; [ 'test' ] ] | to nuon | from nuon;
[ [ "{0}" ]; [ 'test' ] ] | to nuon | from nuon;
{{"{0}": "sam"}} | to nuon | from nuon;
"#, s).as_ref()
));
assert!(actual.err.is_empty() || actual.err.contains("only strings can be keys") || actual.err.contains("unknown command"));
// TODO: fix parser error for "unknown command" when '=$' is the name
}
}
}