Skip to content

Commit 0c76496

Browse files
committed
Test Rails Controller README example
1 parent 6b3a753 commit 0c76496

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ requests.
6363

6464
You can use the `Server#handle_json` method to handle requests.
6565

66+
<!-- SNIPPET ID: rails_controller -->
6667
```ruby
6768
class ApplicationController < ActionController::Base
6869

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require "json"
4+
require "stringio"
5+
6+
# For simplicity, we'll stub out the relevant parts of the Rails API.
7+
module ActionController
8+
class Base
9+
class Request
10+
attr_reader :body
11+
12+
def initialize(body)
13+
@body = StringIO.new(body)
14+
end
15+
end
16+
17+
class User
18+
attr_reader :id
19+
20+
def initialize(id:)
21+
@id = id
22+
end
23+
end
24+
25+
attr_reader :request
26+
27+
def initialize(request_body)
28+
@request = Request.new(request_body)
29+
end
30+
31+
def render(json:) = json
32+
33+
private
34+
35+
def current_user = User.new(id: 1)
36+
end
37+
end
38+
39+
# We need to create the minimal surrounding resources to run the code snippet
40+
require "mcp"
41+
42+
SomeTool = MCP::Tool.define(name: "some_tool") { MCP::Tool::Response.new(content: "some_tool response") }
43+
AnotherTool = MCP::Tool.define(name: "another_tool") { MCP::Tool::Response.new(content: "another_tool response") }
44+
MyPrompt = MCP::Prompt.define(name: "my_prompt") { MCP::Prompt::Result.new }
45+
46+
require_relative "code_snippet"
47+
48+
puts ApplicationController.new(
49+
{ jsonrpc: "2.0", id: "1", method: "ping" }.to_json,
50+
).index
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
require "fileutils"
6+
require "open3"
7+
require "tempfile"
8+
require "timeout"
9+
10+
require "readme_test_helper"
11+
12+
# Run tests on the code snippets in the README.md file to ensure they work as expected.
13+
# These are not intended to be comprehensive; they are just sanity checks.
14+
class ReadmeCodeSnippetsTest < ActiveSupport::TestCase
15+
include ReadmeTestHelper
16+
17+
make_my_diffs_pretty!
18+
19+
test "Rails Controller example handles requests" do
20+
assert_json_lines(
21+
[
22+
{ jsonrpc: "2.0", id: "1", result: {} },
23+
],
24+
run_code_snippet("rails_controller"),
25+
)
26+
end
27+
28+
private
29+
30+
def assert_json_lines(expected, actual, message = "Expected the given JSON lines")
31+
assert_equal(
32+
expected,
33+
actual.lines(chomp: true).map { JSON.parse(_1, symbolize_names: true) },
34+
message,
35+
)
36+
end
37+
38+
def run_code_snippet(code_snippet_name)
39+
Dir.mktmpdir do |dir|
40+
Dir.chdir(dir) do
41+
File.write("code_snippet.rb", extract_readme_code_snippet(code_snippet_name))
42+
File.write("test.rb", file_fixture("code_snippet_wrappers/readme/#{code_snippet_name}.rb").read)
43+
44+
stdout, stderr, status = Timeout.timeout(5) { Open3.capture3("ruby", "test.rb") }
45+
assert_predicate(status, :success?, "Expected test.rb to exit with success, but got exit status #{status.exitstatus}\n\nSTDOUT: #{stdout}\n\nSTDERR: #{stderr}")
46+
assert_empty(stderr, "Expected no stderr in: #{stderr}")
47+
refute_empty(stdout, "Expected stdout not to be empty")
48+
49+
normalize_stdout(stdout)
50+
end
51+
end
52+
end
53+
54+
def normalize_stdout(stdout)
55+
stdout
56+
.gsub(/duration: \d+\.\d+(?:e-\d+)?/, "duration: 1.23") # e.g. duration: 1.23456789012345678-05
57+
end
58+
end

0 commit comments

Comments
 (0)