1#!/usr/bin/env python2.7 -B 2 3import logs 4import ps 5 6import datetime 7import StringIO 8 9def test_empty(): 10 """Test parsing no tag and no text, not well formed.""" 11 expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "", 12 "") ] 13 text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/ ] 14 15""" 16 check_parsing(expected, text) 17 18 19def test_none(): 20 """Test parsing no tag and no text.""" 21 expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "", 22 "") ] 23 text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/ ] 24""" 25 check_parsing(expected, text) 26 27 28 29def test_trailing_blank(): 30 """Test parsing text containing an extra intended newline at the end.""" 31 expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", 32 "Newline after\n") ] 33 text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] 34Newline after 35 36 37""" 38 check_parsing(expected, text) 39 40 41def test_blank_between(): 42 """Test parsing text containing a newline in the middle.""" 43 expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", 44 "Message\n\nNewline between") ] 45 text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] 46Message 47 48Newline between 49 50""" 51 check_parsing(expected, text) 52 53 54def test_preceeding_blank(): 55 """Test parsing text containing a newline then text.""" 56 expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", 57 "\nNewline before") ] 58 text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] 59 60Newline before 61 62""" 63 check_parsing(expected, text) 64 65 66def test_one_blank(): 67 """Test parsing text one blank line.""" 68 expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", 69 "\n") ] 70 text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] 71 72 73""" 74 check_parsing(expected, text) 75 76 77def test_two_blanks(): 78 """Test parsing text two blank lines.""" 79 expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", 80 "\n\n") ] 81 text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] 82 83 84 85""" 86 check_parsing(expected, text) 87 88 89def test_two_lines_noblanks(): 90 """Test parsing two lines of text with no blank lines.""" 91 expected = [ logs.LogLine(None, "03-29 00:46:58.872", "1000", "1815", "1816", "I", "abcd", 92 "One\nTwo") ] 93 text = """[ 03-29 00:46:58.872 1000: 1815: 1816 I/abcd ] 94One 95Two 96 97""" 98 check_parsing(expected, text) 99 100 101def test_chatty(): 102 """Test a log with chatty identical messages.""" 103 104 expected = [ 105 logs.LogLine("system", "03-29 00:46:58.857", "1000", "1815", "1816", "I", "Noisy", "Message"), 106 logs.LogLine("system", "03-29 00:46:58.858", "1000", "1815", "1816", "I", "Noisy", "Message"), 107 logs.LogLine("system", "03-29 00:46:58.858", "1000", "1815", "1816", "I", "Noisy", "Message"), 108 logs.LogLine("system", "03-29 00:46:58.858", "1000", "1815", "1816", "I", "Noisy", "Message"), 109 logs.LogLine("system", "03-29 00:46:58.859", "1000", "1815", "1816", "I", "Noisy", "Message"), 110 ] 111 text = """--------- beginning of system 112[ 03-29 00:46:58.857 1000: 1815: 1816 I/Noisy ] 113Message 114 115[ 03-29 00:46:58.858 1000: 1815: 1816 I/chatty ] 116uid=1000(system) Thread-6 identical 3 lines 117 118[ 03-29 00:46:58.859 1000: 1815: 1816 I/Noisy ] 119Message 120 121""" 122 check_parsing(expected, text) 123 124 125 126def test_normal(): 127 """Test a realistic (albeit short) log.""" 128 expected = [ 129 logs.LogLine("system", "03-29 00:46:58.857", "1000", "1815", "1816", "I", "Package: ]Manager", 130 "/system/app/KeyChain changed; collecting certs"), 131 logs.LogLine("system", "03-29 00:46:58.872", "1000", "1815", "1816", "I", "PackageManager", 132 "/system/app/HiddenMenu changed; collecting certs"), 133 logs.LogLine("main", "03-29 00:46:58.872", "1000", "1815", "1816", "I", "PackageManager", 134 "/system/app/HiddenMenu changed; collecting certs"), 135 ] 136 137 text = """--------- beginning of system 138[ 03-29 00:46:58.857 1000: 1815: 1816 I/Package: ]Manager ] 139/system/app/KeyChain changed; collecting certs 140 141[ 03-29 00:46:58.872 1000: 1815: 1816 I/PackageManager ] 142/system/app/HiddenMenu changed; collecting certs 143 144--------- switch to main 145[ 03-29 00:46:58.872 1000: 1815: 1816 I/PackageManager ] 146/system/app/HiddenMenu changed; collecting certs 147 148""" 149 check_parsing(expected, text) 150 151 152 153def check_parsing(expected, text): 154 """Parse the text and see if it parsed as expected.""" 155 processes = ps.ProcessSet() 156 result = [x for x in logs.ParseLogcat(StringIO.StringIO(text), processes)] 157 if result != expected: 158 raise Exception("test failed.\nexpected:\n[%s]\nactual\n[%s]" % ( 159 ", ".join([str(r) for r in expected]), 160 ", ".join([str(r) for r in result]))) 161 162 163def main(): 164 test_empty() 165 test_none() 166 test_trailing_blank() 167 test_blank_between() 168 test_preceeding_blank() 169 test_one_blank() 170 test_two_blanks() 171 test_chatty() 172 test_normal() 173 174 175if __name__ == "__main__": 176 main() 177 178 179# vim: set ts=2 sw=2 sts=2 tw=100 nocindent autoindent smartindent expandtab: 180