From c494abc78e5a4d48ef84dc9d13772b74ae0d89b0 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sun, 19 May 2024 03:34:15 +0000 Subject: [PATCH] add string.split error tests --- .../compiler/src/builtin/functions/string.rs | 2 +- crates/compiler/src/value/number.rs | 1 + crates/lib/tests/strings.rs | 82 +++++++++++++++++-- 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/crates/compiler/src/builtin/functions/string.rs b/crates/compiler/src/builtin/functions/string.rs index 63641dc..d91ccc2 100644 --- a/crates/compiler/src/builtin/functions/string.rs +++ b/crates/compiler/src/builtin/functions/string.rs @@ -149,7 +149,7 @@ pub(crate) fn str_split(mut args: ArgumentResult, visitor: &mut Visitor) -> Sass let limit_int = limit.assert_int_with_name("limit", args.span())?; if limit_int < 1 { return Err(( - format!("$limit: Must be greater than 1, was {}.", limit_int), + format!("$limit: Must be 1 or greater, was {}.", limit_int), args.span(), ) .into()); diff --git a/crates/compiler/src/value/number.rs b/crates/compiler/src/value/number.rs index 281ae78..0bd9529 100644 --- a/crates/compiler/src/value/number.rs +++ b/crates/compiler/src/value/number.rs @@ -53,6 +53,7 @@ pub(crate) fn fuzzy_as_int(num: f64) -> Option { let rounded = num.round(); if fuzzy_equals(num, rounded) { + // todo: this can oveflow Some(rounded as i64) } else { None diff --git a/crates/lib/tests/strings.rs b/crates/lib/tests/strings.rs index ff4252a..f0c954c 100644 --- a/crates/lib/tests/strings.rs +++ b/crates/lib/tests/strings.rs @@ -311,27 +311,91 @@ test!( test!( str_split_abc_space, "@use 'sass:string'; - foo { - bar: string.split('a b c', ' '); + a { + color: string.split('a b c', ' '); } ", - "foo {\n bar: [\"a\", \"b\", \"c\"];\n}\n" + "a {\n color: [\"a\", \"b\", \"c\"];\n}\n" ); test!( str_split_abc_space_1, "@use 'sass:string'; - foo { - bar: string.split('a b c', ' ', 1); + a { + color: string.split('a b c', ' ', 1); } ", - "foo {\n bar: [\"a\", \"b c\"];\n}\n" + "a {\n color: [\"a\", \"b c\"];\n}\n" ); test!( str_split_rgb_comma, "@use 'sass:string'; - foo { - bar: string.split('red,green,blue', ','); + a { + color: string.split('red,green,blue', ','); } ", - "foo {\n bar: [\"red\", \"green\", \"blue\"];\n}\n" + "a {\n color: [\"red\", \"green\", \"blue\"];\n}\n" +); +test!( + str_split_big_limit, + "@use 'sass:string'; + a { + color: string.split('red,green,blue', ',', 9223372036854775808); + } + ", + "a {\n color: [\"red\", \"green\", \"blue\"];\n}\n" +); +error!( + str_split_negative_limit, + "@use 'sass:string'; + a { + color: string.split('red,green,blue', ',', -1); + } + ", + "Error: $limit: Must be 1 or greater, was -1." +); +error!( + str_split_zero_limit, + "@use 'sass:string'; + a { + color: string.split('red,green,blue', ',', 0); + } + ", + "Error: $limit: Must be 1 or greater, was 0." +); +error!( + str_split_string_limit, + "@use 'sass:string'; + a { + color: string.split('red,green,blue', ',', '0'); + } + ", + "Error: $limit: \"0\" is not a number." +); +error!( + str_split_first_arg_not_string, + "@use 'sass:string'; + a { + color: string.split(1, ','); + } + ", + "Error: $string: 1 is not a string." +); +error!( + str_split_second_arg_not_string, + "@use 'sass:string'; + a { + color: string.split('1', 2); + } + ", + "Error: $separator: 2 is not a string." +); +error!( + #[ignore = "overflow issue"] + str_split_limit_above_i64_max, + "@use 'sass:string'; + a { + color: string.split('1', '1', 36893488147419103232); + } + ", + "Error: $limit: 36893488147419103000 is not an int." );