Skip to content

Commit

Permalink
fixing string operators
Browse files Browse the repository at this point in the history
  • Loading branch information
FCO committed Feb 25, 2024
1 parent c4d13de commit 6680418
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 39 deletions.
244 changes: 244 additions & 0 deletions lib/Red/Operators.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,250 @@ multi infix:<< >= >>(Date $a is readonly, Red::AST $b) is export {
Red::AST::Ge.new: ast-value($a), $b, :cast<date>
}

############################
#| X eq Y # Where Y is castable to DateTime and writable
multi infix:<eq>(Red::AST $a, DateTime $b is rw) is export {
Red::AST::Eq.new: $a, ast-value($b), :bind-right
}

#| X eq Y # Where Y is castable to DateTime and read only
multi infix:<eq>(Red::AST $a, DateTime $b is readonly) is export {
Red::AST::Eq.new: $a, ast-value($b)
}

#| X eq Y # Where X is castable to DateTime and writable
multi infix:<eq>(DateTime $a is rw, Red::AST $b) is export {
Red::AST::Eq.new: ast-value($a), $b, :bind-left
}

#| X eq Y # Where X is castable to DateTime and read only
multi infix:<eq>(DateTime $a is readonly, Red::AST $b) is export {
Red::AST::Eq.new: ast-value($a), $b
}

#| X ne Y # Where Y is castable to DateTime and writable
multi infix:<ne>(Red::AST $a, DateTime $b is rw) is export {
Red::AST::Ne.new: $a, ast-value($b), :bind-right
}

#| X ne Y # Where Y is castable to DateTime and read only
multi infix:<ne>(Red::AST $a, DateTime $b is readonly) is export {
Red::AST::Ne.new: $a, ast-value($b)
}

#| X ne Y # Where X is castable to DateTime and writable
multi infix:<ne>(DateTime $a is rw, Red::AST $b) is export {
Red::AST::Ne.new: ast-value($a), $b, :bind-left
}

#| X ne Y # Where X is castable to DateTime and read only
multi infix:<ne>(DateTime $a is readonly, Red::AST $b) is export {
Red::AST::Ne.new: ast-value($a), $b
}

#| X lt Y # Where Y is castable to DateTime and writable
multi infix:<lt>(Red::AST $a, DateTime $b is rw) is export {
Red::AST::Lt.new: $a, ast-value($b), :bind-right
}

#| X lt Y # Where Y is castable to DateTime and read only
multi infix:<lt>(Red::AST $a, DateTime $b is readonly) is export {
Red::AST::Lt.new: $a, ast-value($b)
}

#| X lt Y # Where X is castable to DateTime and writable
multi infix:<lt>(DateTime $a is rw, Red::AST $b) is export {
Red::AST::Lt.new: ast-value($a), $b, :bind-left
}

#| X lt Y # Where X is castable to DateTime and read only
multi infix:<lt>(DateTime $a is readonly, Red::AST $b) is export {
Red::AST::Lt.new: ast-value($a), $b
}

#| X gt Y # Where Y is castable to DateTime and writable
multi infix:<gt>(Red::AST $a, DateTime $b is rw) is export {
Red::AST::Gt.new: $a, ast-value($b), :bind-right
}

#| X gt Y # Where Y is castable to DateTime and read only
multi infix:<gt>(Red::AST $a, DateTime $b is readonly) is export {
Red::AST::Gt.new: $a, ast-value($b)
}

#| X gt Y # Where X is castable to DateTime and writable
multi infix:<gt>(DateTime $a is rw, Red::AST $b) is export {
Red::AST::Gt.new: ast-value($a), $b, :bind-left
}

#| X gt Y # Where X is castable to DateTime and read only
multi infix:<gt>(DateTime $a is readonly, Red::AST $b) is export {
Red::AST::Gt.new: ast-value($a), $b
}

#| X le Y # Where Y is castable to DateTime and writable
multi infix:<le>(Red::AST $a, DateTime $b is rw) is export {
Red::AST::Le.new: $a, ast-value($b), :bind-right
}

