Skip to content

Commit 6b3a753

Browse files
committed
Test examples/stdio_server.rb
Including a sanity test ensures that the example code isn't outright wrong. This is not intended to serve as unit tests for the example functionality.
1 parent 6843880 commit 6b3a753

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ transport.open
121121

122122
You can run this script and then type in requests to the server at the command line.
123123

124+
<!-- SNIPPET ID: running_stdio_server -->
124125
```console
125126
$ ./examples/stdio_server.rb
126127
{"jsonrpc":"2.0","id":"1","method":"ping"}

test/integration/examples_test.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
require "open3"
6+
require "json"
7+
require "timeout"
8+
require "tempfile"
9+
require "fileutils"
10+
11+
require "readme_test_helper"
12+
13+
# Run tests on the files in the `examples` directory to ensure they work as expected.
14+
# These are not intended to be comprehensive; they are just sanity checks.
15+
class ExamplesTest < ActiveSupport::TestCase
16+
include ReadmeTestHelper
17+
18+
make_my_diffs_pretty!
19+
20+
test "examples/stdio_server.rb example works exactly as documented in README" do
21+
command_line, *input_lines = extract_readme_code_snippet("running_stdio_server", language: "console").lines(chomp: true)
22+
23+
command = command_line.delete_prefix("$ ")
24+
stdin_data = input_lines.join("\n")
25+
26+
stdout, stderr, status = Open3.capture3(command, chdir: project_root, stdin_data:)
27+
assert_predicate(status, :success?, "Expected #{command} to exit with success, but got exit status #{status.exitstatus}\n\nSTDOUT: #{stdout}\n\nSTDERR: #{stderr}")
28+
assert_empty(stderr, "Expected no stderr in: #{stderr}")
29+
refute_empty(stdout, "Expected stdout not to be empty")
30+
31+
assert_equal(
32+
[
33+
{ jsonrpc: "2.0", id: "1", result: {} },
34+
{
35+
jsonrpc: "2.0",
36+
id: "2",
37+
result: {
38+
tools: [
39+
{
40+
name: "example_tool",
41+
description: "A simple example tool that adds two numbers",
42+
inputSchema: { type: "object", properties: { a: { type: "number" }, b: { type: "number" } }, required: ["a", "b"] },
43+
},
44+
{
45+
name: "echo",
46+
description: "A simple example tool that echoes back its arguments",
47+
inputSchema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] },
48+
},
49+
],
50+
},
51+
},
52+
],
53+
stdout.lines.map { |line| JSON.parse(line, symbolize_names: true) },
54+
)
55+
end
56+
end

0 commit comments

Comments
 (0)