diff --git a/ee/lib/code_suggestions/programming_language.rb b/ee/lib/code_suggestions/programming_language.rb index 7e24d21d20d8174a6f6441ee43d889aaaf84d193..40472c9dac7a5ad6392453f2ae0eec5876099996 100644 --- a/ee/lib/code_suggestions/programming_language.rb +++ b/ee/lib/code_suggestions/programming_language.rb @@ -129,6 +129,10 @@ class ProgrammingLanguage 'PHP' => { 'empty_function' => %r{function\s+(\w*)\s*\(.*?\)\s*(?::\s*(\w+))?\s*\{|\bfunction\s*\([^)]*\)\s*\{}, 'function' => %r{\}\s*|function\s+(\w*)\s*\(.*?\)\s*(?::\s*(\w+))?\s*\{|\bfunction\s*\([^)]*\)\s*\{} + }, + 'C#' => { + 'empty_function' => %r{\b\s*\w+\s+\w+\s*\([^)]*\)\s*\{|\s*\{}, + 'function' => %r{\}\s*|\b\s*\w+\s+\w+\s*\([^)]*\)\s*(\{?)|\s*\{} } }.freeze diff --git a/ee/spec/lib/code_suggestions/programming_language_spec.rb b/ee/spec/lib/code_suggestions/programming_language_spec.rb index cce27325f89f62b07b1c7ae2abc264dacda9e685..cb5699c04056e3fda72259ef95469bb789b943da 100644 --- a/ee/spec/lib/code_suggestions/programming_language_spec.rb +++ b/ee/spec/lib/code_suggestions/programming_language_spec.rb @@ -198,6 +198,7 @@ 'TypeScript' | 'ts language' 'Java' | 'java language' 'PHP' | 'php language' + 'C#' | 'c# language' end with_them do diff --git a/ee/spec/support/shared_examples/code_suggestions/csharp_language_code_suggestion_examples.rb b/ee/spec/support/shared_examples/code_suggestions/csharp_language_code_suggestion_examples.rb new file mode 100644 index 0000000000000000000000000000000000000000..9087ba7bd87cdba46d9ceb7bde4b8bed32f9be1e --- /dev/null +++ b/ee/spec/support/shared_examples/code_suggestions/csharp_language_code_suggestion_examples.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'c# language' do + using RSpec::Parameterized::TableSyntax + + let(:language_name) { 'C#' } + + subject { described_class.new(language_name).cursor_inside_empty_function?(content, suffix) } + + context 'when various variations of empty functions are used' do + where(example: [ + <<~EXAMPLE, + static int AddNumbers(int num1, int num2) { + <CURSOR> + } + + static int SubtractNumbers(int num1, int num2) { + return num1 - num2; + } + EXAMPLE + + <<~EXAMPLE, + static int SumValues(params int[] numbers) + { + <CURSOR> + + static int SubValues(params int[] numbers) + { + return numbers.Sub(); + } + EXAMPLE + + <<~EXAMPLE + class MathUtils + { + public static int Multiply(int num1, int num2) + { + <CURSOR> + } + } + EXAMPLE + ]) + + with_them do + let(:content) { example.split("<CURSOR>").first } + let(:suffix) { example.split("<CURSOR>").last } + + it { is_expected.to be_truthy } + end + end + + context 'when cursor is outside an empty method' do + let(:example) do + <<~CONTENT + static int AddNumbers(int num1, int num2) + { + <CURSOR> + return num1 + num2; + } + + static string GreetUser(string name, string timeOfDay) + { + return $"Good {timeOfDay}, {name}!"; + } + CONTENT + end + + let(:content) { example.split("<CURSOR>").first } + let(:suffix) { example.split("<CURSOR>").last } + + it { is_expected.to be_falsey } + end + + context 'when language is different that the given' do + let(:example) do + <<~CONTENT + def index4(arg1, arg2): + return 1 + + def func1(): + <CURSOR> + + def index2(): + return 0 + + def index3(arg1): + return 1 + CONTENT + end + + let(:content) { example.split("<CURSOR>").first } + let(:suffix) { example.split("<CURSOR>").last } + + it { is_expected.to be_falsey } + end +end