1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
require 'support'
require 'mustermann/string_scanner'
describe Mustermann::StringScanner do
include Support::ScanMatcher
subject(:scanner) { Mustermann::StringScanner.new(example_string) }
let(:example_string) { "foo bar" }
describe :scan do
it { should scan("foo") }
it { should scan(/foo/) }
it { should scan(:name) }
it { should scan(":name") }
it { should_not scan(" ") }
it { should_not scan("bar") }
example do
should scan("foo")
should scan(" ")
should scan("bar")
end
example do
scanner.position = 4
should scan("bar")
end
example do
should scan("foo")
scanner.reset
should scan("foo")
end
end
describe :check do
it { should check("foo") }
it { should check(/foo/) }
it { should check(:name) }
it { should check(":name") }
it { should_not check(" ") }
it { should_not check("bar") }
example do
should check("foo")
should_not check(" ")
should_not check("bar")
should check("foo")
end
example do
scanner.position = 4
should check("bar")
end
end
describe :scan_until do
it { should scan_until("foo") }
it { should scan_until(":name") }
it { should scan_until(" ") }
it { should scan_until("bar") }
it { should_not scan_until("baz") }
example do
should scan_until(" ")
should check("bar")
end
example do
should scan_until(" ")
scanner.reset
should scan("foo")
end
end
describe :check_until do
it { should check_until("foo") }
it { should check_until(":name") }
it { should check_until(" ") }
it { should check_until("bar") }
it { should_not check_until("baz") }
example do
should check_until(" ")
should_not check("bar")
end
end
describe :getch do
example { scanner.getch.should be == "f" }
example do
scanner.scan("foo")
scanner.getch.should be == " "
should scan("bar")
end
example do
scanner.getch
scanner.reset
should scan("foo")
end
end
describe :<< do
example do
should_not scan_until("baz")
scanner << " baz"
scanner.to_s.should be == "foo bar baz"
should scan_until("baz")
end
end
describe :eos? do
it { should_not be_eos }
example do
scanner.position = 7
should be_eos
end
end
describe :beginning_of_line? do
let(:example_string) { "foo\nbar" }
it { should be_beginning_of_line }
example do
scanner.position = 2
should_not be_beginning_of_line
end
example do
scanner.position = 3
should_not be_beginning_of_line
end
example do
scanner.position = 4
should be_beginning_of_line
end
end
describe :rest do
example { scanner.rest.should be == "foo bar" }
example do
scanner.position = 4
scanner.rest.should be == "bar"
end
end
describe :rest_size do
example { scanner.rest_size.should be == 7 }
example do
scanner.position = 4
scanner.rest_size.should be == 3
end
end
describe :peek do
example { scanner.peek(3).should be == "foo" }
example do
scanner.peek(3).should be == "foo"
scanner.peek(3).should be == "foo"
end
example do
scanner.position = 4
scanner.peek(3).should be == "bar"
end
end
describe :inspect do
example { scanner.inspect.should be == '#<Mustermann::StringScanner 0/7 @ "foo bar">' }
example do
scanner.position = 4
scanner.inspect.should be == '#<Mustermann::StringScanner 4/7 @ "foo bar">'
end
end
describe :[] do
example do
should scan(:name)
scanner['name'].should be == "foo bar"
end
example do
should scan(:name, capture: /\S+/)
scanner['name'].should be == "foo"
should scan(" :name", capture: /\S+/)
scanner['name'].should be == "bar"
end
example do
should scan(":a", capture: /\S+/)
should scan(" :b", capture: /\S+/)
scanner['a'].should be == "foo"
scanner['b'].should be == "bar"
end
example do
a = scanner.scan(":a", capture: /\S+/)
b = scanner.scan(" :b", capture: /\S+/)
a.params['a'].should be == 'foo'
b.params['b'].should be == 'bar'
a.params['b'].should be_nil
b.params['a'].should be_nil
end
example do
result = scanner.check(":a", capture: /\S+/)
result.params['a'].should be == 'foo'
scanner['a'].should be_nil
end
example do
should scan(:name)
scanner.reset
scanner['name'].should be_nil
end
end
describe :unscan do
example do
should scan(:name, capture: /\S+/)
scanner['name'].should be == "foo"
should scan(" :name", capture: /\S+/)
scanner['name'].should be == "bar"
scanner.unscan
scanner['name'].should be == "foo"
scanner.rest.should be == " bar"
end
example do
should scan_until(" ")
scanner.unscan
scanner.rest.should be == "foo bar"
end
example do
expect { scanner.unscan }.to raise_error(Mustermann::StringScanner::ScanError,
'unscan failed: previous match record not exist')
end
end
describe :terminate do
example do
scanner.terminate
scanner.should be_eos
end
end
describe :to_h do
example { scanner.to_h.should be == {} }
example do
end
end
describe :to_s do
example { scanner.to_s.should be == "foo bar" }
end
describe :clear_cache do
example do
scanner.scan("foo")
Mustermann::StringScanner.clear_cache
Mustermann::StringScanner.cache_size.should be == 0
end
end
end
|