From f135b8792fbbab3ddbdf77c20b211d29bda3743c Mon Sep 17 00:00:00 2001 From: gemmaro Date: Tue, 9 Aug 2022 09:38:47 +0900 Subject: [PATCH 1/3] add NDJSON::Generator#flush for testing --- lib/ndjson.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/ndjson.rb b/lib/ndjson.rb index 6048885..56891e3 100644 --- a/lib/ndjson.rb +++ b/lib/ndjson.rb @@ -34,5 +34,8 @@ def write obj @output.puts JSON.generate(obj) end + def flush + @output.flush + end end end From ce0a3c14c178d7c8ee3a7916c8c841ceac353e34 Mon Sep 17 00:00:00 2001 From: gemmaro Date: Tue, 9 Aug 2022 09:44:01 +0900 Subject: [PATCH 2/3] install Minitest --- .gitignore | 1 + Gemfile | 5 +++++ Rakefile | 7 +++++++ ndjson.gemspec | 7 +++++-- 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Rakefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66f8ed3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..be173b2 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gemspec diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..8664943 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'minitest/test_task' + +Minitest::TestTask.create + +task default: :test diff --git a/ndjson.gemspec b/ndjson.gemspec index 72f1278..0fa5a93 100644 --- a/ndjson.gemspec +++ b/ndjson.gemspec @@ -8,5 +8,8 @@ Gem::Specification.new do |s| s.email = 'derfinn@gmail.com' s.files = ['lib/ndjson.rb'] s.homepage = 'https://github.com/ndjson/ndjson.rb' - s.license = 'MIT' -end \ No newline at end of file + s.license = 'MIT' + + s.add_development_dependency "minitest" + s.add_development_dependency "rake" +end From 5a5d81483a2afc41814a2eb29032bc614d17a32f Mon Sep 17 00:00:00 2001 From: gemmaro Date: Tue, 9 Aug 2022 09:45:52 +0900 Subject: [PATCH 3/3] add tests --- test/example.ndjson | 3 +++ test/ndjson_test.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 8 ++++++++ 3 files changed, 59 insertions(+) create mode 100644 test/example.ndjson create mode 100644 test/ndjson_test.rb create mode 100644 test/test_helper.rb diff --git a/test/example.ndjson b/test/example.ndjson new file mode 100644 index 0000000..85fe14e --- /dev/null +++ b/test/example.ndjson @@ -0,0 +1,3 @@ + {"some":"thing"} + {"foo":17,"bar":false,"quux":true} + {"may":{"include":"nested","objects":["and","arrays"]}} diff --git a/test/ndjson_test.rb b/test/ndjson_test.rb new file mode 100644 index 0000000..67efe40 --- /dev/null +++ b/test/ndjson_test.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require_relative 'test_helper' + +module NDJSONTest + class ParserTest < Minitest::Test + def test_initialize_with_path + parser = NDJSON::Parser.new(File.expand_path('example.ndjson', __dir__)) + index = 0 + parser.each do |entry| + assert_equal({ 'some' => 'thing' }, entry) if index.zero? + index += 1 + end + assert_equal(index, 3) + end + + def test_initialize_with_array + content = File.read(File.expand_path('example.ndjson', __dir__)).lines + parser = NDJSON::Parser.new(content) + index = 0 + parser.each do |entry| + assert_equal({ 'some' => 'thing' }, entry) if index.zero? + index += 1 + end + assert_equal(index, 3) + end + end + + class GeneratorTest < Minitest::Test + def test_initialize_with_path + file = Tempfile.new('example.ndjson') + assert_equal(String, file.path.class) + path = file.path + generator = NDJSON::Generator.new(path) + generator.write({ 'some' => 'thing' }) + generator.flush + assert_equal(%({"some":"thing"}\n), File.read(path)) + end + + def test_initialize_with_stringio + io = StringIO.new + generator = NDJSON::Generator.new(io) + generator.write({ 'some' => 'thing' }) + io.rewind + assert_equal(%({"some":"thing"}\n), io.read) + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..a4c7480 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift File.expand_path('../lib', __dir__) +require 'ndjson' + +require 'tempfile' +require 'stringio' +require 'minitest/autorun'