From 1fb144b145043e1886aa0a665797184f73b87832 Mon Sep 17 00:00:00 2001 From: Burak Date: Fri, 22 Dec 2023 17:31:57 +0000 Subject: [PATCH] Add tests for builtin `string::format_int` method (#65) --- src/builtins/strings.rs | 38 +++---- .../cases/builtins/strings/format_int.yaml | 101 ++++++++++++++++++ 2 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 tests/interpreter/cases/builtins/strings/format_int.yaml diff --git a/src/builtins/strings.rs b/src/builtins/strings.rs index dac7c33c..cb37f145 100644 --- a/src/builtins/strings.rs +++ b/src/builtins/strings.rs @@ -66,17 +66,7 @@ fn endswith(span: &Span, params: &[Ref], args: &[Value], _strict: bool) -> Ok(Value::Bool(s1.ends_with(s2.as_ref()))) } -fn format_number(n: &Number, base: u64) -> String { - match base { - 2 => n.format_bin(), - 8 => n.format_octal(), - 10 => n.format_decimal(), - 16 => n.format_hex(), - _ => "".to_owned(), - } -} - -fn format_int(span: &Span, params: &[Ref], args: &[Value], _strict: bool) -> Result { +fn format_int(span: &Span, params: &[Ref], args: &[Value], strict: bool) -> Result { let name = "format_int"; ensure_args_count(span, name, params, args, 2)?; let mut n = ensure_numeric(name, ¶ms[0], &args[0])?; @@ -87,14 +77,24 @@ fn format_int(span: &Span, params: &[Ref], args: &[Value], _strict: bool) } let n = n.floor(); - Ok(Value::String( - (sign.to_owned() - + &match ensure_numeric(name, ¶ms[1], &args[1])?.as_u64() { - Some(b) => format_number(&n, b), - _ => return Ok(Value::Undefined), - }) - .into(), - )) + let base = ensure_numeric(name, ¶ms[1], &args[1])?; + + let num = match base.as_u64() { + Some(2) => n.format_bin(), + Some(8) => n.format_octal(), + Some(10) => n.format_decimal(), + Some(16) => n.format_hex(), + _ => { + if strict { + let span = params[1].span(); + bail!(span.error(&format!("`{name}` expects base to be one of 2, 8, 10, 16"))); + } + + return Ok(Value::Undefined); + } + }; + + Ok(Value::String((sign.to_owned() + &num).into())) } fn indexof(span: &Span, params: &[Ref], args: &[Value], _strict: bool) -> Result { diff --git a/tests/interpreter/cases/builtins/strings/format_int.yaml b/tests/interpreter/cases/builtins/strings/format_int.yaml new file mode 100644 index 00000000..19b55424 --- /dev/null +++ b/tests/interpreter/cases/builtins/strings/format_int.yaml @@ -0,0 +1,101 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +cases: + - note: bases + data: {} + modules: + - | + package test + + binary := format_int(10, 2) + octal := format_int(10, 8) + decimal := format_int(10, 10) + hex := format_int(10, 16) + query: data.test + want_result: + binary: "1010" + octal: "12" + decimal: "10" + hex: "a" + + - note: bases-with-floats + data: {} + modules: + - | + package test + + binary := format_int(10.2, 2) + octal := format_int(10.7, 8) + decimal := format_int(10.325436, 10) + hex := format_int(10.0, 16) + query: data.test + want_result: + binary: "1010" + octal: "12" + decimal: "10" + hex: "a" + + - note: bases-with-negative-values + data: {} + modules: + - | + package test + + binary := format_int(-10, 2) + octal := format_int(-10, 8) + decimal := format_int(-10, 10) + hex := format_int(-10, 16) + query: data.test + want_result: + binary: "-1010" + octal: "-12" + decimal: "-10" + hex: "-a" + + - note: bases-with-negative-floats + data: {} + modules: + - | + package test + + binary := format_int(-10.765, 2) + octal := format_int(-10.0, 8) + decimal := format_int(-10.999999999, 10) + hex := format_int(-10.00, 16) + query: data.test + want_result: + binary: "-1010" + octal: "-12" + decimal: "-10" + hex: "-a" + + - note: invalid-num-type + data: {} + modules: + - | + package test + + x := format_int("10", 2) + query: data.test + error: '`format_int` expects numeric argument. Got `"10"` instead' + + - note: invalid-base + data: {} + modules: + - | + package test + + x := format_int(10, 4) + query: data.test + error: "`format_int` expects base to be one of 2, 8, 10, 16" + + - note: invalid-base-type + data: {} + modules: + - | + package test + + x := format_int(10, "2") + query: data.test + error: '`format_int` expects numeric argument. Got `"2"` instead'