summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/utils
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-04 12:07:52 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-04 12:07:52 +0000
commitc6c7437861bff9572747674095c4dfbdfbea4988 (patch)
tree237d1ed922193f19ae326923457344c082003788 /spec/lib/gitlab/utils
parentd80f3cd75e700b6e62910865bfd36734644ffa89 (diff)
downloadgitlab-ce-c6c7437861bff9572747674095c4dfbdfbea4988.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/utils')
-rw-r--r--spec/lib/gitlab/utils/json_size_estimator_spec.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/spec/lib/gitlab/utils/json_size_estimator_spec.rb b/spec/lib/gitlab/utils/json_size_estimator_spec.rb
new file mode 100644
index 00000000000..ae24e25558a
--- /dev/null
+++ b/spec/lib/gitlab/utils/json_size_estimator_spec.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+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