Skip to content

Commit

Permalink
feat: Add functional tests for Chi router endpoints and improve route…
Browse files Browse the repository at this point in the history
…r function analysis

Signed-off-by: HAHWUL <[email protected]>
  • Loading branch information
hahwul committed Feb 3, 2025
1 parent b873f7b commit e607ef8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
19 changes: 19 additions & 0 deletions spec/functional_test/testers/go/chi_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "../../func_spec.cr"

expected_endpoints = [
Endpoint.new("/", "GET"),
Endpoint.new("/articles/", "POST"),
Endpoint.new("/articles/search", "GET"),
Endpoint.new("/articles/{articleSlug:[a-z-]+}", "GET", [Param.new("articleSlug", "", "path")]),
Endpoint.new("/articles/{articleID}/", "GET", [Param.new("articleID", "", "path")]),
Endpoint.new("/articles/{articleID}/", "PUT", [Param.new("articleID", "", "path")]),
Endpoint.new("/articles/{articleID}/", "DELETE", [Param.new("articleID", "", "path")]),
Endpoint.new("/admin/", "GET"),
Endpoint.new("/admin/accounts", "GET"),
Endpoint.new("/accounts", "GET"),
]

FunctionalTester.new("fixtures/go/chi/", {
:techs => 1,
:endpoints => expected_endpoints.size,
}, expected_endpoints).test_all
10 changes: 4 additions & 6 deletions src/analyzer/analyzers/go/chi.cr
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ module Analyzer::Go
def analyze_router_function(file_path : String, func_name : String) : Array(Endpoint)
endpoints = [] of Endpoint
content = File.read(file_path)
# 간단한 정규표현식으로 함수 블록 시작 위치 찾기
if start_index = content.index("func #{func_name}(")
# 함수 블록의 중괄호 범위를 찾기 위해 플래그 사용
# 함수 정의가 포함되어 있는지 확인
if content.includes?("func #{func_name}(")
block_started = false
brace_count = 0
content.lines.each do |line|
content.each_line do |line|
if !block_started
if line.includes?("func #{func_name}(")
block_started = true
Expand All @@ -119,7 +118,6 @@ module Analyzer::Go
else
brace_count += line.count("{")
brace_count -= line.count("}")
# 함수 내부에서 endpoint 처리 (r.Get, r.Post, ...)
details = Details.new(PathInfo.new(file_path))
method = ""
if line.includes?("r.Get(")
Expand All @@ -146,4 +144,4 @@ module Analyzer::Go
endpoints
end
end
end
end

0 comments on commit e607ef8

Please sign in to comment.