Welcome to mirror list, hosted at ThFree Co, Russian Federation.

json_size_estimator_spec.rb « utils « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba49cc3a84772a01c7a99f107b5ea1db47568c1d (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 'fast_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