Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix keywords in CodeBlock name #27

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 49 additions & 38 deletions src/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,52 +331,63 @@ function parse_code_blocks(
broken = false
name = nothing
for line in code
if startswith(line, "// %%")
if !isempty(src) && !all_comments(src)
block = CodeBlock(
basename,
src,
name,
write,
expect_warnings,
expect_errors,
expect_abort,
broken,
)
push!(blocks, block)
src = ""
end
# reset flags
write = contains(line, "write")
expect_warnings = contains(line, "warnings")
expect_errors = contains(line, "errors")
expect_abort = contains(line, "abort")
broken = contains(line, "broken")
if contains(line, "name")
name = match(r"name=\"(.*?)\"", line).captures[1]
else
name = nothing
end
if contains(line, "load")
m = match(r"load=\"(.*?)\"", line)
if !isnothing(m)
filename = joinpath(cwd, m.captures[1])
if !isfile(filename)
error("$(basename): 'load' directive points to a file that was not found: $(filename)")
end
src = read(filename, String)
if !startswith(line, "// %%")
src *= line
src *= "\n"
continue
end

# Store the current CodeBlock
if !isempty(src) && !all_comments(src)
block = CodeBlock(
basename,
strip(src),
name,
write,
expect_warnings,
expect_errors,
expect_abort,
broken,
)
push!(blocks, block)
src = ""
end

# Reset flags
if contains(line, "name")
name = match(r"name=\"(.*?)\"", line).captures[1]
# Remove `name` from line to avoid conflict with other keywords.
line = replace(line, r"name=\"(.*?)\"" => "")
else
name = nothing
end

if contains(line, "load")
m = match(r"load=\"(.*?)\"", line)
if !isnothing(m)
filename = joinpath(cwd, m.captures[1])
if !isfile(filename)
error("$(basename): 'load' directive points to a file that was not found: $(filename)")
end
src = read(filename, String)

# Remove `load` for same reason.
line = replace(line, r"load=\"(.*?)\"" => "")
end
continue
end
src *= line
src *= "\n"

write = contains(line, "write")
expect_warnings = contains(line, "warnings")
expect_errors = contains(line, "errors")
expect_abort = contains(line, "abort")
broken = contains(line, "broken")
end

src = strip(src)
if !isempty(src) && !all_comments(src)
block = CodeBlock(
basename,
string(src),
src,
name,
write,
expect_warnings,
Expand Down
43 changes: 16 additions & 27 deletions test/helpers_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,57 +157,46 @@ def output { 6 }

// %% name="buzz", broken
ic () requires not empty({"Oops"})

// %% name="no errors, warnings, broken, or writes", read
def output { 8 }
"""
blocks = parse_code_blocks(@__DIR__, "my_code", split(code, "\n"))

@test length(blocks) == 7
@test length(blocks) == 8

@test blocks[1] == CodeBlock(
"my_code", """
// some comments
def output { 1 }

""",
"my_code", """// some comments
def output { 1 }""",
nothing, false, false, false, false, false
)
@test blocks[2] == CodeBlock(
"my_code", """
def output { 2 }

""",
"my_code", "def output { 2 }",
nothing, false, false, false, false, false
)
@test blocks[3] == CodeBlock(
"my_code", """

def output { 3 }

""",
"my_code", "def output { 3 }",
nothing, true, false, true, true, false
)
@test blocks[4] == CodeBlock(
"my_code", """
def output { 4 }
""",
"my_code", "def output { 4 }",
"foo", false, true, false, false, false
)
@test blocks[5] == CodeBlock(
"my_code", """
def output { 5 }
""",
"my_code", "def output { 5 }",
"bar", true, false, false, false, false
)
@test blocks[6] == CodeBlock(
"my_code", """
def output { 6 }

""",
"my_code", "def output { 6 }",
"baz", true, false, false, false, false
)
@test blocks[7] == CodeBlock(
"my_code", """
ic () requires not empty({"Oops"})""",
"my_code", """ic () requires not empty({"Oops"})""",
"buzz", false, false, false, false, true
)
@test blocks[8] == CodeBlock(
"my_code", "def output { 8 }",
"no errors, warnings, broken, or writes", false, false, false, false, false
)
end
end
Loading