Skip to content

Commit 1b42934

Browse files
committed
✅ Add diff_helper
1 parent 51ba879 commit 1b42934

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

tests/.api-to-go.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
"jsonplaceholder.typicode.com":
22
docs:
3-
- https://jsonplaceholder.typicode.com/
3+
- https://jsonplaceholder.typicode.com/
4+
format:
5+
- /posts/{post}
6+
- /todos/{todo}

tests/api-to-go.bats

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ setup() {
88
rm -rf ./jsonplaceholder.typicode.com
99
fi
1010

11-
BASE_URL="https://jsonplaceholder.typicode.com"
11+
BASE_HOST="jsonplaceholder.typicode.com"
12+
BASE_URL="https://$BASE_HOST"
1213
API_TO_GO="node ../bin/api-to-go.js"
1314
doc "## $BATS_TEST_DESCRIPTION"
1415
}
@@ -18,28 +19,37 @@ teardown(){
1819
}
1920

2021
@test "Command with valid URL should succeed" {
21-
COMMAND="$API_TO_GO $BASE_URL/todos/1"
22+
COMMAND="$API_TO_GO $BASE_URL/posts/1"
2223
run eval "$COMMAND"
2324
[ "$status" -eq 0 ]
25+
26+
assert_files_match /posts/post.go
27+
assert_files_match /posts/post.json
2428
}
2529

2630
@test "Command with URL and JSON body" {
2731
DATA='{"title": "foo", "body": "bar", "userId": 1}'
2832
COMMAND="$API_TO_GO $BASE_URL/posts '$DATA'"
2933
run eval "$COMMAND"
3034
[ "$status" -eq 0 ]
35+
36+
assert_files_match /posts.go
37+
assert_files_match /posts.json
3138
}
3239

33-
@test "Command with custom headers" {
34-
HEADERS='{"Content-Type": "application/json"}'
35-
COMMAND="$API_TO_GO --headers '$HEADERS' $BASE_URL/todos/1"
40+
41+
@test "Command with URL" {
42+
COMMAND="$API_TO_GO $BASE_URL/posts"
3643
run eval "$COMMAND"
3744
[ "$status" -eq 0 ]
45+
46+
assert_files_match /posts.go
47+
assert_files_match /posts.json
3848
}
3949

40-
@test "Command with POST method" {
41-
DATA='{"title": "foo", "body": "bar", "userId": 1}'
42-
COMMAND="$API_TO_GO --method POST $BASE_URL/posts '$DATA'"
50+
@test "Command with custom headers" {
51+
HEADERS='{"Content-Type": "application/json"}'
52+
COMMAND="$API_TO_GO --headers '$HEADERS' $BASE_URL/posts/1"
4353
run eval "$COMMAND"
4454
[ "$status" -eq 0 ]
4555
}
@@ -50,21 +60,9 @@ teardown(){
5060
[ "$status" -eq 0 ]
5161
}
5262

53-
@test "Debug mode activation" {
54-
COMMAND="$API_TO_GO -D $BASE_URL/posts"
55-
run eval "$COMMAND"
56-
[ "$status" -eq 0 ]
57-
}
58-
5963
@test "Display help message" {
6064
COMMAND="$API_TO_GO --help"
6165
run eval "$COMMAND"
6266
[ "$status" -eq 0 ]
6367
[ "${lines[0]}" = "Usage: api-to-go [options] <url> [body]" ]
6468
}
65-
66-
@test "Display version information" {
67-
COMMAND="$API_TO_GO --version"
68-
run eval "$COMMAND"
69-
[ "$status" -eq 0 ]
70-
}

tests/helper/diff_helper.bash

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
# ファイルが一致するかどうかを確認する関数
4+
assert_files_match() {
5+
local generated_file="./$BASE_HOST$1"
6+
local fixture_file="./fixture/$BASE_HOST$1"
7+
8+
diff_output=$(diff "$generated_file" "$fixture_file")
9+
diff_status=$?
10+
11+
[ "$diff_status" -eq 0 ] # diff コマンドが0を返した場合、ファイルは一致しています
12+
}

tests/helper/helper.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22

33
load helper/doc_helper
4+
load helper/diff_helper
45

56
setup() {
67
echo "--start--"

0 commit comments

Comments
 (0)