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/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 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 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'