Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Adding _Date, _Date?, _Time, and _Time? to Literal::Types (#277)

Resolves #276

authored by

Alberto Colón Viera and committed by
GitHub
16162ba5 70c02c54

+67
+26
lib/literal/types.rb
··· 103 103 ) 104 104 end 105 105 106 + # Matches if the value is a `Date` and matches the given constraints. 107 + # If you don't need any constraints, use `Date` instead of `_Date`. 108 + def _Date(...) 109 + _Constraint(Date, ...) 110 + end 111 + 112 + # Nilable version of `_Date` 113 + def _Date?(...) 114 + _Nilable( 115 + _Date(...) 116 + ) 117 + end 118 + 106 119 def _Deferred(...) 107 120 DeferredType.new(...) 108 121 end ··· 321 334 def _Symbol?(...) 322 335 _Nilable( 323 336 _Symbol(...) 337 + ) 338 + end 339 + 340 + # Matches if the value is a `Time` and matches the given constraints. 341 + # If you don't need any constraints, use `Time` instead of `_Time`. 342 + def _Time(...) 343 + _Constraint(Time, ...) 344 + end 345 + 346 + # Nilable version of `_Time` 347 + def _Time?(...) 348 + _Nilable( 349 + _Time(...) 324 350 ) 325 351 end 326 352
+41
test/types.test.rb
··· 186 186 assert _Constraint(Array) >= _Constraint(Array, Enumerable) 187 187 end 188 188 189 + test "_Date" do 190 + assert _Date(_Interface(:to_date)) === Date.today 191 + assert _Date(Date.today) === Date.today 192 + assert _Date((Date.today)..) === Date.today + 1 193 + 194 + refute _Date(_Interface(:non_existing_method)) === Date.today 195 + refute _Date(Date.today) === "2025-01-13" 196 + refute _Date(Date.today) === nil 197 + refute _Date((Date.today)..) === Date.today - 1 198 + 199 + assert _Date(year: 2025) === Date.new(2025, 1, 13) 200 + refute _Date(year: 2025) === Date.new(2024, 1, 13) 201 + 202 + assert _Date(DateTime, _Interface(:to_datetime)) === DateTime.now 203 + assert _Date(DateTime, year: 2025) === DateTime.new(2025, 1, 13) 204 + refute _Date(DateTime, year: 2025) === Date.new(2025, 1, 13) 205 + end 206 + 189 207 test "_Descendant" do 190 208 assert _Descendant(Enumerable) === Array 191 209 assert _Descendant(Enumerable) === Set ··· 602 620 603 621 refute _Symbol(_Interface(:non_existing_method)) === :symbol 604 622 refute _Symbol(size: 5) === :symbol 623 + end 624 + 625 + test "_Time" do 626 + current_time = Time.now 627 + 628 + assert _Time (_Interface(:to_time)) === current_time 629 + assert _Time(current_time) === current_time 630 + assert _Time(current_time..) === current_time + 60 631 + 632 + refute _Time(_Interface(:non_existing_method)) === current_time 633 + refute _Time(current_time..) === current_time - 60 634 + 635 + assert _Time(zone: "UTC") === Time.new(2025, 1, 13, 20, 0, 0, "UTC") 636 + 637 + fifteen_mins_ago = proc { |t| ((Time.now - (60 * 15))..(Time.now)) === t } 638 + assert _Time(fifteen_mins_ago) === Time.now - (60 * 10) 639 + refute _Time(fifteen_mins_ago) === Time.now - (60 * 20) 640 + occurs_on_monday = proc(&:monday?) 641 + assert _Time(occurs_on_monday) === Time.new(2025, 1, 13) 642 + refute _Time(occurs_on_monday) === Time.new(2025, 1, 14) 643 + 644 + refute _Time(Time.new(2025, 1, 13, 20, 0, 0, "UTC")) === "2025-01-13 20:00:00 UTC" 645 + refute _Time(Time.new(2025, 1, 13, 20, 0, 0, "UTC")) === nil 605 646 end 606 647 607 648 test "_Truthy" do