1#!/usr/bin/env ruby
2# encoding: utf-8
3
4require 'test/unit'
5require File.join(File.dirname(__FILE__), 'setup_variant')
6require 'stringio'
7require 'time'
8
9class TestJSONStringMatching < Test::Unit::TestCase
10  include JSON
11
12  class TestTime < ::Time
13    def self.json_create(string)
14      Time.parse(string)
15    end
16
17    def to_json(*)
18      %{"#{strftime('%FT%T%z')}"}
19    end
20
21    def ==(other)
22      to_i == other.to_i
23    end
24  end
25
26  def test_match_date
27    t = TestTime.new
28    t_json = [ t ].to_json
29    assert_equal [ t ],
30      JSON.parse(t_json, :create_additions => true,
31        :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/ => TestTime })
32    assert_equal [ t.strftime('%FT%T%z') ],
33      JSON.parse(t_json, :create_additions => true,
34        :match_string => { /\A\d{3}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/ => TestTime })
35    assert_equal [ t.strftime('%FT%T%z') ],
36      JSON.parse(t_json,
37        :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/ => TestTime })
38  end
39end
40