summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/utils/json_size_estimator_spec.rb
blob: 5fd66caa5e935baea75126f7a8d5b3e6553926fd (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Utils::JsonSizeEstimator do
  RSpec::Matchers.define :match_json_bytesize_of do |expected|
    match do |actual|
      actual == expected.to_json.bytesize
    end
  end

  def estimate(object)
    described_class.estimate(object)
  end

  [
    [],
    [[[[]]]],
    [1, "str", 3.14, ["str", { a: -1 }]],
    {},
    { a: {} },
    { a: { b: { c: [1, 2, 3], e: Time.now, f: nil } } },
    { 100 => 500 },
    { '狸' => '狸' },
    nil
  ].each do |example|
    it { expect(estimate(example)).to match_json_bytesize_of(example) }
  end

  it 'calls #to_s on unknown object' do
    klass = Class.new do
      def to_s
        'hello'
      end
    end

    expect(estimate(klass.new)).to match_json_bytesize_of(klass.new.to_s) # "hello"
  end
end