Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdlib: Add types for JSON.load_file and .load_file! #2031

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions stdlib/json/0/json.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,28 @@ module JSON
#
def self?.load: (string | _JsonReadableIO | _JsonRead source, ?Proc proc, ?json_options options) -> untyped

# <!--
# rdoc-file=ext/json/lib/json/common.rb
# - JSON.load_file(path, opts={}) -> object
# -->
# Calls:
# parse(File.read(path), opts)
#
# See method #parse.
#
def self?.load_file: (string path, ?json_options opts) -> untyped

# <!--
# rdoc-file=ext/json/lib/json/common.rb
# - JSON.load_file!(path, opts = {})
# -->
# Calls:
# JSON.parse!(File.read(path, opts))
#
# See method #parse!
#
def self?.load_file!: (string path, ?json_options opts) -> untyped

# <!-- rdoc-file=ext/json/lib/json/common.rb -->
# Sets or returns default options for the JSON.load method. Initially:
# opts = JSON.load_default_options
Expand Down
21 changes: 21 additions & 0 deletions test/stdlib/json/JSON_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative "../test_helper"
require "json"
require "tempfile"

class JsonToStr
def initialize(value = "")
Expand Down Expand Up @@ -108,6 +109,26 @@ def test_load
assert_send_type "(String, Proc, Hash[untyped, untyped]) -> 42", JSON, :load, "42", proc { }, { alllow_nan: true }
end

def test_load_file
Tempfile.create("json") do |f|
f.write '{}'
f.close

assert_send_type "(String) -> untyped", JSON, :load_file, f.path
assert_send_type "(String, Hash[untyped, untyped]) -> untyped", JSON, :load_file, f.path, { allow_nan: true }
end
end

def test_load_file!
Tempfile.create("json") do |f|
f.write '{}'
f.close

assert_send_type "(String) -> untyped", JSON, :load_file!, f.path
assert_send_type "(String, Hash[untyped, untyped]) -> untyped", JSON, :load_file!, f.path, { allow_nan: true }
end
end

def test_load_default_options
assert_send_type "() -> Hash[untyped, untyped]", JSON, :load_default_options
end
Expand Down