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 some tests #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Gemfile.lock
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'minitest/test_task'

Minitest::TestTask.create

task default: :test
3 changes: 3 additions & 0 deletions lib/ndjson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ def write obj
@output.puts JSON.generate(obj)
end

def flush
@output.flush
end
end
end
7 changes: 5 additions & 2 deletions ndjson.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ Gem::Specification.new do |s|
s.email = '[email protected]'
s.files = ['lib/ndjson.rb']
s.homepage = 'https://github.com/ndjson/ndjson.rb'
s.license = 'MIT'
end
s.license = 'MIT'

s.add_development_dependency "minitest"
s.add_development_dependency "rake"
end
3 changes: 3 additions & 0 deletions test/example.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"some":"thing"}
{"foo":17,"bar":false,"quux":true}
{"may":{"include":"nested","objects":["and","arrays"]}}
48 changes: 48 additions & 0 deletions test/ndjson_test.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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'