[前][次][番号順一覧][スレッド一覧]

ruby-changes:63108

From: Keith <ko1@a...>
Date: Fri, 25 Sep 2020 17:29:01 +0900 (JST)
Subject: [ruby-changes:63108] c3614877d2 (master): [flori/json] Add `load_file` and `load_file!` methods, with tests. Fixes issue #386.

https://git.ruby-lang.org/ruby.git/commit/?id=c3614877d2

From c3614877d205e716bc94ad521918ad57c12ed445 Mon Sep 17 00:00:00 2001
From: Keith Bennett <keithrbennett@g...>
Date: Tue, 30 Jun 2020 14:07:23 -0400
Subject: [flori/json]     Add `load_file` and `load_file!` methods, with
 tests. Fixes issue #386.

https://github.com/flori/json/commit/0be363c99b

diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb
index 8e45b51..bf2c599 100644
--- a/ext/json/lib/json/common.rb
+++ b/ext/json/lib/json/common.rb
@@ -282,6 +282,16 @@ module JSON https://github.com/ruby/ruby/blob/trunk/ext/json/lib/json/common.rb#L282
     Parser.new(source, **(opts||{})).parse
   end
 
+  # Parses the content of a file (see parse method documentation for more information).
+  def load_file(filespec, opts = {})
+    parse(File.read(filespec), opts)
+  end
+
+  # Parses the content of a file (see parse! method documentation for more information).
+  def load_file!(filespec, opts = {})
+    parse!(File.read(filespec), opts)
+  end
+
   # :call-seq:
   #   JSON.generate(obj, opts = nil) -> new_string
   #
diff --git a/test/json/json_common_interface_test.rb b/test/json/json_common_interface_test.rb
index 53f335e..4fdc2b1 100644
--- a/test/json/json_common_interface_test.rb
+++ b/test/json/json_common_interface_test.rb
@@ -123,4 +123,60 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/json/json_common_interface_test.rb#L123
     assert_equal @json, JSON(@hash)
     assert_equal @hash, JSON(@json)
   end
+
+  def test_load_file
+    test_load_shared(:load_file)
+  end
+
+  def test_load_file!
+    test_load_shared(:load_file!)
+  end
+
+  def test_load_file_with_option
+    test_load_file_with_option_shared(:load_file)
+  end
+
+  def test_load_file_with_option!
+    test_load_file_with_option_shared(:load_file!)
+  end
+
+  private
+
+  def test_load_shared(method_name)
+    temp_file_containing(@json) do |filespec|
+      assert_equal JSON.public_send(method_name, filespec), @hash
+    end
+  end
+
+  def test_load_file_with_option_shared(method_name)
+    temp_file_containing(@json) do |filespec|
+      parsed_object = JSON.public_send(method_name, filespec, symbolize_names: true)
+      key_classes = parsed_object.keys.map(&:class)
+      assert_true key_classes.include?(Symbol) && (! key_classes.include?(String))
+    end
+  end
+
+  # Copied and slightly modified from https://github.com/keithrbennett/trick_bag
+  # (https://github.com/keithrbennett/trick_bag/blob/master/lib/trick_bag/io/temp_files.rb).
+  #
+  # For the easy creation and deletion of a temp file populated with text,
+  # wrapped around the code block you provide.
+  #
+  # @param text the text to write to the temporary file
+  # @param file_prefix optional prefix for the temporary file's name
+  # @yield filespec of the temporary file
+  def temp_file_containing(text, file_prefix = '')
+    raise "This method must be called with a code block." unless block_given?
+
+    filespec = nil
+    begin
+      Tempfile.open(file_prefix) do |file|
+        file << text
+        filespec = file.path
+      end
+      yield(filespec)
+    ensure
+      File.delete(filespec) if filespec && File.exist?(filespec)
+    end
+    end
 end
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]