Skip to content

Commit

Permalink
Add test cases for Utils.utf8_encode
Browse files Browse the repository at this point in the history
  • Loading branch information
p-lambert committed Jan 16, 2018
1 parent f912cba commit d8385df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ddtrace/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ def self.utf8_encode(str, options = {})
options[:placeholder] || STRING_PLACEHOLDER
end

STRING_PLACEHOLDER = ''.freeze
STRING_PLACEHOLDER = ''.encode(Encoding::UTF_8).freeze
end
end
31 changes: 31 additions & 0 deletions test/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,35 @@ def test_forked_process_id_collision
refute_equal(Datadog::Utils.next_id, r.read.chomp.to_i)
r.close
end

def test_utf8_encoding_happy_path
str = 'pristine ✓'

assert_equal('pristine ✓', Datadog::Utils.utf8_encode(str))

assert_equal(::Encoding::UTF_8, Datadog::Utils.utf8_encode(str).encoding)

# we don't allocate new objects when a valid UTF-8 string is provided
assert_same(str, Datadog::Utils.utf8_encode(str))
end

def test_utf8_encoding_invalid_conversion
time_bomb = "\xC2".force_encoding(::Encoding::ASCII_8BIT)

# making sure this is indeed a problem
assert_raises(Encoding::CompatibilityError) do
time_bomb.encode(Encoding::UTF_8)
end

assert_equal(Datadog::Utils::STRING_PLACEHOLDER, Datadog::Utils.utf8_encode(time_bomb))

# we can also set a custom placeholder
assert_equal('?', Datadog::Utils.utf8_encode(time_bomb, placeholder: '?'))
end

def test_binary_data
byte_array = "keep what \xC2 is valid".force_encoding(::Encoding::ASCII_8BIT)

assert_equal('keep what is valid', Utils.utf8_encode(byte_array, binary: true))
end
end

0 comments on commit d8385df

Please sign in to comment.