Experiment: Allow both $true/true and $false/false (#4696)

* Change true/false to keywords

* oops, clippy

* Both kinds of bools

* Add in some boolean variables

* disable py virtualenv test for now
This commit is contained in:
JT
2022-03-02 19:55:03 -05:00
committed by GitHub
parent fd88920a9d
commit 96a1bf5f8d
50 changed files with 173 additions and 100 deletions

View File

@ -162,17 +162,17 @@ let default_theme = {
# The default config record. This is where much of your global configuration is setup.
let $config = {
filesize_metric: $false
filesize_metric: false
table_mode: rounded # basic, compact, compact_double, light, thin, with_love, rounded, reinforced, heavy, none, other
use_ls_colors: $true
rm_always_trash: $false
use_ls_colors: true
rm_always_trash: false
color_config: $default_theme
use_grid_icons: $true
use_grid_icons: true
footer_mode: "25" # always, never, number_of_rows, auto
quick_completions: $true # set this to $false to prevent auto-selecting completions when only one remains
animate_prompt: $false # redraw the prompt every second
quick_completions: true # set this to false to prevent auto-selecting completions when only one remains
animate_prompt: false # redraw the prompt every second
float_precision: 2
use_ansi_coloring: $true
use_ansi_coloring: true
filesize_format: "auto" # b, kb, kib, mb, mib, gb, gib, tb, tib, pb, pib, eb, eib, zb, zib, auto
edit_mode: emacs # emacs, vi
max_history_size: 10000

View File

@ -2,22 +2,22 @@ use crate::tests::{run_test, TestResult};
#[test]
fn if_test1() -> TestResult {
run_test("if $true { 10 } else { 20 } ", "10")
run_test("if true { 10 } else { 20 } ", "10")
}
#[test]
fn if_test2() -> TestResult {
run_test("if $false { 10 } else { 20 } ", "20")
run_test("if false { 10 } else { 20 } ", "20")
}
#[test]
fn simple_if() -> TestResult {
run_test("if $true { 10 } ", "10")
run_test("if true { 10 } ", "10")
}
#[test]
fn simple_if2() -> TestResult {
run_test("if $false { 10 } ", "")
run_test("if false { 10 } ", "")
}
#[test]

View File

@ -3,7 +3,7 @@ use crate::tests::{fail_test, run_test, TestResult};
#[test]
fn no_scope_leak1() -> TestResult {
fail_test(
"if $false { let $x = 10 } else { let $x = 20 }; $x",
"if false { let $x = 10 } else { let $x = 20 }; $x",
"Variable not found",
)
}

View File

@ -17,7 +17,7 @@ fn proper_shadow() -> TestResult {
fn config_filesize_format_with_metric_true() -> TestResult {
// Note: this tests both the config variable and that it is properly captured into a block
run_test(
r#"let config = {"filesize_metric": $true "filesize_format": "kib" }; do { 40kb | into string } "#,
r#"let config = {"filesize_metric": true "filesize_format": "kib" }; do { 40kb | into string } "#,
"39.1 KiB",
)
}
@ -26,7 +26,7 @@ fn config_filesize_format_with_metric_true() -> TestResult {
fn config_filesize_format_with_metric_false_kib() -> TestResult {
// Note: this tests both the config variable and that it is properly captured into a block
run_test(
r#"let config = {"filesize_metric": $false "filesize_format": "kib" }; do { 40kb | into string } "#,
r#"let config = {"filesize_metric": false "filesize_format": "kib" }; do { 40kb | into string } "#,
"39.1 KiB",
)
}
@ -35,7 +35,7 @@ fn config_filesize_format_with_metric_false_kib() -> TestResult {
fn config_filesize_format_with_metric_false_kb() -> TestResult {
// Note: this tests both the config variable and that it is properly captured into a block
run_test(
r#"let config = {"filesize_metric": $false "filesize_format": "kb" }; do { 40kb | into string } "#,
r#"let config = {"filesize_metric": false "filesize_format": "kb" }; do { 40kb | into string } "#,
"40.0 KB",
)
}
@ -265,15 +265,25 @@ fn datetime_literal() -> TestResult {
#[test]
fn shortcircuiting_and() -> TestResult {
run_test(r#"$false && (5 / 0; $false)"#, "false")
run_test(r#"false && (5 / 0; false)"#, "false")
}
#[test]
fn shortcircuiting_or() -> TestResult {
run_test(r#"$true || (5 / 0; $false)"#, "true")
run_test(r#"true || (5 / 0; false)"#, "true")
}
#[test]
fn open_ended_range() -> TestResult {
run_test(r#"1.. | first 100000 | length"#, "100000")
}
#[test]
fn bool_variable() -> TestResult {
run_test(r#"$true"#, "true")
}
#[test]
fn bool_variable2() -> TestResult {
run_test(r#"$false"#, "false")
}

View File

@ -27,12 +27,12 @@ fn modulo2() -> TestResult {
#[test]
fn and() -> TestResult {
run_test("$true && $false", "false")
run_test("true && false", "false")
}
#[test]
fn or() -> TestResult {
run_test("$true || $false", "true")
run_test("true || false", "true")
}
#[test]

View File

@ -4,7 +4,7 @@ use super::run_test_contains;
#[test]
fn env_shorthand() -> TestResult {
run_test("FOO=BAR if $false { 3 } else { 4 }", "4")
run_test("FOO=BAR if false { 3 } else { 4 }", "4")
}
#[test]

View File

@ -284,6 +284,32 @@ pub(crate) fn eval_source(
true
}
fn seems_like_number(bytes: &[u8]) -> bool {
if bytes.is_empty() {
false
} else {
let b = bytes[0];
b == b'0'
|| b == b'1'
|| b == b'2'
|| b == b'3'
|| b == b'4'
|| b == b'5'
|| b == b'6'
|| b == b'7'
|| b == b'8'
|| b == b'9'
|| b == b'('
|| b == b'{'
|| b == b'['
|| b == b'$'
|| b == b'"'
|| b == b'\''
|| b == b'-'
}
}
/// Finds externals that have names that look like math expressions
pub fn external_exceptions(engine_state: &EngineState, stack: &Stack) -> Vec<Vec<u8>> {
let mut executables = vec![];
@ -299,16 +325,16 @@ pub fn external_exceptions(engine_state: &EngineState, stack: &Stack) -> Vec<Vec
while let Some(Ok(item)) = contents.next() {
if is_executable::is_executable(&item.path()) {
if let Ok(name) = item.file_name().into_string() {
let name = name.as_bytes().to_vec();
if nu_parser::is_math_expression_like(&name) {
if seems_like_number(name.as_bytes()) {
let name = name.as_bytes().to_vec();
executables.push(name);
}
}
if let Some(name) = item.path().file_stem() {
let name = name.to_string_lossy();
let name = name.as_bytes().to_vec();
if nu_parser::is_math_expression_like(&name) {
if seems_like_number(name.as_bytes()) {
let name = name.as_bytes().to_vec();
executables.push(name);
}
}
@ -326,15 +352,15 @@ pub fn external_exceptions(engine_state: &EngineState, stack: &Stack) -> Vec<Vec
while let Some(Ok(item)) = contents.next() {
if is_executable::is_executable(&item.path()) {
if let Ok(name) = item.file_name().into_string() {
let name = name.as_bytes().to_vec();
if nu_parser::is_math_expression_like(&name) {
if seems_like_number(name.as_bytes()) {
let name = name.as_bytes().to_vec();
executables.push(name);
}
}
if let Some(name) = item.path().file_stem() {
let name = name.to_string_lossy();
let name = name.as_bytes().to_vec();
if nu_parser::is_math_expression_like(&name) {
if seems_like_number(name.as_bytes()) {
let name = name.as_bytes().to_vec();
executables.push(name);
}
}