File: test_bigd.rb

package info (click to toggle)
ruby-oj 2.17.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 968 kB
  • ctags: 1,672
  • sloc: ansic: 10,658; ruby: 6,524; makefile: 2
file content (63 lines) | stat: -rwxr-xr-x 1,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
# encoding: UTF-8

$VERBOSE = true

%w(lib ext test).each do |dir|
  $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
end

require 'rubygems' if RUBY_VERSION.start_with?('1.8.')

#require "minitest/spec"
require "minitest/autorun"
 
require "oj"
 
# Uncomment this line and test_big_decimal will fail
require "active_support/json"
 
# With ActiveSupport 4.0, neither of these settings affect BigDecimal#to_json,
# only BigDecimal#as_json
#
# ActiveSupport.encode_big_decimal_as_string = false
# ActiveSupport::JSON::Encoding.encode_big_decimal_as_string = false
 
describe Oj do
 
  # Options set by default in Rails 4.0 / Rabl
  def options
    {
      :bigdecimal_as_decimal=>true, # default = true
      :use_to_json=>true, # default = false
      :mode=>:compat, # default = object
      :time_format=>:ruby, # default = unix
    }
  end
 
  def test_big_decimal
    orig = BigDecimal.new("3.14159265359")
    puts "*** to_s: #{orig.to_s}"
    puts "*** to_json: #{orig.to_json}"
    puts "*** JSON.dump: #{JSON.dump(orig)}"
    json = Oj.dump(orig, options)
    puts "*** json: #{json}"
 
    value = Oj.load(json)
    puts "*** value: #{value.class}"
    assert_equal(value, orig)
 
    # by default, without active support
    # assert_equal("0.314159265359E1", json)
    # in Rails 4.1, with active support
    # assert_equal("3.14159265359", json)
  end
 
  # Floats are unaffected
  def test_float
    orig = 3.14159265359
    json = Oj.dump(orig, options)
    assert_equal("3.14159265359", json)
  end
 
end