# encoding: UTF-8

from humanname import Name


def test_simple_name():
    n = Name.parse("Jane Doe")
    assert n.given_name == 'Jane'
    assert n.surname == 'Doe'
    assert n.middle_names is None
    assert n.suffix is None
    assert n.display_full == 'Jane Doe'
    assert n.display_first_last == 'Jane Doe'
    assert n.display_initial_surname == 'J. Doe'
    assert n.goes_by_middle_name is False
    assert n.length == 8


def test_complex_name():
    n = Name.parse("JOHN ALLEN Q DE LA MACDONALD JR")
    assert n.given_name == 'John'
    assert n.surname == 'de la MacDonald'
    assert n.suffix == 'Jr.'
    assert n.display_full == 'John Allen Q. de la MacDonald, Jr.'
    assert n.display_first_last == 'John de la MacDonald'
    assert n.display_initial_surname == 'J. de la MacDonald'
    assert n.length == len('John Allen Q. de la MacDonald, Jr.')


def test_failure():
    assert Name.parse('nope') is None


def test_non_utf8():
    string = u"Björn O'Malley-Muñoz".encode("ISO-8859-1").decode("ISO-8859-1")

    n = Name.parse(string)
    assert n.given_name == u"Björn"             # Normalized NFKD
    assert n.surname == u"O'Malley-Muñoz"       # Normalized NFKD


def test_equality_identical():
    assert Name.parse("Jane Doe") == Name.parse("Jane Doe")


def test_equality_consistent():
    assert Name.parse("Jane Doe") == Name.parse("J. Doe")


def test_equality_inconsistent():
    assert Name.parse("Jane Doe") != Name.parse("John Doe")


def test_hash_identical():
    assert hash(Name.parse("Jane Doe")) == hash(Name.parse("Jane Doe"))


def test_hash_consistent():
    assert hash(Name.parse("Jane Doe")) == hash(Name.parse("J. Doe"))


def test_hash_different_surnames():
    assert hash(Name.parse("Jane Doe")) != hash(Name.parse("Jane Dee"))


def test_matches_slug_or_localpart_matching():
    assert Name.parse("Jane Doe").matches_slug_or_localpart('janexdoe')


def test_matches_slug_or_localpart_nonmatching():
    assert not Name.parse("Jane Doe").matches_slug_or_localpart('johnxdoe')


"""
  it 'does not leak memory' do
    def rss
      GC.start
      `ps -o rss= -p #{Process.pid}`.chomp.to_i
    end

    before = rss

    100000.times do
      n = HumanName.parse("John Doe")
      HumanName::NAME_PARTS.each { |part| n.send(part) }
    end

    expect(rss).to be < 2 * before
  end
"""

@@ -2031,42 +2031,63 @@
 )%0A%0A%0A
-%22%22%22%0A  it 'does not leak memory' do
+def test_no_memory_leak():%0A    import gc%0A    import os%0A
 %0A   
@@ -2098,79 +2098,128 @@
  rss
+():
 %0A      
-GC.start%0A      %60ps -o rss= -p #%7BProcess.pid%7D%60.chomp.to_i%0A    end
+  gc.collect()%0A        out = os.popen(%22ps -o rss= -p %25d%22 %25 os.getpid()).read()%0A        return int(out.strip())
 %0A%0A  
@@ -2236,30 +2236,42 @@
  rss
+()
 %0A%0A    
-100000.times do%0A
+for _ in range(100000):%0A  
     
@@ -2276,21 +2276,16 @@
     n = 
-Human
 Name.par
@@ -2309,109 +2309,89 @@
     
-HumanName::NAME_PARTS.each %7B %7Cpart%7C n.send(part) %7D%0A    end%0A%0A    expect(rss).to be %3C 2 * before%0A  end%0A%22%22%22
+  n.given_name%0A        n.surname%0A%0A    after = rss()%0A%0A    assert after %3C 1.1 * before
 %0A

Enable memory leak test