Skip to content

Commit

Permalink
stdlib: Add types for JSON.load_file and .load_file!
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Sep 29, 2024
1 parent 5e334ef commit 36f84b7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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

0 comments on commit 36f84b7

Please sign in to comment.