Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time Args support for month string values #2413

Merged
merged 7 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions artichoke-backend/src/extn/core/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub(in crate::extn) mod mruby;
pub mod require;
pub(super) mod trampoline;

pub use trampoline::integer;

#[derive(Debug, Clone, Copy)]
pub struct Kernel;

Expand Down
171 changes: 162 additions & 9 deletions artichoke-backend/src/extn/core/time/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::convert::to_int;
use crate::convert::{to_int, to_str};
use crate::extn::core::kernel::integer;
use crate::extn::prelude::*;

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -57,18 +58,64 @@ impl TryConvertMut<&mut [Value], Args> for Artichoke {
for (i, &arg) in args.iter().enumerate() {
match i {
0 => {
let arg = to_int(self, arg)?;
let arg: i64 = arg.try_convert_into(self)?;
let arg: i64 = to_int(self, arg).and_then(|arg| arg.try_convert_into(self))?;

result.year = i32::try_from(arg).map_err(|_| ArgumentError::with_message("year out of range"))?;
}
1 => {
// TODO: This should support 3 letter month names
// as per the docs. https://ruby-doc.org/3.1.2/Time.html#method-c-new
let arg = to_int(self, arg)?;
let arg: i64 = arg.try_convert_into(self)?;

result.month = match u8::try_from(arg) {
// ```irb
// 3.1.2 => Time.utc(2022, 2).month
// => 2
// 3.1.2 => class I; def to_int; 2; end; end
// => :to_int
// 3.1.2 => Time.utc(2022, I.new).month
// => 2
// 3.1.2 > Time.utc(2022, "feb").month
// => 2
// 3.1.2 > class A; def to_str; "feb"; end; end
// => :to_str
// 3.1.2 > Time.utc(2022, A.new).month
// => 2
// 3.1.2 > class I; def to_str; "2"; end; end
// => :to_str
// 3.1.2 > Time.utc(2022, I.new).month
// => 2
// ```
let month: i64 = if Ruby::Fixnum == arg.ruby_type() {
b-n marked this conversation as resolved.
Show resolved Hide resolved
// Short circuit to avoid string checking
let arg = to_int(self, arg)?;
arg.try_convert_into(self)?
} else {
if let Ok(arg) = to_str(self, arg) {
let mut month_str: Vec<u8> = arg.try_convert_into_mut(self)?;
month_str.make_ascii_lowercase();
match month_str.as_slice() {
b"jan" => Ok(1),
b"feb" => Ok(2),
b"mar" => Ok(3),
b"apr" => Ok(4),
b"may" => Ok(5),
b"jun" => Ok(6),
b"jul" => Ok(7),
b"aug" => Ok(8),
b"sep" => Ok(9),
b"oct" => Ok(10),
b"nov" => Ok(11),
b"dec" => Ok(12),
_ => {
// Delegate to `Kernel#Integer` as last
// resort to handle Integer strings.
let arg = integer(self, arg, None)?;
arg.try_convert_into(self)
}
}
} else {
let arg = to_int(self, arg)?;
arg.try_convert_into(self)
}?
b-n marked this conversation as resolved.
Show resolved Hide resolved
};

result.month = match u8::try_from(month) {
Ok(month @ 1..=12) => Ok(month),
_ => Err(ArgumentError::with_message("mon out of range")),
b-n marked this conversation as resolved.
Show resolved Hide resolved
}?;
Expand Down Expand Up @@ -300,6 +347,112 @@ mod tests {
assert_eq!(0, result.nanoseconds);
}

#[test]
fn month_supports_string_values() {
let mut interp = interpreter();

let table = [
b-n marked this conversation as resolved.
Show resolved Hide resolved
(b"[2022, 'jan']", 1),
(b"[2022, 'feb']", 2),
(b"[2022, 'mar']", 3),
(b"[2022, 'apr']", 4),
(b"[2022, 'may']", 5),
(b"[2022, 'jun']", 6),
(b"[2022, 'jul']", 7),
(b"[2022, 'aug']", 8),
(b"[2022, 'sep']", 9),
(b"[2022, 'oct']", 10),
(b"[2022, 'nov']", 11),
(b"[2022, 'dec']", 12),
];

for (input, expected_month) in table {
let args = interp.eval(input).unwrap();
let mut ary_args: Vec<Value> = interp.try_convert_mut(args).unwrap();
let result: Args = interp.try_convert_mut(ary_args.as_mut_slice()).unwrap();

assert_eq!(expected_month, result.month);
}
}

#[test]
fn month_strings_are_case_insensitive() {
let mut interp = interpreter();

let table = [
(b"[2022, 'Feb']", 2),
(b"[2022, 'fEb']", 2),
(b"[2022, 'feB']", 2),
(b"[2022, 'FEb']", 2),
(b"[2022, 'FeB']", 2),
(b"[2022, 'fEB']", 2),
(b"[2022, 'FEB']", 2),
];

for (input, expected_month) in table {
let args = interp.eval(input).unwrap();
let mut ary_args: Vec<Value> = interp.try_convert_mut(args).unwrap();
let result: Args = interp.try_convert_mut(ary_args.as_mut_slice()).unwrap();

assert_eq!(expected_month, result.month);
b-n marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[test]
fn month_supports_string_like_values() {
let mut interp = interpreter();

let args = interp
.eval(b"class A; def to_str; 'feb'; end; end; [2022, A.new]")
.unwrap();
let mut ary_args: Vec<Value> = interp.try_convert_mut(args).unwrap();
let result: Args = interp.try_convert_mut(ary_args.as_mut_slice()).unwrap();

assert_eq!(2, result.month);
}

#[test]
fn month_supports_int_like_values() {
let mut interp = interpreter();

let args = interp.eval(b"class A; def to_int; 2; end; end; [2022, A.new]").unwrap();
let mut ary_args: Vec<Value> = interp.try_convert_mut(args).unwrap();
let result: Args = interp.try_convert_mut(ary_args.as_mut_slice()).unwrap();

assert_eq!(2, result.month);
}

#[test]
fn month_string_can_be_integer_strings() {
let mut interp = interpreter();

let args = interp
.eval(b"class A; def to_str; '2'; end; end; [2022, A.new]")
.unwrap();
let mut ary_args: Vec<Value> = interp.try_convert_mut(args).unwrap();
let result: Args = interp.try_convert_mut(ary_args.as_mut_slice()).unwrap();

assert_eq!(2, result.month);
}

#[test]
fn invalid_month_string_responds_with_int_conversion_error() {
b-n marked this conversation as resolved.
Show resolved Hide resolved
let mut interp = interpreter();

let args = interp
.eval(b"class A; def to_str; 'aaa'; end; end; [2022, A.new]")
.unwrap();
let mut ary_args: Vec<Value> = interp.try_convert_mut(args).unwrap();
let result: Result<Args, Error> = interp.try_convert_mut(ary_args.as_mut_slice());
let error = result.unwrap_err();

assert_eq!(
error.message().as_bstr(),
br#"invalid value for Integer(): "aaa""#.as_bstr()
);
assert_eq!(error.name(), "ArgumentError");
}

#[test]
fn subsec_is_valid_micros_not_nanos() {
let mut interp = interpreter();
Expand Down