Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for convert_to_byte_offset_position
Browse files Browse the repository at this point in the history
NotFounds committed Jun 4, 2024

Verified

This commit was signed with the committer’s verified signature.
dmtucker David Tucker
1 parent a661b98 commit 3cbb882
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/utils_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# typed: true
# frozen_string_literal: true

require "test_helper"

class UtilsTest < Minitest::Test
def test_convert_to_byte_offset_position_with_utf8
source = "hello"
assert_equal 2, RubyLsp.convert_to_byte_offset_position(source, 2, Encoding::UTF_8)
assert_equal 5, RubyLsp.convert_to_byte_offset_position(source, 5, Encoding::UTF_8)

# test multibyte characters
source = "こんにちは"
assert_equal 9, RubyLsp.convert_to_byte_offset_position(source, 3, Encoding::UTF_8)
assert_equal 15, RubyLsp.convert_to_byte_offset_position(source, 5, Encoding::UTF_8)
end

def test_convert_to_byte_offset_position_with_utf16
source = "hello"
assert_equal 4, RubyLsp.convert_to_byte_offset_position(source, 2, Encoding::UTF_16)
assert_equal 10, RubyLsp.convert_to_byte_offset_position(source, 5, Encoding::UTF_16)

# test surrogate pairs
source = "a😊b"
assert_equal 4, RubyLsp.convert_to_byte_offset_position(source, 2, Encoding::UTF_16)
assert_equal 6, RubyLsp.convert_to_byte_offset_position(source, 3, Encoding::UTF_16)
end

def test_convert_to_byte_offset_position_with_utf32
source = "hello"
assert_equal 8, RubyLsp.convert_to_byte_offset_position(source, 2, Encoding::UTF_32)
assert_equal 20, RubyLsp.convert_to_byte_offset_position(source, 5, Encoding::UTF_32)
end
end

0 comments on commit 3cbb882

Please sign in to comment.