-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add tests for convert_to_byte_offset_position
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |