Ruby tests were a nice way to start, and it was nice to borrow some from
selecta. However, it's going to be much easier to write tests in the
same language as the implementation.
| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,54 +0,0 @@ |
| 1 |
-require "minitest/autorun" |
|
| 2 |
- |
|
| 3 |
-# Largely borrowed from selecta |
|
| 4 |
-describe "score" do |
|
| 5 |
- def score(candidate, query) |
|
| 6 |
- # FIXME: should escape this properly |
|
| 7 |
- ret = `./testscore '#{query}' '#{candidate}'`
|
|
| 8 |
- ret.to_f unless ret.empty? |
|
| 9 |
- end |
|
| 10 |
- |
|
| 11 |
- def assert_unmatched(candidate, query) |
|
| 12 |
- assert_equal nil, score(candidate, query) |
|
| 13 |
- end |
|
| 14 |
- |
|
| 15 |
- def assert_matched(candidate, query) |
|
| 16 |
- assert_operator 0, :<=, score(candidate, query) |
|
| 17 |
- end |
|
| 18 |
- |
|
| 19 |
- it "scores 1 when the query is empty" do |
|
| 20 |
- assert_equal 1, score("a", "")
|
|
| 21 |
- end |
|
| 22 |
- |
|
| 23 |
- it "scores 0 when the choice is empty" do |
|
| 24 |
- assert_unmatched "", "a" |
|
| 25 |
- end |
|
| 26 |
- |
|
| 27 |
- it "scores 1 when exact match" do |
|
| 28 |
- assert_equal 1, score("a", "a")
|
|
| 29 |
- end |
|
| 30 |
- |
|
| 31 |
- it "scores 0 when the query is longer than the choice" do |
|
| 32 |
- assert_unmatched "short", "longer" |
|
| 33 |
- end |
|
| 34 |
- |
|
| 35 |
- it "scores 0 when the query doesn't match at all" do |
|
| 36 |
- assert_unmatched "a", "b" |
|
| 37 |
- end |
|
| 38 |
- |
|
| 39 |
- it "scores 0 when only a prefix of the query matches" do |
|
| 40 |
- assert_unmatched "ab", "ac" |
|
| 41 |
- end |
|
| 42 |
- |
|
| 43 |
- it "scores greater than 0 when it matches" do |
|
| 44 |
- assert_matched "a", "a" |
|
| 45 |
- assert_matched "ab", "a" |
|
| 46 |
- assert_matched "ba", "a" |
|
| 47 |
- assert_matched "bab", "a" |
|
| 48 |
- assert_matched "bababababab", "aaaaa" |
|
| 49 |
- end |
|
| 50 |
- |
|
| 51 |
- it "prefers start of words" do |
|
| 52 |
- assert_operator score("app/models/foo", "amo"), :<, score("app/models/order", "amo")
|
|
| 53 |
- end |
|
| 54 |
-end |
| ... | ... |
@@ -4,11 +4,12 @@ require "minitest/autorun" |
| 4 | 4 |
describe "score" do |
| 5 | 5 |
def score(candidate, query) |
| 6 | 6 |
# FIXME: should escape this properly |
| 7 |
- `./testscore '#{query}' '#{candidate}'`.to_f
|
|
| 7 |
+ ret = `./testscore '#{query}' '#{candidate}'`
|
|
| 8 |
+ ret.to_f unless ret.empty? |
|
| 8 | 9 |
end |
| 9 | 10 |
|
| 10 | 11 |
def assert_unmatched(candidate, query) |
| 11 |
- assert_equal -1, score(candidate, query) |
|
| 12 |
+ assert_equal nil, score(candidate, query) |
|
| 12 | 13 |
end |
| 13 | 14 |
|
| 14 | 15 |
def assert_matched(candidate, query) |
| ... | ... |
@@ -8,11 +8,11 @@ describe "score" do |
| 8 | 8 |
end |
| 9 | 9 |
|
| 10 | 10 |
def assert_unmatched(candidate, query) |
| 11 |
- assert_equal 0, score(candidate, query) |
|
| 11 |
+ assert_equal -1, score(candidate, query) |
|
| 12 | 12 |
end |
| 13 | 13 |
|
| 14 | 14 |
def assert_matched(candidate, query) |
| 15 |
- assert_operator 0, :<, score(candidate, query) |
|
| 15 |
+ assert_operator 0, :<=, score(candidate, query) |
|
| 16 | 16 |
end |
| 17 | 17 |
|
| 18 | 18 |
it "scores 1 when the query is empty" do |
| ... | ... |
@@ -7,12 +7,20 @@ describe "score" do |
| 7 | 7 |
`./testscore '#{query}' '#{candidate}'`.to_f
|
| 8 | 8 |
end |
| 9 | 9 |
|
| 10 |
+ def assert_unmatched(candidate, query) |
|
| 11 |
+ assert_equal 0, score(candidate, query) |
|
| 12 |
+ end |
|
| 13 |
+ |
|
| 14 |
+ def assert_matched(candidate, query) |
|
| 15 |
+ assert_operator 0, :<, score(candidate, query) |
|
| 16 |
+ end |
|
| 17 |
+ |
|
| 10 | 18 |
it "scores 1 when the query is empty" do |
| 11 | 19 |
assert_equal 1, score("a", "")
|
| 12 | 20 |
end |
| 13 | 21 |
|
| 14 | 22 |
it "scores 0 when the choice is empty" do |
| 15 |
- assert_equal 0, score("", "a")
|
|
| 23 |
+ assert_unmatched "", "a" |
|
| 16 | 24 |
end |
| 17 | 25 |
|
| 18 | 26 |
it "scores 1 when exact match" do |
| ... | ... |
@@ -20,22 +28,22 @@ describe "score" do |
| 20 | 28 |
end |
| 21 | 29 |
|
| 22 | 30 |
it "scores 0 when the query is longer than the choice" do |
| 23 |
- assert_equal 0, score("short", "longer")
|
|
| 31 |
+ assert_unmatched "short", "longer" |
|
| 24 | 32 |
end |
| 25 | 33 |
|
| 26 | 34 |
it "scores 0 when the query doesn't match at all" do |
| 27 |
- assert_equal 0, score("a", "b")
|
|
| 35 |
+ assert_unmatched "a", "b" |
|
| 28 | 36 |
end |
| 29 | 37 |
|
| 30 | 38 |
it "scores 0 when only a prefix of the query matches" do |
| 31 |
- assert_equal 0, score("ab", "ac")
|
|
| 39 |
+ assert_unmatched "ab", "ac" |
|
| 32 | 40 |
end |
| 33 | 41 |
|
| 34 | 42 |
it "scores greater than 0 when it matches" do |
| 35 |
- assert_operator 0, :<, score("a", "a")
|
|
| 36 |
- assert_operator 0, :<, score("ab", "a")
|
|
| 37 |
- assert_operator 0, :<, score("ba", "a")
|
|
| 38 |
- assert_operator 0, :<, score("bab", "a")
|
|
| 39 |
- assert_operator 0, :<, score("babababab", "aaaa")
|
|
| 43 |
+ assert_matched "a", "a" |
|
| 44 |
+ assert_matched "ab", "a" |
|
| 45 |
+ assert_matched "ba", "a" |
|
| 46 |
+ assert_matched "bab", "a" |
|
| 47 |
+ assert_matched "bababababab", "aaaaa" |
|
| 40 | 48 |
end |
| 41 | 49 |
end |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,41 @@ |
| 1 |
+require "minitest/autorun" |
|
| 2 |
+ |
|
| 3 |
+# Largely borrowed from selecta |
|
| 4 |
+describe "score" do |
|
| 5 |
+ def score(candidate, query) |
|
| 6 |
+ # FIXME: should escape this properly |
|
| 7 |
+ `./testscore '#{query}' '#{candidate}'`.to_f
|
|
| 8 |
+ end |
|
| 9 |
+ |
|
| 10 |
+ it "scores 1 when the query is empty" do |
|
| 11 |
+ assert_equal 1, score("a", "")
|
|
| 12 |
+ end |
|
| 13 |
+ |
|
| 14 |
+ it "scores 0 when the choice is empty" do |
|
| 15 |
+ assert_equal 0, score("", "a")
|
|
| 16 |
+ end |
|
| 17 |
+ |
|
| 18 |
+ it "scores 1 when exact match" do |
|
| 19 |
+ assert_equal 1, score("a", "a")
|
|
| 20 |
+ end |
|
| 21 |
+ |
|
| 22 |
+ it "scores 0 when the query is longer than the choice" do |
|
| 23 |
+ assert_equal 0, score("short", "longer")
|
|
| 24 |
+ end |
|
| 25 |
+ |
|
| 26 |
+ it "scores 0 when the query doesn't match at all" do |
|
| 27 |
+ assert_equal 0, score("a", "b")
|
|
| 28 |
+ end |
|
| 29 |
+ |
|
| 30 |
+ it "scores 0 when only a prefix of the query matches" do |
|
| 31 |
+ assert_equal 0, score("ab", "ac")
|
|
| 32 |
+ end |
|
| 33 |
+ |
|
| 34 |
+ it "scores greater than 0 when it matches" do |
|
| 35 |
+ assert_operator 0, :<, score("a", "a")
|
|
| 36 |
+ assert_operator 0, :<, score("ab", "a")
|
|
| 37 |
+ assert_operator 0, :<, score("ba", "a")
|
|
| 38 |
+ assert_operator 0, :<, score("bab", "a")
|
|
| 39 |
+ assert_operator 0, :<, score("babababab", "aaaa")
|
|
| 40 |
+ end |
|
| 41 |
+end |