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

Add load_file and load_file! methods, with tests. Fixes issue #386. #387

Merged
merged 1 commit into from
Jun 30, 2020
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
10 changes: 10 additions & 0 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ def parse!(source, opts = {})
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
#
Expand Down
56 changes: 56 additions & 0 deletions tests/json_common_interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,60 @@ def test_JSON
assert_equal @json, JSON(@hash)
assert_equal @hash, JSON(@json)
end

def test_load_file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test with options...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that. I've added this (and an equivalent function for load_file!), would it suffice?:

  def test_load_file_with_option
    temp_file_containing(@json) do |filespec|
      key_classes = load_file(filespec, symbolize_names: true).keys.map(&:class)
      assert_true (key_classes.include?(Symbol) && (! key_classes.include?(String)))
    end
  end

Also, in order to get the tests to pass I had to rebase master my feature branch. Is it ok to git push -f this branch?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok to git push -f this branch?

Yes, that's the best thing to do 😄

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