-
-
Save codenamev/492ec4df56dacd94cf7fd9a8d27479b2 to your computer and use it in GitHub Desktop.
Create a fully implemented RSpec spec given a Ruby file.
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
#!/usr/bin/env ruby | |
# usage: generate_spec some_ruby_file.rb path/to/your/new/spec/some_ruby_spec.rb | |
# requirements: expects ANTHROPIC_API_KEY environment key to be defined | |
raise "You must provide a ruby file to generate a spec for it!" if ARGV.empty? | |
raise "File must exist!" unless File.exist?(ARGV[0]) | |
require "bundler/inline" | |
gemfile do | |
source "https://rubygems.org" | |
gem "sublayer", "~> 0.1.0.pre.alpha.3" | |
end | |
puts "Generating spec for #{ARGV[0]}..." | |
Sublayer.configuration.ai_provider = Sublayer::Providers::Claude | |
Sublayer.configuration.ai_model = "claude-3-opus-20240229" | |
# Soon to be #"clause-3-5-sonet-20240620" | |
class RSpecFromCodeGenerator < Sublayer::Generators::Base | |
llm_output_adapter type: :single_string, | |
name: "generated_spec", | |
description: "The generated RSpec spec for the requested code" | |
def initialize(file:) | |
@code = File.read(file) | |
end | |
def generate | |
super | |
end | |
def prompt | |
<<-PROMPT | |
You are an expert programmer in Ruby. | |
You are tasked with writing a single RSpec file with all the test cases for the following code: | |
<code> | |
#{@code} | |
</code> | |
Take a deep breath and think step by step before you start coding. | |
PROMPT | |
end | |
end | |
spec_file = ARGV[1] | |
spec_content = RSpecFromCodeGenerator.new(file: ARGV[0]).generate | |
if spec_file | |
p "Saving spec to file: #{spec_file}..." | |
File.write(spec_file, spec_content) | |
else | |
p spec_content | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment