|
| 1 | +# -*- coding:binary -*- |
| 2 | +require 'spec_helper' |
| 3 | + |
| 4 | +require 'rex/time' |
| 5 | + |
| 6 | +describe Rex::ExtTime do |
| 7 | + |
| 8 | + let(:conversions) do |
| 9 | + { |
| 10 | + 0 => '0 secs', |
| 11 | + 1 => '1 sec', |
| 12 | + 60 => '1 min', |
| 13 | + 61 => '1 min 1 sec', |
| 14 | + 121 => '2 mins 1 sec', |
| 15 | + 3600 => '1 hour', |
| 16 | + 3660 => '1 hour 1 min', |
| 17 | + 3661 => '1 hour 1 min 1 sec', |
| 18 | + 7326 => '2 hours 2 mins 6 secs', |
| 19 | + 86400 => '1 day', |
| 20 | + 86401 => '1 day 1 sec', |
| 21 | + 86460 => '1 day 1 min', |
| 22 | + 86461 => '1 day 1 min 1 sec', |
| 23 | + 90000 => '1 day 1 hour', |
| 24 | + 90060 => '1 day 1 hour 1 min', |
| 25 | + 90125 => '1 day 1 hour 2 mins 5 secs', |
| 26 | + 31536000 => '1 year', |
| 27 | + 31536003 => '1 year 3 secs', |
| 28 | + 31536063 => '1 year 1 min 3 secs', |
| 29 | + 31539600 => '1 year 1 hour', |
| 30 | + 31622400 => '1 year 1 day', |
| 31 | + 31626000 => '1 year 1 day 1 hour', |
| 32 | + 31626001 => '1 year 1 day 1 hour 1 sec', |
| 33 | + 31626060 => '1 year 1 day 1 hour 1 min', |
| 34 | + 31626061 => '1 year 1 day 1 hour 1 min 1 sec' |
| 35 | + } |
| 36 | + end |
| 37 | + |
| 38 | + subject { described_class } |
| 39 | + |
| 40 | + describe ".sec_to_s" do |
| 41 | + it "returns string encoded seconds" do |
| 42 | + conversions.each do |k, v| |
| 43 | + expect(subject.sec_to_s(k)).to eq(v) |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + describe ".str_to_sec" do |
| 49 | + it "returns seconds from encoded string" do |
| 50 | + conversions.each do |k, v| |
| 51 | + expect(subject.str_to_sec(v)).to eq(k) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context "when invalid encoded string" do |
| 56 | + let(:invalid) { 'invalid' } |
| 57 | + it "returns 0" do |
| 58 | + expect(subject.str_to_sec(invalid)).to eq(0) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context "when incorrect pluralization" do |
| 63 | + let(:invalid) { '1 years 1 days 2 hour 1 min 1 secs' } |
| 64 | + it "returns correct number of seconds" do |
| 65 | + expect(subject.str_to_sec(invalid)).to eq(31629661) |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments