Class: Dotcodegen::TestCodeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/dotcodegen/test_code_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, file_to_test_path:, openai_key:, openai_org_id: nil) ⇒ TestCodeGenerator

Returns a new instance of TestCodeGenerator.



10
11
12
13
14
15
# File 'lib/dotcodegen/test_code_generator.rb', line 10

def initialize(config:, file_to_test_path:, openai_key:, openai_org_id: nil)
  @config = config
  @file_to_test_path = file_to_test_path
  @openai_key = openai_key
  @openai_org_id = openai_org_id
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/dotcodegen/test_code_generator.rb', line 8

def config
  @config
end

#file_to_test_pathObject (readonly)

Returns the value of attribute file_to_test_path.



8
9
10
# File 'lib/dotcodegen/test_code_generator.rb', line 8

def file_to_test_path
  @file_to_test_path
end

#openai_keyObject (readonly)

Returns the value of attribute openai_key.



8
9
10
# File 'lib/dotcodegen/test_code_generator.rb', line 8

def openai_key
  @openai_key
end

#openai_org_idObject (readonly)

Returns the value of attribute openai_org_id.



8
9
10
# File 'lib/dotcodegen/test_code_generator.rb', line 8

def openai_org_id
  @openai_org_id
end

Instance Method Details

#generate_test_codeObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/dotcodegen/test_code_generator.rb', line 17

def generate_test_code
  response = openai_client.chat(
    parameters: {
      model: 'gpt-4-turbo-preview',
      messages: [{ role: 'user', content: test_prompt_text }], # Required.
      temperature: 0.7
    }
  )
  FormatOutput.format(response.dig('choices', 0, 'message', 'content'))
end

#openai_clientObject



58
59
60
61
62
# File 'lib/dotcodegen/test_code_generator.rb', line 58

def openai_client
  client_options = { access_token: openai_key }
  client_options[:organization_id] = openai_org_id unless openai_org_id.nil?
  @openai_client ||= OpenAI::Client.new(client_options)
end

#test_file_contentObject

rubocop:enable Metrics/MethodLength



50
51
52
# File 'lib/dotcodegen/test_code_generator.rb', line 50

def test_file_content
  File.open(file_to_test_path).read
end

#test_instructionsObject



54
55
56
# File 'lib/dotcodegen/test_code_generator.rb', line 54

def test_instructions
  config['content']
end

#test_promptObject

rubocop:disable Metrics/MethodLength



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dotcodegen/test_code_generator.rb', line 33

def test_prompt
  [
    'You are an expert programmer. You have been given a task to write a test file for a given file following some instructions.',
    'This is the file you want to test:',
    '--start--',
    test_file_content,
    '--end--',
    'Here are the instructions on how to write the test file:',
    '--start--',
    test_instructions,
    '--end--',
    "Your answer will be directly written in the file you want to test. Don't include any explanation or comments in your answer that isn't code.",
    'You can use the comment syntax to write comments in your answer.'
  ].join("\n")
end

#test_prompt_textObject



28
29
30
# File 'lib/dotcodegen/test_code_generator.rb', line 28

def test_prompt_text
  [{ "type": 'text', "text": test_prompt }]
end