def test_replace(self): """ Replace decoding errors with the Unicode replacement character. """ self.assertThat( format.binary('utf-32')(u'\N{SNOWMAN}'.encode('utf-8')), ExactlyEquals(u'\ufffd'))
def test_encoding(self): """ Binary values are decoded with the given encoding. """ self.assertThat( format.binary('utf-8')(u'\N{SNOWMAN}'.encode('utf-8')), ExactlyEquals(u'\u2603'))
def _default_value_formatter( human_readable, field_limit, utc_timestamps=True, encoding='utf-8', ): """ Create a value formatter based on several user-specified options. """ fields = {} if human_readable: fields = { eliot_ns(u'timestamp'): format.timestamp(include_microsecond=False, utc_timestamps=utc_timestamps), eliot_ns(u'duration'): format.duration(), } return compose( # We want tree-format to handle newlines. partial(format.escape_control_characters, overrides={0x0a: u'\n'}), partial(format.truncate_value, field_limit) if field_limit else identity, format.some(format.fields(fields), format.text(), format.binary(encoding), format.anything(encoding)))
def test_mapping(self): """ Values for known field names are passed through their processor. """ fields = { u'a': format.binary('utf-8'), } self.assertThat( format.fields(fields)(u'\N{SNOWMAN}'.encode('utf-8'), u'a'), ExactlyEquals(u'\N{SNOWMAN}'))
def test_not_binary(self): """ Not binary values are ignored. """ self.assertThat(format.binary('utf-8')(u'hello'), Is(None))