|
107 | 107 | @test reverse(str1) == Base.AnnotatedString("tset", [(1:4, :label => 5)]) |
108 | 108 | @test reverse(str2) == Base.AnnotatedString("esac", [(2:3, :label => "oomph")]) |
109 | 109 | end |
| 110 | + |
| 111 | +@testset "AnnotatedIOBuffer" begin |
| 112 | + aio = Base.AnnotatedIOBuffer() |
| 113 | + # Append-only writing |
| 114 | + @test write(aio, Base.AnnotatedString("hello", [(1:5, :tag => 1)])) == 5 |
| 115 | + @test write(aio, ' ') == 1 |
| 116 | + @test write(aio, Base.AnnotatedString("world", [(1:5, :tag => 2)])) == 5 |
| 117 | + @test Base.annotations(aio) == [(1:5, :tag => 1), (7:11, :tag => 2)] |
| 118 | + # Reading |
| 119 | + @test read(seekstart(deepcopy(aio.io)), String) == "hello world" |
| 120 | + @test read(seekstart(deepcopy(aio)), String) == "hello world" |
| 121 | + @test read(seek(aio, 0), Base.AnnotatedString) == Base.AnnotatedString("hello world", [(1:5, :tag => 1), (7:11, :tag => 2)]) |
| 122 | + @test read(seek(aio, 1), Base.AnnotatedString) == Base.AnnotatedString("ello world", [(1:4, :tag => 1), (6:10, :tag => 2)]) |
| 123 | + @test read(seek(aio, 4), Base.AnnotatedString) == Base.AnnotatedString("o world", [(1:1, :tag => 1), (3:7, :tag => 2)]) |
| 124 | + @test read(seek(aio, 5), Base.AnnotatedString) == Base.AnnotatedString(" world", [(2:6, :tag => 2)]) |
| 125 | + @test read(seekstart(truncate(deepcopy(aio), 5)), Base.AnnotatedString) == Base.AnnotatedString("hello", [(1:5, :tag => 1)]) |
| 126 | + @test read(seekstart(truncate(deepcopy(aio), 6)), Base.AnnotatedString) == Base.AnnotatedString("hello ", [(1:5, :tag => 1)]) |
| 127 | + @test read(seekstart(truncate(deepcopy(aio), 7)), Base.AnnotatedString) == Base.AnnotatedString("hello w", [(1:5, :tag => 1), (7:7, :tag => 2)]) |
| 128 | + @test read(seek(aio, 0), Base.AnnotatedChar) == Base.AnnotatedChar('h', [:tag => 1]) |
| 129 | + @test read(seek(aio, 5), Base.AnnotatedChar) == Base.AnnotatedChar(' ', Pair{Symbol, Any}[]) |
| 130 | + @test read(seek(aio, 6), Base.AnnotatedChar) == Base.AnnotatedChar('w', [:tag => 2]) |
| 131 | + # Check method compatibility with IOBuffer |
| 132 | + @test position(aio) == 7 |
| 133 | + @test seek(aio, 4) === aio |
| 134 | + @test skip(aio, 2) === aio |
| 135 | + @test Base.annotations(copy(aio)) == Base.annotations(aio) |
| 136 | + @test take!(copy(aio).io) == take!(copy(aio.io)) |
| 137 | + # Writing into the middle of the buffer |
| 138 | + @test write(seek(aio, 6), "alice") == 5 # Replace 'world' with 'alice' |
| 139 | + @test read(seekstart(aio), String) == "hello alice" |
| 140 | + @test Base.annotations(aio) == [(1:5, :tag => 1), (7:11, :tag => 2)] # Should be unchanged |
| 141 | + @test write(seek(aio, 0), Base.AnnotatedString("hey-o", [(1:5, :hey => 'o')])) == 5 |
| 142 | + @test read(seekstart(aio), String) == "hey-o alice" |
| 143 | + @test Base.annotations(aio) == [(1:5, :hey => 'o'), (7:11, :tag => 2)] # First annotation should have been entirely replaced |
| 144 | + @test write(seek(aio, 7), Base.AnnotatedString("bbi", [(1:3, :hey => 'a')])) == 3 # a[lic => bbi]e ('alice' => 'abbie') |
| 145 | + @test read(seekstart(aio), String) == "hey-o abbie" |
| 146 | + @test Base.annotations(aio) == [(1:5, :hey => 'o'), (7:7, :tag => 2), (8:10, :hey => 'a'), (11:11, :tag => 2)] |
| 147 | + @test write(seek(aio, 0), Base.AnnotatedString("ab")) == 2 # Check first annotation's region is adjusted correctly |
| 148 | + @test read(seekstart(aio), String) == "aby-o abbie" |
| 149 | + @test Base.annotations(aio) == [(3:5, :hey => 'o'), (7:7, :tag => 2), (8:10, :hey => 'a'), (11:11, :tag => 2)] |
| 150 | + @test write(seek(aio, 3), Base.AnnotatedString("ss")) == 2 |
| 151 | + @test read(seekstart(aio), String) == "abyss abbie" |
| 152 | + @test Base.annotations(aio) == [(3:3, :hey => 'o'), (7:7, :tag => 2), (8:10, :hey => 'a'), (11:11, :tag => 2)] |
| 153 | + # Writing one buffer to another |
| 154 | + newaio = Base.AnnotatedIOBuffer() |
| 155 | + @test write(newaio, seekstart(aio)) == 11 |
| 156 | + @test read(seekstart(newaio), String) == "abyss abbie" |
| 157 | + @test Base.annotations(newaio) == Base.annotations(aio) |
| 158 | + @test write(seek(newaio, 5), seek(aio, 5)) == 6 |
| 159 | + @test Base.annotations(newaio) == Base.annotations(aio) |
| 160 | + @test write(newaio, seek(aio, 5)) == 6 |
| 161 | + @test read(seekstart(newaio), String) == "abyss abbie abbie" |
| 162 | + @test Base.annotations(newaio) == vcat(Base.annotations(aio), [(13:13, :tag => 2), (14:16, :hey => 'a'), (17:17, :tag => 2)]) |
| 163 | +end |
0 commit comments