#| X le Y # Where Y is castable to DateTime and read only
multi infix:<le>(Red::AST $a, DateTime $b is readonly) is export {
Red::AST::Le.new: $a, ast-value($b)
}

#| X le Y # Where X is castable to DateTime and writable
multi infix:<le>(DateTime $a is rw, Red::AST $b) is export {
Red::AST::Le.new: ast-value($a), $b, :bind-left
}

#| X le Y # Where X is castable to DateTime and read only
multi infix:<le>(DateTime $a is readonly, Red::AST $b) is export {
Red::AST::Le.new: ast-value($a), $b
}

#| X ge Y # Where Y is castable to DateTime and writable
multi infix:<ge>(Red::AST $a, DateTime $b is rw) is export {
Red::AST::Ge.new: $a, ast-value($b), :bind-right
}

#| X ge Y # Where Y is castable to DateTime and read only
multi infix:<ge>(Red::AST $a, DateTime $b is readonly) is export {
Red::AST::Ge.new: $a, ast-value($b)
}

#| X ge Y # Where X is castable to DateTime and writable
multi infix:<ge>(DateTime $a is rw, Red::AST $b) is export {
Red::AST::Ge.new: ast-value($a), $b, :bind-left
}

#| X ge Y # Where X is castable to DateTime and read only
multi infix:<ge>(DateTime $a is readonly, Red::AST $b) is export {
Red::AST::Ge.new: ast-value($a), $b
}
############################

############################
#| X eq Y # Where Y is castable to Date and writable
multi infix:<eq>(Red::AST $a, Date $b is rw) is export {
Red::AST::Eq.new: $a, ast-value($b), :bind-right
}

#| X eq Y # Where Y is castable to Date and read only
multi infix:<eq>(Red::AST $a, Date $b is readonly) is export {
Red::AST::Eq.new: $a, ast-value($b)
}

#| X eq Y # Where X is castable to Date and writable
multi infix:<eq>(Date $a is rw, Red::AST $b) is export {
Red::AST::Eq.new: ast-value($a), $b, :bind-left
}

#| X eq Y # Where X is castable to Date and read only
multi infix:<eq>(Date $a is readonly, Red::AST $b) is export {
Red::AST::Eq.new: ast-value($a), $b
}

#| X ne Y # Where Y is castable to Date and writable
multi infix:<ne>(Red::AST $a, Date $b is rw) is export {
Red::AST::Ne.new: $a, ast-value($b), :bind-right
}

#| X ne Y # Where Y is castable to Date and read only
multi infix:<ne>(Red::AST $a, Date $b is readonly) is export {
Red::AST::Ne.new: $a, ast-value($b)
}

#| X ne Y # Where X is castable to Date and writable
multi infix:<ne>(Date $a is rw, Red::AST $b) is export {
Red::AST::Ne.new: ast-value($a), $b, :bind-left
}

#| X ne Y # Where X is castable to Date and read only
multi infix:<ne>(Date $a is readonly, Red::AST $b) is export {
Red::AST::Ne.new: ast-value($a), $b
}

#| X lt Y # Where Y is castable to Date and writable
multi infix:<lt>(Red::AST $a, Date $b is rw) is export {
Red::AST::Lt.new: $a, ast-value($b), :bind-right
}

#| X lt Y # Where Y is castable to Date and read only
multi infix:<lt>(Red::AST $a, Date $b is readonly) is export {
Red::AST::Lt.new: $a, ast-value($b)
}

#| X lt Y # Where X is castable to Date and writable
multi infix:<lt>(Date $a is rw, Red::AST $b) is export {
Red::AST::Lt.new: ast-value($a), $b, :bind-left
}

#| X lt Y # Where X is castable to Date and read only
multi infix:<lt>(Date $a is readonly, Red::AST $b) is export {
Red::AST::Lt.new: ast-value($a), $b
}

#| X gt Y # Where Y is castable to Date and writable
multi infix:<gt>(Red::AST $a, Date $b is rw) is export {
Red::AST::Gt.new: $a, ast-value($b), :bind-right
}

