From 36f84b7f67001f065b2ca96e47d6d9de1cd2aea9 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 30 Sep 2024 01:25:48 +0900 Subject: [PATCH] stdlib: Add types for JSON.load_file and .load_file! refs: * https://rubydoc.info/gems/json/JSON.load_file * https://github.com/ruby/ruby/blob/master/ext/json/lib/json/common.rb#L242-L262 * https://github.com/ruby/ruby/pull/3581 * https://github.com/flori/json/blob/v2.7.2/lib/json/common.rb#L242-L262 * https://github.com/flori/json/pull/387 --- stdlib/json/0/json.rbs | 22 ++++++++++++++++++++++ test/stdlib/json/JSON_test.rb | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/stdlib/json/0/json.rbs b/stdlib/json/0/json.rbs index 8a801b027..e9cd40540 100644 --- a/stdlib/json/0/json.rbs +++ b/stdlib/json/0/json.rbs @@ -980,6 +980,28 @@ module JSON # def self?.load: (string | _JsonReadableIO | _JsonRead source, ?Proc proc, ?json_options options) -> untyped + # + # Calls: + # parse(File.read(path), opts) + # + # See method #parse. + # + def self?.load_file: (string path, ?json_options opts) -> untyped + + # + # Calls: + # JSON.parse!(File.read(path, opts)) + # + # See method #parse! + # + def self?.load_file!: (string path, ?json_options opts) -> untyped + # # Sets or returns default options for the JSON.load method. Initially: # opts = JSON.load_default_options diff --git a/test/stdlib/json/JSON_test.rb b/test/stdlib/json/JSON_test.rb index f76c56cfe..4af4c2f91 100644 --- a/test/stdlib/json/JSON_test.rb +++ b/test/stdlib/json/JSON_test.rb @@ -1,5 +1,6 @@ require_relative "../test_helper" require "json" +require "tempfile" class JsonToStr def initialize(value = "") @@ -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