7335c0e82b97cbccdce524491bbdf68f096dd019
[packages/precise/mcollective.git] / lib / mcollective / vendor / json / tests / test_json_string_matching.rb
1 #!/usr/bin/env ruby
2 # -*- coding: utf-8 -*-
3
4 require 'test/unit'
5 require File.join(File.dirname(__FILE__), 'setup_variant')
6 require 'stringio'
7 require 'time'
8
9 class 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
39 end