#| X gt Y # Where Y is castable to Date and read only
multi infix:<gt>(Red::AST $a, Date $b is readonly) is export {
Red::AST::Gt.new: $a, ast-value($b)
}

#| X gt Y # Where X is castable to Date and writable
multi infix:<gt>(Date $a is rw, Red::AST $b) is export {
Red::AST::Gt.new: ast-value($a), $b, :bind-left
}

#| X gt Y # Where X is castable to Date and read only
multi infix:<gt>(Date $a is readonly, Red::AST $b) is export {
Red::AST::Gt.new: ast-value($a), $b
}

#| X le Y # Where Y is castable to Date and writable
multi infix:<le>(Red::AST $a, Date $b is rw) is export {
Red::AST::Le.new: $a, ast-value($b), :bind-right
}

#| X le Y # Where Y is castable to Date and read only
multi infix:<le>(Red::AST $a, Date $b is readonly) is export {
Red::AST::Le.new: $a, ast-value($b)
}

#| X le Y # Where X is castable to Date and writable
multi infix:<le>(Date $a is rw, Red::AST $b) is export {
Red::AST::Le.new: ast-value($a), $b, :bind-left
}

#| X le Y # Where X is castable to Date and read only
multi infix:<le>(Date $a is readonly, Red::AST $b) is export {
Red::AST::Le.new: ast-value($a), $b
}

#| X ge Y # Where Y is castable to Date and writable
multi infix:<ge>(Red::AST $a, Date $b is rw) is export {
Red::AST::Ge.new: $a, ast-value($b), :bind-right
}

#| X ge Y # Where Y is castable to Date and read only
multi infix:<ge>(Red::AST $a, Date $b is readonly) is export {
Red::AST::Ge.new: $a, ast-value($b)
}

#| X ge Y # Where X is castable to Date and writable
multi infix:<ge>(Date $a is rw, Red::AST $b) is export {
Red::AST::Ge.new: ast-value($a), $b, :bind-left
}

#| X ge Y # Where X is castable to Date and read only
multi infix:<ge>(Date $a is readonly, Red::AST $b) is export {
Red::AST::Ge.new: ast-value($a), $b
}
############################

