From 3cbb8828b0eb2433e9c164b7cd13edd5491e6a0a Mon Sep 17 00:00:00 2001 From: "NotFounds(Iori IKEDA)" Date: Tue, 4 Jun 2024 23:17:39 +0900 Subject: [PATCH] Add tests for convert_to_byte_offset_position --- test/utils_test.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/utils_test.rb diff --git a/test/utils_test.rb b/test/utils_test.rb new file mode 100644 index 0000000000..aadb6f85f2 --- /dev/null +++ b/test/utils_test.rb @@ -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