Skip to content

Commit accc6eb

Browse files
committed
replace rspec with minitest
1 parent c46d208 commit accc6eb

File tree

7 files changed

+92
-184
lines changed

7 files changed

+92
-184
lines changed

Rakefile

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
require "bundler/gem_tasks"
2-
require "rake/extensiontask"
3-
require 'rspec/core/rake_task'
1+
require 'bundler/gem_tasks'
2+
require 'rake/extensiontask'
3+
require 'rake/testtask'
44

5-
RSpec::Core::RakeTask.new(:spec)
6-
Rake::ExtensionTask.new("jaro_winkler") do |ext|
7-
ext.lib_dir = "lib/jaro_winkler"
8-
end
9-
10-
task default: [:compile, :spec]
5+
task default: [:compile, :test]
116

127
task benchmark: %w[benchmark:native benchmark:pure]
138

@@ -52,4 +47,14 @@ task compare: :compile do
5247
end
5348
end
5449
table.each{|row| puts row.join(' | ')}
50+
end
51+
52+
Rake::ExtensionTask.new('jaro_winkler') do |ext|
53+
ext.lib_dir = 'lib/jaro_winkler'
54+
end
55+
56+
Rake::TestTask.new do |t|
57+
t.libs << 'test'
58+
t.test_files = FileList['test/**/test_*.rb']
59+
t.verbose = true
5560
end

ext/jaro_winkler/jaro_winkler.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#include "ruby.h"
22
#include "jaro.h"
33

4-
VALUE rb_mJaroWinkler;
4+
VALUE rb_mJaroWinkler,
5+
rb_eError,
6+
rb_eInvalidWeightError;
7+
58
VALUE distance(int argc, VALUE *argv, VALUE self);
69

710
void Init_jaro_winkler(void){
811
rb_mJaroWinkler = rb_define_module("JaroWinkler");
12+
rb_eError = rb_define_class_under(rb_mJaroWinkler, "Error", rb_eRuntimeError);
13+
rb_eInvalidWeightError = rb_define_class_under(rb_mJaroWinkler, "InvalidWeightError", rb_eError);
914
rb_define_module_function(rb_mJaroWinkler, "c_distance", distance, -1);
1015
}
1116

@@ -19,7 +24,7 @@ VALUE distance(int argc, VALUE *argv, VALUE self){
1924
ignore_case = rb_hash_aref(opt, ID2SYM(rb_intern("ignore_case"))),
2025
adj_table = rb_hash_aref(opt, ID2SYM(rb_intern("adj_table")));
2126
if(!NIL_P(weight)) c_opt.weight = NUM2DBL(weight);
22-
if(c_opt.weight > 0.25) rb_raise(rb_eRuntimeError, "Scaling factor should not exceed 0.25, otherwise the distance can become larger than 1.");
27+
if(c_opt.weight > 0.25) rb_raise(rb_eInvalidWeightError, "Scaling factor should not exceed 0.25, otherwise the distance can become larger than 1.");
2328
if(!NIL_P(threshold)) c_opt.threshold = NUM2DBL(threshold);
2429
if(!NIL_P(ignore_case)) c_opt.ignore_case = (TYPE(ignore_case) == T_FALSE || NIL_P(ignore_case)) ? 0 : 1;
2530
if(!NIL_P(adj_table)) c_opt.adj_table = (TYPE(adj_table) == T_FALSE || NIL_P(adj_table)) ? 0 : 1;

jaro_winkler.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
1717
spec.add_development_dependency "bundler", "~> 1.7"
1818
spec.add_development_dependency "rake", "~> 10.0"
1919
spec.add_development_dependency "rake-compiler"
20-
spec.add_development_dependency "rspec"
20+
spec.add_development_dependency "minitest"
2121
spec.add_development_dependency "fuzzy-string-match"
2222
spec.add_development_dependency "hotwater"
2323
spec.add_development_dependency "amatch"

spec/adjusting_table_spec.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/jaro_winkler_spec.rb

Lines changed: 0 additions & 75 deletions
This file was deleted.

spec/spec_helper.rb

Lines changed: 0 additions & 89 deletions
This file was deleted.

test/test_jaro_winkler.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
require 'minitest/autorun'
2+
require 'jaro_winkler'
3+
4+
class TestJaroWinkler < Minitest::Test
5+
def test_distance
6+
assert_distance 0.9667, 'henka', 'henkan'
7+
assert_distance 1.0, 'al', 'al'
8+
assert_distance 0.9611, 'martha', 'marhta'
9+
assert_distance 0.8324, 'jones', 'johnson'
10+
assert_distance 0.9583, 'abcvwxyz', 'cabvwxyz'
11+
assert_distance 0.8400, 'dwayne', 'duane'
12+
assert_distance 0.8133, 'dixon', 'dicksonx'
13+
assert_distance 0.0, 'fvie', 'ten'
14+
assert_distance 1.0, 'tony', 'tony'
15+
assert_distance 1.0, 'tonytonyjan', 'tonytonyjan'
16+
assert_distance 1.0, 'x', 'x'
17+
assert_distance 0.0, '', ''
18+
assert_distance 0.0, 'tony', ''
19+
assert_distance 0.0, '', 'tony'
20+
assert_distance 0.8727, 'tonytonyjan', 'tony'
21+
assert_distance 0.8727, 'tony', 'tonytonyjan'
22+
end
23+
24+
def test_unicode
25+
assert_distance 0.9818, '變形金剛4:絕跡重生', '變形金剛4: 絕跡重生'
26+
assert_distance 0.8222, '連勝文', '連勝丼'
27+
assert_distance 0.8222, '馬英九', '馬英丸'
28+
assert_distance 0.6667, '良い', 'いい'
29+
end
30+
31+
def test_ignore_case
32+
assert_distance 0.9611, 'MARTHA', 'marhta', ignore_case: true
33+
end
34+
35+
def test_weight
36+
assert_distance 0.9778, 'MARTHA', 'MARHTA', weight: 0.2
37+
end
38+
39+
def test_threshold
40+
assert_distance 0.9444, 'MARTHA', 'MARHTA', threshold: 0.99
41+
end
42+
43+
44+
def test_adjusting_table
45+
assert_distance 0.9667, 'HENKA', 'HENKAN', adj_table: true
46+
assert_distance 1.0, 'AL', 'AL', adj_table: true
47+
assert_distance 0.9611, 'MARTHA', 'MARHTA', adj_table: true
48+
assert_distance 0.8598, 'JONES', 'JOHNSON', adj_table: true
49+
assert_distance 0.9583, 'ABCVWXYZ', 'CABVWXYZ', adj_table: true
50+
assert_distance 0.8730, 'DWAYNE', 'DUANE', adj_table: true
51+
assert_distance 0.8393, 'DIXON', 'DICKSONX', adj_table: true
52+
assert_distance 0.0, 'FVIE', 'TEN', adj_table: true
53+
end
54+
55+
def test_error
56+
assert_raises JaroWinkler::InvalidWeightError do
57+
JaroWinkler.distance 'MARTHA', 'MARHTA', weight: 0.26
58+
end
59+
end
60+
61+
def test_long_string
62+
JaroWinkler.distance 'haisai' * 20, 'haisai' * 20
63+
end
64+
65+
private
66+
67+
def assert_distance score, str1, str2, **options
68+
assert_equal score, JaroWinkler.distance(str1, str2, **options).round(4)
69+
end
70+
end

0 commit comments

Comments
 (0)