#| X lt Y
multi infix:<lt>(Red::AST $a, Red::AST $b) is export {
Red::AST::Lt.new: $a, $b, :cast<str>
Expand Down
65 changes: 26 additions & 39 deletions t/80-date-operator.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ my $*RED-DB = database $driver, |%( @conf.map: { do given .split: "=
model Dt {
has Int $.id is serial;
has DateTime $.created is column = DateTime.now;

}

Dt.^create-table(:if-not-exists);
Expand All @@ -26,46 +25,34 @@ Dt.^create(created => $earlier);

my $now = DateTime.now;

lives-ok { is Dt.^all.grep(*.created < $now).elems, 1, "$earlier < $now" };
lives-ok { is Dt.^all.grep(*.created lt $now).elems, 1, "$earlier lt $now" };
lives-ok { is Dt.^all.grep(*.created < $earlier).elems, 0, "$earlier < $earlier" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created lt $earlier).elems, 0, "$earlier lt $earlier" };
lives-ok { is Dt.^all.grep(*.created <= $now).elems, 1, "$earlier <= $now" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created le $now).elems, 1, "$earlier le $now" };
lives-ok { is Dt.^all.grep(*.created <= $earlier).elems, 1, "$earlier <= $earlier" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created le $earlier).elems, 1, "$earlier le $earlier" };
lives-ok { is Dt.^all.grep(*.created < $now).elems, 1, "$earlier < $now" };
lives-ok { is Dt.^all.grep(*.created lt $now).elems, 1, "$earlier lt $now" };
lives-ok { is Dt.^all.grep(*.created < $earlier).elems, 0, "$earlier < $earlier" };
lives-ok { is Dt.^all.grep(*.created <= $now).elems, 1, "$earlier <= $now" };
lives-ok { is Dt.^all.grep(*.created <= $earlier).elems, 1, "$earlier <= $earlier" };
lives-ok { is Dt.^all.grep(*.created <= $even_earlier).elems, 0, "$earlier <= $even_earlier" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created le $even_earlier).elems, 0, "$earlier le $even_earlier" };
lives-ok { is Dt.^all.grep(*.created == $now).elems, 0, "$earlier == $now" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created eq $now).elems, 0, "$earlier eq $now" };
lives-ok { is Dt.^all.grep(*.created == $earlier).elems, 1, "$earlier == $earlier" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created eq $earlier).elems, 1, "$earlier eq $earlier" };
lives-ok { is Dt.^all.grep(*.created != $earlier).elems, 0, "$earlier != $earlier" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created ne $earlier).elems, 0, "$earlier ne $earlier" };
lives-ok { is Dt.^all.grep(*.created != $now).elems, 1, "$earlier != $now" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created ne $now).elems, 1, "$earlier ne $now" };
lives-ok { is Dt.^all.grep(*.created > $now).elems, 0, "$earlier > $now" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created gt $now).elems, 0, "$earlier gt $now" };
lives-ok { is Dt.^all.grep(*.created > $even_earlier).elems, 1, "$earlier > $even_earlier" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created gt $even_earlier).elems, 1, "$earlier gt $even_earlier" };
lives-ok { is Dt.^all.grep(*.created == $now).elems, 0, "$earlier == $now" };
lives-ok { is Dt.^all.grep(*.created == $earlier).elems, 1, "$earlier == $earlier" };
lives-ok { is Dt.^all.grep(*.created != $earlier).elems, 0, "$earlier != $earlier" };
lives-ok { is Dt.^all.grep(*.created != $now).elems, 1, "$earlier != $now" };
lives-ok { is Dt.^all.grep(*.created > $now).elems, 0, "$earlier > $now" };
lives-ok { is Dt.^all.grep(*.created > $even_earlier).elems, 1, "$earlier > $even_earlier" };
lives-ok { is Dt.^all.grep(*.created >= $even_earlier).elems, 1, "$earlier >= $even_earlier" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created >= $earlier).elems, 1, "$earlier >= $earlier" };
lives-ok { is Dt.^all.grep(*.created >= $now).elems, 0, "$earlier >= $now" };

lives-ok { is Dt.^all.grep(*.created ge $now).elems, 0, "$earlier ge $now" };
lives-ok { is Dt.^all.grep(*.created ge $earlier).elems, 1, "$earlier ge $earlier" };
lives-ok { is Dt.^all.grep(*.created ge $even_earlier).elems, 1, "$earlier ge $even_earlier" };
lives-ok { is Dt.^all.grep(*.created >= $earlier).elems, 1, "$earlier >= $earlier" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created ge $earlier).elems, 1, "$earlier ge $earlier" };
lives-ok { is Dt.^all.grep(*.created >= $now).elems, 0, "$earlier >= $now" };
todo "we should define how string operators should work with DateTime";
lives-ok { is Dt.^all.grep(*.created ge $now).elems, 0, "$earlier ge $now" };
lives-ok { is Dt.^all.grep(*.created gt $even_earlier).elems, 1, "$earlier gt $even_earlier" };
lives-ok { is Dt.^all.grep(*.created gt $now).elems, 0, "$earlier gt $now" };
lives-ok { is Dt.^all.grep(*.created ne $now).elems, 1, "$earlier ne $now" };
lives-ok { is Dt.^all.grep(*.created ne $earlier).elems, 0, "$earlier ne $earlier" };
lives-ok { is Dt.^all.grep(*.created eq $earlier).elems, 1, "$earlier eq $earlier" };
lives-ok { is Dt.^all.grep(*.created eq $now).elems, 0, "$earlier eq $now" };
lives-ok { is Dt.^all.grep(*.created le $even_earlier).elems, 0, "$earlier le $even_earlier" };
lives-ok { is Dt.^all.grep(*.created le $earlier).elems, 1, "$earlier le $earlier" };
lives-ok { is Dt.^all.grep(*.created lt $earlier).elems, 0, "$earlier lt $earlier" };
lives-ok { is Dt.^all.grep(*.created le $now).elems, 1, "$earlier le $now" };

done-testing

0 comments on commit 6680418

Please sign in to comment.