Sha256: cfc483e6deabe8d374d9558c7fe61c503a105507bdcda25168b4ac9d8dd76926

Contents?: true

Size: 868 Bytes

Versions: 1

Compression:

Stored size: 868 Bytes

Contents

# frozen_string_literal: true

require "rspec/expectations"
require_relative "iso8601/regexp"

RSpec::Matchers.define :be_an_iso8601_string do |precision: nil|
  match do |actual|
    raise ArgumentError, "Cannot specify keyword and chained `precision` argument" if @precision && precision

    return false unless actual.is_a?(String)

    matches = actual.match(RSpec::ISO8601::REGEXP)

    precision ||= @precision
    matches &&
      (precision.nil? || (matches[:usec]&.length || 0) == precision) &&
      (@utc != true || matches[:offset] == "Z")
  end

  description do
    "be an ISO8601#{" UTC" if @utc} string#{" with precision #{precision}" unless precision.nil?}"
  end

  chain :with_precision do |digits|
    @precision = digits
  end

  chain :in_utc do
    @utc = true
  end
end

RSpec::Matchers.alias_matcher :an_iso8601_string, :be_an_iso8601_string

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-iso8601-0.2.0 lib/rspec/iso8601.rb