1#!/usr/bin/env python3 2# Copyright 2021 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Tests decoding metadata from log strings.""" 16 17import unittest 18 19from pw_log_tokenized import FormatStringWithMetadata 20 21 22class TestFormatStringWithMetadata(unittest.TestCase): 23 """Tests extracting metadata from a pw_log_tokenized-style format string.""" 24 25 def test_all_fields(self): 26 log = FormatStringWithMetadata( 27 '■msg♦hello %d■file♦__FILE__■module♦log module name!' 28 ) 29 self.assertEqual(log.message, 'hello %d') 30 self.assertEqual(log.module, 'log module name!') 31 self.assertEqual(log.file, '__FILE__') 32 33 def test_different_fields(self): 34 log = FormatStringWithMetadata('■msg♦hello %d■module♦■THING♦abc123') 35 self.assertEqual(log.message, 'hello %d') 36 self.assertEqual(log.module, '') 37 self.assertEqual(log.file, '') 38 self.assertEqual(log.fields['THING'], 'abc123') 39 40 def test_no_metadata(self): 41 log = FormatStringWithMetadata('a■msg♦not formatted correctly') 42 self.assertEqual(log.message, log.raw_string) 43 self.assertEqual(log.module, '') 44 self.assertEqual(log.file, '') 45 46 def test_invalid_field_name(self): 47 log = FormatStringWithMetadata('■msg♦M♦S♦G■1abc♦abc■other♦hi') 48 self.assertEqual(log.message, 'M♦S♦G■1abc♦abc') 49 self.assertEqual(log.fields['other'], 'hi') 50 51 def test_delimiters_in_value(self): 52 log = FormatStringWithMetadata('■msg♦♦■♦■yo■module♦M♦DU■E') 53 self.assertEqual(log.message, '♦■♦■yo') 54 self.assertEqual(log.module, 'M♦DU■E') 55 56 57if __name__ == '__main__': 58 unittest.main() 59