This wiki (clWiki) is my own, and I recently needed to inject a bit of functionality. The thing is fairly well tested, but not the most refactored thing in the world. Like any lousy short example that comes to mind, it needs a rather large setup.
My wiki supports [used to support] FullHierarchy and what I call global links. If I have a page called //TheArts/MusicBlog/ChristmasSample, without global links, if I'm in a different part of the hierarchy, I have to have an absolute link: //TheArts/MusicBlog/ChristmasSample. But with global links, I can just put in the page name: ChristmasSample, and the index will identify a match and under the covers link to the absolute page underneath.
What if I have the same page name in two different parts of the hierarchy? What if I have //ComputerTech/BurningCds and //MusicStuff/BurningCds and then I try to link to just BurningCds? In this case, I have the link go to the find results page, doing a title search on BurningCds, since the wiki shouldn't pick. Fine.
Then I started having a lot of pages like this:
//AllProjects/ThisProject
//AllProjects/ThisProject/TaskOne
//AllProjects/ThisProject/TaskTwo
//AllProjects/ThisProject/TaskThree
Then I'd be on a page like //ProgressLog/Oct13_Oct17 and link to ThisProject.
In this case there's more than one hit in the index -- every one of the four example pages gets returned, so it's a link back to the find results page. But this sucks, because, at least for myself, it's obvious I just wanted a direct link to //AllProjects/ThisProject, not to the search results including every child page.
So I wanted to add in this change. The existing code that processed the index hits to determine a direct link or a find results link wasn't too long:
hits = wikiIndex.search(pageName, titles_only)
case hits.length
when 0
result = pageName
when 1
result = "#{pageName}"
else
result = "#{pageName}"
end
but was in the middle of a longer method which itself was in a chain of calls.
I started adding some code here:
hits = wikiIndex.search(pageName, titles_only)
# new code to eliminate non-'exact' matches.
case hits.length
when 0
result = pageName
when 1
result = "#{pageName}"
else
result = "#{pageName}"
end
I put in my first shot of code -- and it didn't work at all. Not only did I still get find results links for multiple hits, I no longer got any link at all for pages that already only had one match. So immediately I knew I had two simultaneous cases working against each other, and testing this the old fashioned way was going to be a pain. I'd fix one case, break the other and go back and forth. I didn't really have an existing test for this stuff anyway ... so what to do? No existing fixture, no test, not well refactored.
I saw that I really only had two pieces of data I needed to work with -- the hits array and the pageName string. (pageName is the WikiWord I'm trying to link up).
So I wrote the following test:
class TestGlobalHitReducer < TestBase
def test_global_hit_reducer
# for global links, we want matches to be limited to exact matches.
# If //RootPage/SubPage has seven children:
#
# //RootPage/SubPage/ChildOne
# //RootPage/SubPage/ChildTwo
# ...
#
# and the content is SubPage, we don't want a global link to the
# find results of SubPage, but a direct link to //RootPage/SubPage.
# If there's also a //OtherPage/SubPage, then the global link in this
# case will still go to Find Results.
hits = ['//RootPage/SubPage', '//RootPage/SubPage/ChildOne']
reduced = GlobalHitReducer.reduce_to_exact_if_exists('SubPage', hits)
assert_equal(['//RootPage/SubPage'], reduced)
reduced = GlobalHitReducer.reduce_to_exact_if_exists('SubPa', hits)
assert_equal(hits, reduced)
hits = ['//RootPage/SubPage', '//OtherPage/SubPage']
reduced = GlobalHitReducer.reduce_to_exact_if_exists('SubPage', hits)
assert_equal(hits, reduced)
end
end
Then started writing a GlobalHitReducer class to fulfull my needs, and here ‘tis:
class GlobalHitReducer
def GlobalHitReducer.reduce_to_exact_if_exists(term, hits)
reduced = hits.dup
reduced.delete_if do |hit|
parts = hit.split('/')
exact = (parts[-1] =~ /^#{term}$/i)
!exact
end
if !reduced.empty?
reduced
else
hits
end
end
end
Feel free to proof this, this code ain't that old and is bound to have glitchiness happening...
An example of adding something new that got tested in and of itself. Add this into production:
hits = wikiIndex.search(pageName, titles_only)
hits = GlobalHitReducer.reduce_to_exact_if_exists(pageName, hits)
case hits.length
when 0
result = pageName
when 1
result = "#{pageName}"
else
result = "#{pageName}"
end