2022-02-28 13:10:02 +01:00
use nu_test_support ::{ nu , pipeline } ;
2022-09-01 14:08:19 +02:00
use proptest ::prelude ::* ;
2022-02-28 13:10:02 +01:00
#[ test ]
fn to_nuon_correct_compaction ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
open appveyor . yml
| to nuon
| str length
| $in > 500
" #
) ) ;
assert_eq! ( actual . out , " true " ) ;
}
#[ test ]
fn to_nuon_list_of_numbers ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
[ 1 , 2 , 3 , 4 ]
| to nuon
| from nuon
| $in = = [ 1 , 2 , 3 , 4 ]
" #
) ) ;
assert_eq! ( actual . out , " true " ) ;
}
#[ test ]
fn to_nuon_list_of_strings ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
[ abc , xyz , def ]
| to nuon
| from nuon
| $in = = [ abc , xyz , def ]
" #
) ) ;
assert_eq! ( actual . out , " true " ) ;
}
#[ test ]
fn to_nuon_table ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
[ [ my , columns ] ; [ abc , xyz ] , [ def , ijk ] ]
| to nuon
| from nuon
| $in = = [ [ my , columns ] ; [ abc , xyz ] , [ def , ijk ] ]
" #
) ) ;
assert_eq! ( actual . out , " true " ) ;
}
#[ test ]
fn to_nuon_bool ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
2022-03-03 01:55:03 +01:00
false
2022-02-28 13:10:02 +01:00
| to nuon
| from nuon
" #
) ) ;
assert_eq! ( actual . out , " false " ) ;
}
2022-03-25 20:35:37 +01:00
#[ test ]
fn to_nuon_escaping ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
" hello \" world "
| to nuon
| from nuon
" #
) ) ;
assert_eq! ( actual . out , " hello \" world " ) ;
}
#[ test ]
fn to_nuon_escaping2 ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
" hello \\ world "
| to nuon
| from nuon
" #
) ) ;
assert_eq! ( actual . out , " hello \\ world " ) ;
}
2022-10-04 13:25:21 +02:00
#[ test ]
fn to_nuon_escaping3 ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
[ " hello \\ world " ]
| to nuon
| from nuon
| $in = = [ hello \ world ]
" #
) ) ;
assert_eq! ( actual . out , " true " ) ;
}
#[ test ]
fn to_nuon_escaping4 ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
[ " hello \" world " ]
| to nuon
| from nuon
| $in = = [ " hello \" world " ]
" #
) ) ;
assert_eq! ( actual . out , " true " ) ;
}
#[ test ]
fn to_nuon_escaping5 ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
{ s : " hello \" world " }
| to nuon
| from nuon
| $in = = { s : " hello \" world " }
" #
) ) ;
assert_eq! ( actual . out , " true " ) ;
}
2022-02-28 13:10:02 +01:00
#[ test ]
fn to_nuon_negative_int ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
- 1
| to nuon
| from nuon
" #
) ) ;
assert_eq! ( actual . out , " -1 " ) ;
}
#[ test ]
fn to_nuon_records ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
{ name : " foo bar " , age : 100 , height : 10 }
| to nuon
| from nuon
| $in = = { name : " foo bar " , age : 100 , height : 10 }
" #
) ) ;
assert_eq! ( actual . out , " true " ) ;
}
2022-03-01 00:31:53 +01:00
#[ test ]
fn binary_to ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
0 x [ ab cd ef ] | to nuon
" #
) ) ;
assert_eq! ( actual . out , " 0x[ABCDEF] " ) ;
}
#[ test ]
fn binary_roundtrip ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
" 0x[1f ff] " | from nuon | to nuon
" #
) ) ;
assert_eq! ( actual . out , " 0x[1FFF] " ) ;
}
2022-03-07 14:39:02 +01:00
#[ test ]
fn read_binary_data ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
open sample . nuon | get 5.3
" #
) ) ;
assert_eq! ( actual . out , " 31 " )
}
#[ test ]
fn read_record ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
open sample . nuon | get 4. name
" #
) ) ;
assert_eq! ( actual . out , " Bobby " )
}
#[ test ]
fn read_bool ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
2022-03-19 20:12:10 +01:00
open sample . nuon | get 3 | $in = = true
2022-03-07 14:39:02 +01:00
" #
) ) ;
assert_eq! ( actual . out , " true " )
}
2022-06-13 19:45:07 +02:00
#[ test ]
fn float_doesnt_become_int ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
1.0 | to nuon
" #
) ) ;
assert_eq! ( actual . out , " 1.0 " )
}
2022-06-17 20:01:37 +02:00
#[ test ]
fn float_inf_parsed_properly ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
inf | to nuon
" #
) ) ;
assert_eq! ( actual . out , " inf " )
}
#[ test ]
fn float_neg_inf_parsed_properly ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
- inf | to nuon
" #
) ) ;
assert_eq! ( actual . out , " -inf " )
}
#[ test ]
fn float_nan_parsed_properly ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
NaN | to nuon
" #
) ) ;
assert_eq! ( actual . out , " NaN " )
}
2022-08-21 12:12:13 +02:00
#[ test ]
fn to_nuon_converts_columns_with_spaces ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
let test = [ [ a , b , " c d " ] ; [ 1 2 3 ] [ 4 5 6 ] ] ; $test | to nuon | from nuon
" #
) ) ;
assert! ( actual . err . is_empty ( ) ) ;
}
2022-08-23 13:51:07 +02:00
2022-12-30 09:49:35 +01:00
#[ test ]
fn to_nuon_quotes_empty_string ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
let test = " " ; $test | to nuon
" #
) ) ;
assert! ( actual . err . is_empty ( ) ) ;
assert_eq! ( actual . out , r # """"# )
}
#[ test ]
fn to_nuon_quotes_empty_string_in_list ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
let test = [ " " ] ; $test | to nuon | from nuon | $in = = [ " " ]
" #
) ) ;
assert! ( actual . err . is_empty ( ) ) ;
assert_eq! ( actual . out , " true " )
}
#[ test ]
fn to_nuon_quotes_empty_string_in_table ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
let test = [ [ a , b ] ; [ ' ' , la ] [ le lu ] ] ; $test | to nuon | from nuon
" #
) ) ;
assert! ( actual . err . is_empty ( ) ) ;
}
2022-08-23 13:51:07 +02:00
#[ test ]
2022-11-19 12:09:39 +01:00
fn does_not_quote_strings_unnecessarily ( ) {
2022-08-23 13:51:07 +02:00
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
let test = [ [ " a " , " b " , " c d " ] ; [ 1 2 3 ] [ 4 5 6 ] ] ; $test | to nuon
" #
) ) ;
assert_eq! ( actual . out , " [[a, b, \" c d \" ]; [1, 2, 3], [4, 5, 6]] " ) ;
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
let a = { " ro name " : " sam " rank : 10 } ; $a | to nuon
" #
) ) ;
assert_eq! ( actual . out , " { \" ro name \" : sam, rank: 10} " ) ;
}
2022-09-01 14:08:19 +02:00
2022-11-19 12:09:39 +01:00
#[ test ]
fn quotes_some_strings_necessarily ( ) {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
r #"
[ ' true ',' false ',' null ' ,
' NaN ',' NAN ',' nan ',' + nan ',' - nan ' ,
' inf ',' + inf ',' - inf ',' INF ' ,
' Infinity ',' + Infinity ',' - Infinity ',' INFINITY ' ,
' + 19.99 ',' - 19.99 ' , ' 19.99 b ' ,
' 19.99 kb ',' 19.99 mb ',' 19.99 gb ',' 19.99 tb ',' 19.99 pb ',' 19.99 eb ',' 19.99 zb ' ,
' 19.99 kib ',' 19.99 mib ',' 19.99 gib ',' 19.99 tib ',' 19.99 pib ',' 19.99 eib ',' 19.99 zib ' ,
' 19 ns ' , ' 19 us ' , ' 19 ms ' , ' 19 sec ' , ' 19 min ' , ' 19 hr ' , ' 19 day ' , ' 19 wk ' ,
' - 11. 0 .. - 15.0 ' , ' 11. 0 .. - 15.0 ' , ' - 11. 0 .. 15.0 ' ,
' - 11. 0 .. < - 15.0 ' , ' 11. 0 .. < - 15.0 ' , ' - 11. 0 .. < 15.0 ' ,
' - 11. 0 .. ' , ' 11. 0 .. ' , ' .. 15.0 ' , ' .. - 15.0 ' , ' .. < 15.0 ' , ' .. < - 15.0 ' ,
2022-12-22 00:36:07 +01:00
' 2000 - 01 - 01 ' , ' 2022 - 02 - 02 T14 :30 :00 ' , ' 2022 - 02 - 02 T14 :30 :00 + 05 :00 ' ,
2022-12-30 09:49:35 +01:00
',' , ' '
2022-12-22 00:36:07 +01:00
' & & '
2022-11-19 12:09:39 +01:00
] | to nuon | from nuon | describe
" #
) ) ;
assert_eq! ( actual . out , " list<string> " ) ;
}
2022-12-22 01:42:23 +01:00
#[ test ]
fn read_code_should_fail_rather_than_panic ( ) {
let actual = nu! ( cwd : " tests/fixtures/formats " , pipeline (
r # "open code.nu | from nuon"#
) ) ;
assert! ( actual . err . contains ( " error when parsing " ) )
}
2022-09-01 14:08:19 +02:00
proptest! {
#[ test ]
fn to_nuon_from_nuon ( c : char ) {
if c ! = '\0' & & c ! = '\r' {
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
format! ( r #"
2023-01-30 02:37:54 +01:00
{ { " prop{c}test " : " sam " } } | to nuon | from nuon ;
[ [ " prop{c}test " ] ; [ ' test ' ] ] | to nuon | from nuon ;
[ [ " {c} " ] ; [ ' test ' ] ] | to nuon | from nuon ;
{ { " {c} " : " sam " } } | to nuon | from nuon ;
" #).as_ref()
2022-09-01 14:08:19 +02:00
) ) ;
2023-01-24 09:05:46 +01:00
assert! ( actual . err . is_empty ( ) | | actual . err . contains ( " Unexpected end of code " ) | | actual . err . contains ( " only strings can be keys " ) | | actual . err . contains ( " unbalanced { and } " ) ) ;
2022-09-01 14:08:19 +02:00
// 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 ) {
2022-09-25 22:06:13 +02:00
if s ! = " \\ 0 " & & ! s . is_empty ( ) & & ! s . contains ( '\\' ) & & ! s . contains ( '"' ) {
2022-09-01 14:08:19 +02:00
let actual = nu! (
cwd : " tests/fixtures/formats " , pipeline (
format! ( r #"
2023-01-30 02:37:54 +01:00
{ { " prop{s}test " : " sam " } } | to nuon | from nuon ;
[ [ " prop{s}test " ] ; [ ' test ' ] ] | to nuon | from nuon ;
[ [ " {s} " ] ; [ ' test ' ] ] | to nuon | from nuon ;
{ { " {s} " : " sam " } } | to nuon | from nuon ;
" #).as_ref()
2022-09-01 14:08:19 +02:00
) ) ;
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
}
}
}