Skip to content
Browse files

minitest

  • Loading branch information...
1 parent 8b1ca2d commit 040b0d21cead59b54fb4c47f20a4a1c7a4afd053 Josh Hull committed
Showing with 123 additions and 130 deletions.
  1. +18 −16 Rakefile
  2. +4 −2 jsonpath.gemspec
  3. +0 −110 spec/jsonpath_spec.rb
  4. +0 −2 spec/spec_helper.rb
  5. +101 −0 test/test_jsonpath.rb
View
34 Rakefile
@@ -1,16 +1,18 @@
-require 'bundler'
-Bundler::GemHelper.install_tasks
-
-require 'rake/rdoctask'
-desc "Generate documentation"
-Rake::RDocTask.new do |rd|
- rd.main = "README.rdoc"
- rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
- rd.rdoc_dir = 'rdoc'
-end
-
-require 'rspec/core/rake_task'
-RSpec::Core::RakeTask.new(:spec)
-
-task :default => :spec
-task :test => :spec
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rake/rdoctask'
+desc "Generate documentation"
+Rake::RDocTask.new do |rd|
+ rd.main = "README.rdoc"
+ rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
+ rd.rdoc_dir = 'rdoc'
+end
+
+task :test do
+ $: << 'lib'
+ require 'minitest/autorun'
+ require 'phocus'
+ require 'jsonpath'
+ Dir['./test/**/test_*.rb'].each { |test| require test }
+end
View
6 jsonpath.gemspec
@@ -18,12 +18,14 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.7}
s.test_files = `git ls-files`.split("\n").select{|f| f =~ /^spec/}
s.rubyforge_project = 'jsonpath'
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
# dependencies
s.add_runtime_dependency 'json'
s.add_development_dependency 'code_stats'
s.add_development_dependency 'rake'
- s.add_development_dependency 'rspec', '~> 2.5.0'
- s.add_development_dependency 'bundler', '~> 1.0.0'
+ s.add_development_dependency 'minitest', '~> 2.2.0'
+ s.add_development_dependency 'phocus'
+ s.add_development_dependency 'bundler', '~> 1.0.10'
end
View
110 spec/jsonpath_spec.rb
@@ -1,110 +0,0 @@
-require 'spec_helper'
-
-describe "JsonPath" do
-
- before(:each) do
- @object = { "store"=> {
- "book" => [
- { "category"=> "reference",
- "author"=> "Nigel Rees",
- "title"=> "Sayings of the Century",
- "price"=> 8.95
- },
- { "category"=> "fiction",
- "author"=> "Evelyn Waugh",
- "title"=> "Sword of Honour",
- "price"=> 12.99
- },
- { "category"=> "fiction",
- "author"=> "Herman Melville",
- "title"=> "Moby Dick",
- "isbn"=> "0-553-21311-3",
- "price"=> 8.99
- },
- { "category"=> "fiction",
- "author"=> "J. R. R. Tolkien",
- "title"=> "The Lord of the Rings",
- "isbn"=> "0-395-19395-8",
- "price"=> 22.99
- }
- ],
- "bicycle"=> {
- "color"=> "red",
- "price"=> 19.95,
- "catalogue_number" => 12345,
- },
- "under_score" => "hello"
- }
- }
- end
-
- it "should lookup a direct path" do
- JsonPath.new('$.store.*').on(@object).to_a.first['book'].size.should == 4
- end
-
- it "should retrieve all authors" do
- JsonPath.new('$..author').on(@object).to_a.should == [
- @object['store']['book'][0]['author'],
- @object['store']['book'][1]['author'],
- @object['store']['book'][2]['author'],
- @object['store']['book'][3]['author']
- ]
- end
-
- it "should retrieve all prices" do
- JsonPath.new('$..price').on(@object).to_a.sort.should == [
- @object['store']['bicycle']['price'],
- @object['store']['book'][0]['price'],
- @object['store']['book'][1]['price'],
- @object['store']['book'][2]['price'],
- @object['store']['book'][3]['price']
- ].sort
- end
-
- it "should recognize all types of array splices" do
- JsonPath.new('$..book[0:1:1]').on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][1]]
- JsonPath.new('$..book[1::2]').on(@object).to_a.should == [@object['store']['book'][1], @object['store']['book'][3]]
- JsonPath.new('$..book[::2]').on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][2]]
- JsonPath.new('$..book[:-2:2]').on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][2]]
- JsonPath.new('$..book[2::]').on(@object).to_a.should == [@object['store']['book'][2], @object['store']['book'][3]]
- end
-
- it "should recognize array comma syntax" do
- JsonPath.new('$..book[0,1]').on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][1]]
- JsonPath.new('$..book[2,-1::]').on(@object).to_a.should == [@object['store']['book'][2], @object['store']['book'][3]]
- end
-
- it "should support filters" do
- JsonPath.new("$..book[?(@['isbn'])]").on(@object).to_a.should == [@object['store']['book'][2], @object['store']['book'][3]]
- JsonPath.new("$..book[?(@['price'] < 10)]").on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][2]]
- end
-
- it "should recognize paths with underscores" do
- JsonPath.new('$.store.bicycle.catalogue_number').on(@object).to_a.should == [@object['store']['bicycle']['catalogue_number']]
- end
-
- it "should support eval'd array indicies" do
- JsonPath.new('$..book[(@.length-2)]').on(@object).to_a.should == [@object['store']['book'][2]]
- end
-
- it "should support first" do
- JsonPath.new('$..book[(@.length-2)]').first(@object) == @object['store']['book'][2]
- end
-
- it "should correct retrieve the right number of all nodes" do
- JsonPath.new('$..*').on(@object).to_a.size.should == 30
- end
-
- it "should correct retrieve the right number of all nodes" do
- JsonPath.new('$..under_score').first(@object).should == 'hello'
- end
-
- it "should deal with a space in the key name" do
- JsonPath.new("$.'c d'").on({"a" => "a","b" => "b", "c d" => "e"}).to_a.should == ['e']
- end
-
- it "should support a convenience class method" do
- JsonPath.on(@object, '$..author').to_a.should == JsonPath.new('$..author').on(@object).to_a
- end
-
-end
View
2 spec/spec_helper.rb
@@ -1,2 +0,0 @@
-$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
-require 'jsonpath'
View
101 test/test_jsonpath.rb
@@ -0,0 +1,101 @@
+class TestJsonpath < MiniTest::Unit::TestCase
+
+ def setup
+ @object = { "store"=> {
+ "book" => [
+ { "category"=> "reference",
+ "author"=> "Nigel Rees",
+ "title"=> "Sayings of the Century",
+ "price"=> 8.95
+ },
+ { "category"=> "fiction",
+ "author"=> "Evelyn Waugh",
+ "title"=> "Sword of Honour",
+ "price"=> 12.99
+ },
+ { "category"=> "fiction",
+ "author"=> "Herman Melville",
+ "title"=> "Moby Dick",
+ "isbn"=> "0-553-21311-3",
+ "price"=> 8.99
+ },
+ { "category"=> "fiction",
+ "author"=> "J. R. R. Tolkien",
+ "title"=> "The Lord of the Rings",
+ "isbn"=> "0-395-19395-8",
+ "price"=> 22.99
+ }
+ ],
+ "bicycle"=> {
+ "color"=> "red",
+ "price"=> 19.95,
+ "catalogue_number" => 12345,
+ } } }
+ end
+
+ def test_lookup_direct_path
+ assert_equal 4, JsonPath.new('$.store.*').on(@object).to_a.first['book'].size
+ end
+
+ def test_retrieve_all_authors
+ assert_equal [
+ @object['store']['book'][0]['author'],
+ @object['store']['book'][1]['author'],
+ @object['store']['book'][2]['author'],
+ @object['store']['book'][3]['author']
+ ], JsonPath.new('$..author').on(@object)
+ end
+
+ def test_retrieve_all_prices
+ assert_equal [
+ @object['store']['bicycle']['price'],
+ @object['store']['book'][0]['price'],
+ @object['store']['book'][1]['price'],
+ @object['store']['book'][2]['price'],
+ @object['store']['book'][3]['price']
+ ].sort, JsonPath.new('$..price').on(@object).sort
+ end
+
+ def test_recognize_array_splices
+ assert_equal [@object['store']['book'][0], @object['store']['book'][1]], JsonPath.new('$..book[0:1:1]').on(@object)
+ assert_equal [@object['store']['book'][1], @object['store']['book'][3]], JsonPath.new('$..book[1::2]').on(@object)
+ assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new('$..book[::2]').on(@object)
+ assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new('$..book[:-2:2]').on(@object)
+ assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[2::]').on(@object)
+ end
+
+ def test_recognize_array_comma
+ assert_equal [@object['store']['book'][0], @object['store']['book'][1]], JsonPath.new('$..book[0,1]').on(@object)
+ assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[2,-1::]').on(@object)
+ end
+
+ def test_recognize_filters
+ assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new("$..book[?(@['isbn'])]").on(@object)
+ assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] < 10)]").on(@object)
+ end
+
+ def test_paths_with_underscores
+ assert_equal [@object['store']['bicycle']['catalogue_number']], JsonPath.new('$.store.bicycle.catalogue_number').on(@object)
+ end
+
+ def test_recognize_array_with_evald_index
+ assert_equal [@object['store']['book'][2]], JsonPath.new('$..book[(@.length-2)]').on(@object)
+ end
+
+ def test_use_first
+ assert_equal @object['store']['book'][2], JsonPath.new('$..book[(@.length-2)]').first(@object)
+ end
+
+ def test_counting
+ assert_equal 29, JsonPath.new('$..*').on(@object).to_a.size
+ end
+
+ def test_space_in_path
+ assert_equal ['e'], JsonPath.new("$.'c d'").on({"a" => "a","b" => "b", "c d" => "e"})
+ end
+
+ def test_class_method
+ assert_equal JsonPath.new('$..author').on(@object), JsonPath.on(@object, '$..author')
+ end
+
+end

0 comments on commit 040b0d2

Please sign in to comment.
Something went wrong with that request. Please try again.