Skip to content

Commit 93453d4

Browse files
authored
Merge pull request #37 from nhz2/nz/depth-and-parent-tests
Fix depth and parent functions
2 parents ff0ee25 + c0b93ad commit 93453d4

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

.github/workflows/Downstream.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Downstream
2+
on:
3+
push:
4+
branches: [main]
5+
tags: [v*]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
name: ${{ matrix.package }}
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
package:
16+
- "XLSX"
17+
steps:
18+
- uses: actions/checkout@v4
19+
- uses: julia-actions/setup-julia@v2
20+
with:
21+
version: 1
22+
arch: x64
23+
show-versioninfo: true
24+
- uses: julia-actions/julia-buildpkg@latest
25+
- name: Load this and run the downstream tests
26+
shell: julia --color=yes {0}
27+
run: |
28+
using Pkg
29+
Pkg.Registry.update()
30+
Pkg.activate(;temp=true)
31+
# force it to use this PR's version of the package
32+
ENV["JULIA_PKG_DEVDIR"]= mktempdir()
33+
Pkg.develop([
34+
PackageSpec(path="."),
35+
PackageSpec(name="${{ matrix.package }}"),
36+
])
37+
Pkg.update()
38+
Pkg.test("${{ matrix.package }}")

src/XML.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ attributes(o) = o.attributes
257257
value(o) = o.value
258258
children(o::T) where {T} = isnothing(o.children) ? () : o.children
259259

260-
depth(o) = 1
260+
depth(o) = missing
261261
parent(o) = missing
262262
next(o) = missing
263263
prev(o) = missing
@@ -357,7 +357,7 @@ write(x; kw...) = (io = IOBuffer(); write(io, x; kw...); String(take!(io)))
357357

358358
write(filename::AbstractString, x; kw...) = open(io -> write(io, x; kw...), filename, "w")
359359

360-
function write(io::IO, x; indentsize::Int=2, depth::Int=depth(x))
360+
function write(io::IO, x; indentsize::Int=2, depth::Int=1)
361361
indent = ' ' ^ indentsize
362362
nodetype = XML.nodetype(x)
363363
tag = XML.tag(x)

src/raw.jl

+10-1
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,23 @@ function children(o::Raw)
208208
end
209209
end
210210

211+
"""
212+
depth(node) --> Int
213+
214+
Return the depth of the node. Will be `0` for `Document` nodes. Not defined for `XML.Node`.
215+
"""
216+
function depth(o::Raw)
217+
o.depth
218+
end
219+
211220
"""
212221
parent(node) --> typeof(node), Nothing
213222
214223
Return the parent of the node. Will be `nothing` for `Document` nodes. Not defined for `XML.Node`.
215224
"""
216225
function parent(o::Raw)
217226
depth = o.depth
218-
depth === 1 && return nothing
227+
depth === 0 && return nothing
219228
p = prev(o)
220229
while p.depth >= depth
221230
p = prev(p)

test/runtests.jl

+12
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ end
134134
end
135135
end
136136

137+
@testset "depth and parent" begin
138+
@test XML.depth(data) == 0
139+
@test isnothing(XML.parent(data))
140+
@test XML.depth(doc[1]) == 1
141+
@test XML.parent(doc[1]) == data
142+
@test XML.depth(doc[2]) == 1
143+
@test XML.depth(doc[3]) == 2
144+
@test XML.parent(doc[3]) == doc[2]
145+
@test XML.depth(doc[end]) == 1
146+
@test XML.parent(doc[end]) == data
147+
end
148+
137149
@testset "tag/attributes/value" begin
138150
x = doc[1] # <?xml version="1.0"?>
139151
@test XML.tag(x) === nothing

0 commit comments

Comments
 (0)