PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test_name.rb

https://github.com/openaustralia/openaustralia-parser
Ruby | 311 lines | 258 code | 43 blank | 10 comment | 0 complexity | c6b04688c19acc71d0f19df9908f5aa8 MD5 | raw file
  1. # frozen_string_literal: true
  2. # !/usr/bin/env ruby
  3. $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
  4. require "test/unit"
  5. require "name"
  6. class TestName < Test::Unit::TestCase
  7. def setup
  8. @matthew = Name.new(first: "Matthew", middle: "Noah", last: "Landauer")
  9. @joanna_gash = Name.new(first: "Joanna", last: "Gash")
  10. end
  11. def test_new
  12. assert_equal("Matthew", @matthew.first)
  13. assert_equal("Noah", @matthew.middle)
  14. assert_equal("Landauer", @matthew.last)
  15. end
  16. def test_new_wrong_parameters
  17. assert_raise(RuntimeError) { Name.new(first: "foo", blah: "dibble") }
  18. end
  19. def test_equals
  20. assert_equal(@matthew, Name.new(last: "Landauer", middle: "Noah", first: "Matthew"))
  21. assert_not_equal(@matthew, Name.new(last: "Landauer"))
  22. end
  23. def test_simple_parse
  24. assert_equal(@joanna_gash, Name.last_title_first("Gash Joanna"))
  25. end
  26. def test_capitals
  27. assert_equal(@joanna_gash, Name.last_title_first("GASH joanna"))
  28. end
  29. # def test_comma
  30. # assert_equal(@joanna_gash, Name.last_title_first("Gash, Joanna"))
  31. # end
  32. def test_middle_name
  33. assert_equal(Name.new(last: "Albanese", first: "Anthony", middle: "Norman"),
  34. Name.last_title_first("Albanese Anthony Norman"))
  35. end
  36. def test_two_middle_names
  37. assert_equal(Name.new(last: "Albanese", first: "Anthony", middle: "Norman peter"),
  38. Name.last_title_first("Albanese Anthony Norman Peter"))
  39. end
  40. def test_the_hon
  41. assert_equal(Name.new(last: "Baird", title: "the Hon.", first: "Bruce", middle: "George"),
  42. Name.last_title_first("Baird the Hon. Bruce George"))
  43. end
  44. def test_initials_last
  45. assert_equal(Name.new(last: "Johnson", initials: "JF"),
  46. Name.last_title_first("Johnson, JF"))
  47. end
  48. def test_initials_last2
  49. assert_equal(Name.new(last: "Johnson", initials: "JFK"),
  50. Name.last_title_first("Johnson, JFK"))
  51. end
  52. def test_nickname
  53. assert_equal(Name.new(last: "Abbott", title: "the Hon.", first: "Anthony", middle: "John"),
  54. Name.last_title_first("ABBOTT, the Hon. Anthony (Tony) John"))
  55. end
  56. def test_dr
  57. assert_equal(Name.new(last: "Emerson", title: "Dr", first: "Craig", middle: "Anthony"),
  58. Name.last_title_first("EMERSON, Dr Craig Anthony"))
  59. end
  60. def test_informal_name
  61. assert_equal("Matthew Landauer", Name.new(first: "Matthew", last: "Landauer", title: "Dr").informal_name)
  62. end
  63. def test_full_name
  64. name = Name.new(last: "Abbott", title: "the Hon.", first: "Anthony", middle: "John")
  65. assert_equal("the Hon. Anthony John Abbott", name.full_name)
  66. end
  67. def test_capitals_irish_name
  68. assert_equal("O'Connor", Name.new(last: "o'connor").last)
  69. end
  70. def test_capitals_scottish_name
  71. assert_equal("McMullan", Name.new(last: "mcmullan").last)
  72. end
  73. def test_dath
  74. assert_equal("D'Ath", Name.new(last: "d’ath").last)
  75. end
  76. def test_first_name_ed
  77. name = Name.title_first_last("Ed Husic")
  78. assert_equal("Husic", name.last)
  79. assert_equal("Ed", name.first)
  80. end
  81. def test_non_breaking_space
  82. # First check names without any unicode
  83. # assert_equal(Name.new(:title => "Mr", :first => "John", :last => "Smith"), Name.title_first_last("Mr John Smith"))
  84. # assert_equal(Name.new(:title => "Mr", :first => "John", :last => "Smith"), Name.last_title_first("Smith, Mr John"))
  85. # Now check similar names with unicode
  86. nbsp = [160].pack("U")
  87. assert_equal(Name.new(title: "Mr", first: "John", last: "Smith"),
  88. Name.title_first_last("Mr#{nbsp}John#{nbsp}Smith"))
  89. # assert_equal(Name.new(:title => "Mr", :first => "John", :last => "Smith"), Name.last_title_first("Smith,#{nbsp}Mr#{nbsp}John"))
  90. end
  91. def test_double_barrelled
  92. assert_equal("Hanson-Young", Name.new(last: "hanson-young").last)
  93. end
  94. def test_title_first_last
  95. assert_equal(Name.new(title: "Dr", first: "John", last: "Smith"), Name.title_first_last("Dr John Smith"))
  96. assert_equal(Name.new(title: "Dr", last: "Smith"), Name.title_first_last("Dr Smith"))
  97. assert_equal(Name.new(title: "Mr", last: "Smith"), Name.title_first_last("Mr Smith"))
  98. assert_equal(Name.new(title: "Mrs", last: "Smith"), Name.title_first_last("Mrs Smith"))
  99. assert_equal(Name.new(title: "Ms", first: "Julie", last: "Smith"), Name.title_first_last("Ms Julie Smith"))
  100. assert_equal(Name.new(title: "Ms", first: "Julie", middle: "Sarah Marie", last: "Smith"),
  101. Name.title_first_last("Ms Julie Sarah Marie Smith"))
  102. end
  103. def test_john_debus
  104. # He has a title of "Hon." rather than "the Hon."
  105. name = Name.last_title_first("DEBUS, Hon. Robert (Bob) John")
  106. assert_equal("Debus", name.last)
  107. assert_equal("Hon.", name.title)
  108. assert_equal("Robert", name.first)
  109. assert_equal("John", name.middle)
  110. end
  111. # Deal with weirdo titles at the end
  112. def test_post_title
  113. name = Name.last_title_first("COMBET, the Hon. Gregory (Greg) Ivan, AM")
  114. assert_equal("Combet", name.last)
  115. assert_equal("the Hon.", name.title)
  116. assert_equal("Gregory", name.first)
  117. assert_equal("Ivan", name.middle)
  118. assert_equal("AM", name.post_title)
  119. end
  120. def test_post_title_mbe
  121. assert_equal(Name.new(first: "John", last: "Smith", post_title: "MBE"), Name.last_title_first("Smith, John, MBE"))
  122. end
  123. def test_post_title_qc
  124. assert_equal(Name.new(first: "John", last: "Smith", post_title: "QC"), Name.last_title_first("Smith, John, QC"))
  125. end
  126. def test_post_title_obe
  127. assert_equal(Name.new(first: "John", last: "Smith", post_title: "OBE"), Name.last_title_first("Smith, John, OBE"))
  128. end
  129. def test_post_title_ksj
  130. assert_equal(Name.new(first: "John", last: "Smith", post_title: "KSJ"), Name.last_title_first("Smith, John, KSJ"))
  131. end
  132. def test_post_title_jp
  133. assert_equal(Name.new(first: "John", last: "Smith", post_title: "JP"), Name.last_title_first("Smith, John, JP"))
  134. end
  135. def test_capilisation_on_middle_name
  136. assert_equal("McCahon", Name.new(middle: "mccahon").middle)
  137. end
  138. def test_ian_sinclair
  139. assert_equal(Name.new(last: "Sinclair", title: "the Rt Hon.", first: "Ian", middle: "McCahon"),
  140. Name.last_title_first("SINCLAIR, the Rt Hon. Ian Mccahon"))
  141. end
  142. def test_two_post_titles
  143. assert_equal(Name.new(last: "Williams", title: "the Hon.", first: "Daryl", middle: "Robert", post_title: "AM QC"),
  144. Name.last_title_first("WILLIAMS, the Hon. Daryl Robert, AM, QC"))
  145. end
  146. def test_stott_despoja
  147. # Difficult situation of two last names which aren't hyphenated
  148. assert_equal(Name.new(last: "Stott Despoja", first: "Natasha", middle: "Jessica"),
  149. Name.last_title_first("STOTT DESPOJA, Natasha Jessica"))
  150. assert_equal(Name.new(last: "Stott Despoja", title: "Senator"),
  151. Name.title_first_last("Senator STOTT DESPOJA"))
  152. assert_equal(Name.new(last: "Stott Despoja", first: "Natasha"),
  153. Name.title_first_last("Natasha Stott Despoja"))
  154. end
  155. # Class for simple (naive) way of comparing two names. Only compares parts of the name
  156. # that exist in both names
  157. def test_matches
  158. dr_john_smith = Name.new(title: "Dr", first: "John", last: "Smith")
  159. peter_smith = Name.new(first: "Peter", last: "Smith")
  160. smith = Name.new(last: "Smith")
  161. dr_john = Name.new(title: "Dr", first: "John")
  162. assert(dr_john_smith.matches?(dr_john_smith))
  163. assert(!dr_john_smith.matches?(peter_smith))
  164. # When there is no overlap between the names they should not match
  165. assert(!smith.matches?(dr_john))
  166. end
  167. def test_nickname_after_middle_names
  168. assert_equal(Name.new(last: "Macdonald", title: "the Hon.", first: "John", middle: "Alexander Lindsay"),
  169. Name.last_title_first("MACDONALD, the Hon. John Alexander Lindsay (Sandy)"))
  170. end
  171. # This test for the regression introduced by adding support for initials
  172. def test_matches_with_middle_name_missing
  173. name1 = Name.new(first: "Kim", middle: "William", last: "Wilkie")
  174. name2 = Name.new(first: "Kim", last: "Wilkie")
  175. assert(name1.matches?(name2))
  176. end
  177. def test_the_hon_john_howard_mp
  178. assert_equal(Name.title_first_last("The Hon John Howard MP"),
  179. Name.new(title: "the Hon.", first: "John", last: "Howard", post_title: "MP"))
  180. end
  181. def test_senator_the_hon_nick_minchin
  182. assert_equal(Name.title_first_last("Senator the Hon Nick Minchin"),
  183. Name.new(title: "Senator the Hon.", first: "Nick", last: "Minchin"))
  184. end
  185. def test_title_first_last_djc_kerr
  186. assert_equal(Name.new(initials: "DJC", last: "Kerr"), Name.title_first_last("DJC Kerr"))
  187. end
  188. def test_parsing_initials
  189. assert_equal(Name.new(initials: "LK", last: "Johnson"), Name.title_first_last("LK Johnson"))
  190. end
  191. def test_matches_with_first_initials
  192. l_johnson = Name.title_first_last("L Johnson")
  193. leonard_keith_johnson = Name.new(first: "Leonard", middle: "Keith", last: "Johnson")
  194. leslie_royston_johnson = Name.new(first: "Leslie", middle: "Royston", last: "Johnson")
  195. peter_francis_johnson = Name.new(first: "Peter", middle: "Francis", last: "Johnson")
  196. assert(!peter_francis_johnson.matches?(l_johnson))
  197. assert(leonard_keith_johnson.matches?(l_johnson))
  198. assert(leslie_royston_johnson.matches?(l_johnson))
  199. end
  200. def test_matches_with_middle_initials
  201. lk_johnson = Name.title_first_last("LK Johnson")
  202. leonard_keith_johnson = Name.new(first: "Leonard", middle: "Keith", last: "Johnson")
  203. leslie_royston_johnson = Name.new(first: "Leslie", middle: "Royston", last: "Johnson")
  204. assert(!leslie_royston_johnson.matches?(lk_johnson))
  205. assert(leonard_keith_johnson.matches?(lk_johnson))
  206. assert(lk_johnson.matches?(leonard_keith_johnson))
  207. end
  208. def test_initials
  209. assert_equal("DJ", Name.title_first_last("Dan John Van Manen").real_initials)
  210. assert_equal("JEP", Name.new(first: "John", middle: "Edward Peter").real_initials)
  211. assert_equal("DJ", Name.new(first: "Dan", middle: "John", last: "Van Manen").real_initials)
  212. assert_equal("MN", Name.new(initials: "MN").real_initials)
  213. end
  214. def test_first_initial
  215. assert_equal("J", Name.new(first: "John", middle: "Edward Peter").first_initial)
  216. assert_equal("M", Name.new(initials: "MN").first_initial)
  217. end
  218. def test_middle_initials
  219. assert_equal("EP", Name.new(first: "John", middle: "Edward Peter").middle_initials)
  220. assert_equal("N", Name.new(initials: "MN").middle_initials)
  221. end
  222. def test_another_three_letter_initial
  223. assert_equal(Name.new(title: "Hon.", initials: "DGH", last: "Adams"), Name.title_first_last("Hon. DGH Adams"))
  224. assert_equal(Name.new(title: "Hon.", initials: "DGH", last: "Adams"), Name.title_first_last("Hon. D.G.H. Adams"))
  225. end
  226. def test_lady_bjelke_petersen
  227. assert_equal(Name.new(last: "Bjelke-Petersen", title: "Lady"),
  228. Name.last_title_first("BJELKE-PETERSEN, Lady (Florence Isabel)"))
  229. end
  230. def test_initials_with_fullstops
  231. assert("DGH", Name.initials_with_fullstops("D.G.H."))
  232. assert("AB", Name.initials_with_fullstops("A.B."))
  233. assert("M", Name.initials_with_fullstops("M."))
  234. assert("AB", Name.initials_with_fullstops("AB."))
  235. assert_nil(Name.initials_with_fullstops("AB"))
  236. assert("", Name.initials_with_fullstops(".."))
  237. end
  238. def test_empty_name
  239. assert_equal(Name.new({}), Name.title_first_last(""))
  240. assert_equal(Name.new({}), Name.last_title_first(""))
  241. end
  242. def test_initials_at_end
  243. assert_equal(Name.new(initials: "MAJ", last: "Vaile"), Name.last_title_first("Vaile, M.A.J."))
  244. assert_equal(Name.new(initials: "M", last: "Turnbull"), Name.last_title_first("Turnbull, M."))
  245. end
  246. def test_initials_with_spaces
  247. assert_equal(Name.new(last: "Wakelin", initials: "BH"), Name.last_title_first("Wakelin, B. H."))
  248. end
  249. # If only people could learn how to type properly
  250. def test_initials_with_multiple_fullstops
  251. assert_equal(Name.new(last: "Trood", initials: "RB"), Name.last_title_first("Trood R.B.."))
  252. end
  253. end