PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/strscan/test_stringscanner.rb

http://github.com/ruby/ruby
Ruby | 772 lines | 660 code | 101 blank | 11 comment | 0 complexity | 1bdb8a11274926782abc889ad4dda9a3 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # -*- coding: utf-8 -*-
  2. # frozen_string_literal: true
  3. #
  4. # test/strscan/test_stringscanner.rb
  5. #
  6. require 'strscan'
  7. require 'test/unit'
  8. class TestStringScanner < Test::Unit::TestCase
  9. def create_string_scanner(string, *args)
  10. StringScanner.new(string, *args)
  11. end
  12. def test_s_new
  13. s = create_string_scanner('test string')
  14. assert_instance_of StringScanner, s
  15. assert_equal false, s.eos?
  16. str = 'test string'.dup
  17. s = create_string_scanner(str, false)
  18. assert_instance_of StringScanner, s
  19. assert_equal false, s.eos?
  20. assert_same str, s.string
  21. end
  22. UNINIT_ERROR = ArgumentError
  23. def test_s_allocate
  24. s = StringScanner.allocate
  25. assert_equal '#<StringScanner (uninitialized)>', s.inspect.sub(/StringScanner_C/, 'StringScanner')
  26. assert_raise(UNINIT_ERROR) { s.eos? }
  27. assert_raise(UNINIT_ERROR) { s.scan(/a/) }
  28. s.string = 'test'
  29. assert_equal '#<StringScanner 0/4 @ "test">', s.inspect.sub(/StringScanner_C/, 'StringScanner')
  30. assert_nothing_raised(UNINIT_ERROR) { s.eos? }
  31. assert_equal false, s.eos?
  32. end
  33. def test_s_mustc
  34. assert_nothing_raised(NotImplementedError) {
  35. StringScanner.must_C_version
  36. }
  37. end
  38. def test_dup
  39. s = create_string_scanner('test string')
  40. d = s.dup
  41. assert_equal s.inspect, d.inspect
  42. assert_equal s.string, d.string
  43. assert_equal s.pos, d.pos
  44. assert_equal s.matched?, d.matched?
  45. assert_equal s.eos?, d.eos?
  46. s = create_string_scanner('test string')
  47. s.scan(/test/)
  48. d = s.dup
  49. assert_equal s.inspect, d.inspect
  50. assert_equal s.string, d.string
  51. assert_equal s.pos, d.pos
  52. assert_equal s.matched?, d.matched?
  53. assert_equal s.eos?, d.eos?
  54. s = create_string_scanner('test string')
  55. s.scan(/test/)
  56. s.scan(/NOT MATCH/)
  57. d = s.dup
  58. assert_equal s.inspect, d.inspect
  59. assert_equal s.string, d.string
  60. assert_equal s.pos, d.pos
  61. assert_equal s.matched?, d.matched?
  62. assert_equal s.eos?, d.eos?
  63. s = create_string_scanner('test string')
  64. s.terminate
  65. d = s.dup
  66. assert_equal s.inspect, d.inspect
  67. assert_equal s.string, d.string
  68. assert_equal s.pos, d.pos
  69. assert_equal s.matched?, d.matched?
  70. assert_equal s.eos?, d.eos?
  71. end
  72. def test_const_Version
  73. assert_instance_of String, StringScanner::Version
  74. assert_equal true, StringScanner::Version.frozen?
  75. end
  76. def test_const_Id
  77. assert_instance_of String, StringScanner::Id
  78. assert_equal true, StringScanner::Id.frozen?
  79. end
  80. def test_inspect
  81. str = 'test string'.dup
  82. s = create_string_scanner(str, false)
  83. assert_instance_of String, s.inspect
  84. assert_equal s.inspect, s.inspect
  85. assert_equal '#<StringScanner 0/11 @ "test ...">', s.inspect.sub(/StringScanner_C/, 'StringScanner')
  86. s.get_byte
  87. assert_equal '#<StringScanner 1/11 "t" @ "est s...">', s.inspect.sub(/StringScanner_C/, 'StringScanner')
  88. s = create_string_scanner("\n")
  89. assert_equal '#<StringScanner 0/1 @ "\n">', s.inspect
  90. end
  91. def test_eos?
  92. s = create_string_scanner('test string')
  93. assert_equal false, s.eos?
  94. assert_equal false, s.eos?
  95. s.scan(/\w+/)
  96. assert_equal false, s.eos?
  97. assert_equal false, s.eos?
  98. s.scan(/\s+/)
  99. s.scan(/\w+/)
  100. assert_equal true, s.eos?
  101. assert_equal true, s.eos?
  102. s.scan(/\w+/)
  103. assert_equal true, s.eos?
  104. s = create_string_scanner('test'.dup)
  105. s.scan(/te/)
  106. s.string.replace ''
  107. assert_equal true, s.eos?
  108. end
  109. def test_bol?
  110. s = create_string_scanner("a\nbbb\n\ncccc\nddd\r\neee")
  111. assert_equal true, s.bol?
  112. assert_equal true, s.bol?
  113. s.scan(/a/)
  114. assert_equal false, s.bol?
  115. assert_equal false, s.bol?
  116. s.scan(/\n/)
  117. assert_equal true, s.bol?
  118. s.scan(/b/)
  119. assert_equal false, s.bol?
  120. s.scan(/b/)
  121. assert_equal false, s.bol?
  122. s.scan(/b/)
  123. assert_equal false, s.bol?
  124. s.scan(/\n/)
  125. assert_equal true, s.bol?
  126. s.unscan
  127. assert_equal false, s.bol?
  128. s.scan(/\n/)
  129. s.scan(/\n/)
  130. assert_equal true, s.bol?
  131. s.scan(/c+\n/)
  132. assert_equal true, s.bol?
  133. s.scan(/d+\r\n/)
  134. assert_equal true, s.bol?
  135. s.scan(/e+/)
  136. assert_equal false, s.bol?
  137. end
  138. def test_string
  139. s = create_string_scanner('test')
  140. assert_equal 'test', s.string
  141. s.string = 'a'
  142. assert_equal 'a', s.string
  143. s.scan(/a/)
  144. s.string = 'b'
  145. assert_equal 0, s.pos
  146. end
  147. def test_string_set_is_equal
  148. name = 'tenderlove'
  149. s = create_string_scanner(name)
  150. assert_equal name.object_id, s.string.object_id
  151. s.string = name
  152. assert_equal name.object_id, s.string.object_id
  153. end
  154. def test_string_append
  155. s = create_string_scanner('tender'.dup)
  156. s << 'love'
  157. assert_equal 'tenderlove', s.string
  158. s.string = 'tender'.dup
  159. s << 'love'
  160. assert_equal 'tenderlove', s.string
  161. end
  162. def test_pos
  163. s = create_string_scanner('test string')
  164. assert_equal 0, s.pos
  165. s.get_byte
  166. assert_equal 1, s.pos
  167. s.get_byte
  168. assert_equal 2, s.pos
  169. s.terminate
  170. assert_equal 11, s.pos
  171. end
  172. def test_pos_unicode
  173. s = create_string_scanner("abcädeföghi")
  174. assert_equal 0, s.charpos
  175. assert_equal "abcä", s.scan_until(/ä/)
  176. assert_equal 4, s.charpos
  177. assert_equal "defö", s.scan_until(/ö/)
  178. assert_equal 8, s.charpos
  179. s.terminate
  180. assert_equal 11, s.charpos
  181. end
  182. def test_concat
  183. s = create_string_scanner('a'.dup)
  184. s.scan(/a/)
  185. s.concat 'b'
  186. assert_equal false, s.eos?
  187. assert_equal 'b', s.scan(/b/)
  188. assert_equal true, s.eos?
  189. s.concat 'c'
  190. assert_equal false, s.eos?
  191. assert_equal 'c', s.scan(/c/)
  192. assert_equal true, s.eos?
  193. end
  194. def test_scan
  195. s = create_string_scanner('stra strb strc', true)
  196. tmp = s.scan(/\w+/)
  197. assert_equal 'stra', tmp
  198. tmp = s.scan(/\s+/)
  199. assert_equal ' ', tmp
  200. assert_equal 'strb', s.scan(/\w+/)
  201. assert_equal ' ', s.scan(/\s+/)
  202. tmp = s.scan(/\w+/)
  203. assert_equal 'strc', tmp
  204. assert_nil s.scan(/\w+/)
  205. assert_nil s.scan(/\w+/)
  206. str = 'stra strb strc'.dup
  207. s = create_string_scanner(str, false)
  208. tmp = s.scan(/\w+/)
  209. assert_equal 'stra', tmp
  210. tmp = s.scan(/\s+/)
  211. assert_equal ' ', tmp
  212. assert_equal 'strb', s.scan(/\w+/)
  213. assert_equal ' ', s.scan(/\s+/)
  214. tmp = s.scan(/\w+/)
  215. assert_equal 'strc', tmp
  216. assert_nil s.scan(/\w+/)
  217. assert_nil s.scan(/\w+/)
  218. s = create_string_scanner('test'.dup)
  219. s.scan(/te/)
  220. # This assumes #string does not duplicate string,
  221. # but it is implementation specific issue.
  222. # DO NOT RELY ON THIS FEATURE.
  223. s.string.replace ''
  224. # unspecified: assert_equal 2, s.pos
  225. assert_equal nil, s.scan(/test/)
  226. # [ruby-bugs:4361]
  227. s = create_string_scanner("")
  228. assert_equal "", s.scan(//)
  229. assert_equal "", s.scan(//)
  230. end
  231. def test_scan_string
  232. s = create_string_scanner('stra strb strc')
  233. assert_equal 'str', s.scan('str')
  234. assert_equal 'str', s[0]
  235. assert_equal 3, s.pos
  236. assert_equal 'a ', s.scan('a ')
  237. str = 'stra strb strc'.dup
  238. s = create_string_scanner(str, false)
  239. matched = s.scan('str')
  240. assert_equal 'str', matched
  241. end
  242. def test_skip
  243. s = create_string_scanner('stra strb strc', true)
  244. assert_equal 4, s.skip(/\w+/)
  245. assert_equal 1, s.skip(/\s+/)
  246. assert_equal 4, s.skip(/\w+/)
  247. assert_equal 1, s.skip(/\s+/)
  248. assert_equal 4, s.skip(/\w+/)
  249. assert_nil s.skip(/\w+/)
  250. assert_nil s.skip(/\s+/)
  251. assert_equal true, s.eos?
  252. s = create_string_scanner('test'.dup)
  253. s.scan(/te/)
  254. s.string.replace ''
  255. assert_equal nil, s.skip(/./)
  256. # [ruby-bugs:4361]
  257. s = create_string_scanner("")
  258. assert_equal 0, s.skip(//)
  259. assert_equal 0, s.skip(//)
  260. end
  261. def test_skip_with_begenning_of_string_anchor_match
  262. s = create_string_scanner("a\nb")
  263. assert_equal 2, s.skip(/a\n/)
  264. assert_equal 1, s.skip(/\Ab/)
  265. end
  266. def test_skip_with_begenning_of_line_anchor_match
  267. s = create_string_scanner("a\nbc")
  268. assert_equal 2, s.skip(/a\n/)
  269. assert_equal 1, s.skip(/^b/)
  270. assert_equal 1, s.skip(/^c/)
  271. end
  272. def test_getch
  273. s = create_string_scanner('abcde')
  274. assert_equal 'a', s.getch
  275. assert_equal 'b', s.getch
  276. assert_equal 'c', s.getch
  277. assert_equal 'd', s.getch
  278. assert_equal 'e', s.getch
  279. assert_nil s.getch
  280. s = create_string_scanner("\244\242".dup.force_encoding("euc-jp"))
  281. assert_equal "\244\242".dup.force_encoding("euc-jp"), s.getch
  282. assert_nil s.getch
  283. s = create_string_scanner('test'.dup)
  284. s.scan(/te/)
  285. s.string.replace ''
  286. assert_equal nil, s.getch
  287. end
  288. def test_get_byte
  289. s = create_string_scanner('abcde')
  290. assert_equal 'a', s.get_byte
  291. assert_equal 'b', s.get_byte
  292. assert_equal 'c', s.get_byte
  293. assert_equal 'd', s.get_byte
  294. assert_equal 'e', s.get_byte
  295. assert_nil s.get_byte
  296. assert_nil s.get_byte
  297. s = create_string_scanner("\244\242".dup.force_encoding("euc-jp"))
  298. assert_equal "\244".dup.force_encoding("euc-jp"), s.get_byte
  299. assert_equal "\242".dup.force_encoding("euc-jp"), s.get_byte
  300. assert_nil s.get_byte
  301. s = create_string_scanner('test'.dup)
  302. s.scan(/te/)
  303. s.string.replace ''
  304. assert_equal nil, s.get_byte
  305. end
  306. def test_matched
  307. s = create_string_scanner('stra strb strc')
  308. s.scan(/\w+/)
  309. assert_equal 'stra', s.matched
  310. s.scan(/\s+/)
  311. assert_equal ' ', s.matched
  312. s.scan('st')
  313. assert_equal 'st', s.matched
  314. s.scan(/\w+/)
  315. assert_equal 'rb', s.matched
  316. s.scan(/\s+/)
  317. assert_equal ' ', s.matched
  318. s.scan(/\w+/)
  319. assert_equal 'strc', s.matched
  320. s.scan(/\w+/)
  321. assert_nil s.matched
  322. s.getch
  323. assert_nil s.matched
  324. s = create_string_scanner('stra strb strc')
  325. s.getch
  326. assert_equal 's', s.matched
  327. s.get_byte
  328. assert_equal 't', s.matched
  329. assert_equal 't', s.matched
  330. end
  331. def test_AREF
  332. s = create_string_scanner('stra strb strc')
  333. s.scan(/\w+/)
  334. assert_nil s[-2]
  335. assert_equal 'stra', s[-1]
  336. assert_equal 'stra', s[0]
  337. assert_nil s[1]
  338. assert_raise(IndexError) { s[:c] }
  339. assert_raise(IndexError) { s['c'] }
  340. s.skip(/\s+/)
  341. assert_nil s[-2]
  342. assert_equal ' ', s[-1]
  343. assert_equal ' ', s[0]
  344. assert_nil s[1]
  345. s.scan(/(s)t(r)b/)
  346. assert_nil s[-100]
  347. assert_nil s[-4]
  348. assert_equal 'strb', s[-3]
  349. assert_equal 's', s[-2]
  350. assert_equal 'r', s[-1]
  351. assert_equal 'strb', s[0]
  352. assert_equal 's', s[1]
  353. assert_equal 'r', s[2]
  354. assert_nil s[3]
  355. assert_nil s[100]
  356. s.scan(/\s+/)
  357. s.getch
  358. assert_nil s[-2]
  359. assert_equal 's', s[-1]
  360. assert_equal 's', s[0]
  361. assert_nil s[1]
  362. s.get_byte
  363. assert_nil s[-2]
  364. assert_equal 't', s[-1]
  365. assert_equal 't', s[0]
  366. assert_nil s[1]
  367. s.scan(/.*/)
  368. s.scan(/./)
  369. assert_nil s[0]
  370. assert_nil s[0]
  371. s = create_string_scanner("\244\242".dup.force_encoding("euc-jp"))
  372. s.getch
  373. assert_equal "\244\242".dup.force_encoding("euc-jp"), s[0]
  374. s = create_string_scanner("foo bar baz")
  375. s.scan(/(?<a>\w+) (?<b>\w+) (\w+)/)
  376. assert_equal 'foo', s[1]
  377. assert_equal 'bar', s[2]
  378. assert_nil s[3]
  379. assert_equal 'foo', s[:a]
  380. assert_equal 'bar', s[:b]
  381. assert_raise(IndexError) { s[:c] }
  382. assert_equal 'foo', s['a']
  383. assert_equal 'bar', s['b']
  384. assert_raise(IndexError) { s['c'] }
  385. assert_raise_with_message(IndexError, /\u{30c6 30b9 30c8}/) { s["\u{30c6 30b9 30c8}"] }
  386. end
  387. def test_pre_match
  388. s = create_string_scanner('a b c d e')
  389. s.scan(/\w/)
  390. assert_equal '', s.pre_match
  391. s.skip(/\s/)
  392. assert_equal 'a', s.pre_match
  393. s.scan('b')
  394. assert_equal 'a ', s.pre_match
  395. s.scan_until(/c/)
  396. assert_equal 'a b ', s.pre_match
  397. s.getch
  398. assert_equal 'a b c', s.pre_match
  399. s.get_byte
  400. assert_equal 'a b c ', s.pre_match
  401. s.get_byte
  402. assert_equal 'a b c d', s.pre_match
  403. s.scan(/never match/)
  404. assert_nil s.pre_match
  405. end
  406. def test_post_match
  407. s = create_string_scanner('a b c d e')
  408. s.scan(/\w/)
  409. assert_equal ' b c d e', s.post_match
  410. s.skip(/\s/)
  411. assert_equal 'b c d e', s.post_match
  412. s.scan('b')
  413. assert_equal ' c d e', s.post_match
  414. s.scan_until(/c/)
  415. assert_equal ' d e', s.post_match
  416. s.getch
  417. assert_equal 'd e', s.post_match
  418. s.get_byte
  419. assert_equal ' e', s.post_match
  420. s.get_byte
  421. assert_equal 'e', s.post_match
  422. s.scan(/never match/)
  423. assert_nil s.post_match
  424. s.scan(/./)
  425. assert_equal '', s.post_match
  426. s.scan(/./)
  427. assert_nil s.post_match
  428. end
  429. def test_terminate
  430. s = create_string_scanner('ssss')
  431. s.getch
  432. s.terminate
  433. assert_equal true, s.eos?
  434. s.terminate
  435. assert_equal true, s.eos?
  436. end
  437. def test_reset
  438. s = create_string_scanner('ssss')
  439. s.getch
  440. s.reset
  441. assert_equal 0, s.pos
  442. s.scan(/\w+/)
  443. s.reset
  444. assert_equal 0, s.pos
  445. s.reset
  446. assert_equal 0, s.pos
  447. end
  448. def test_matched_size
  449. s = create_string_scanner('test string')
  450. assert_nil s.matched_size
  451. s.scan(/test/)
  452. assert_equal 4, s.matched_size
  453. assert_equal 4, s.matched_size
  454. s.scan(//)
  455. assert_equal 0, s.matched_size
  456. s.scan(/x/)
  457. assert_nil s.matched_size
  458. assert_nil s.matched_size
  459. s.terminate
  460. assert_nil s.matched_size
  461. s = create_string_scanner('test string')
  462. assert_nil s.matched_size
  463. s.scan(/test/)
  464. assert_equal 4, s.matched_size
  465. s.terminate
  466. assert_nil s.matched_size
  467. end
  468. def test_encoding
  469. ss = create_string_scanner("\xA1\xA2".dup.force_encoding("euc-jp"))
  470. assert_equal(Encoding::EUC_JP, ss.scan(/./e).encoding)
  471. end
  472. def test_encoding_string
  473. str = "\xA1\xA2".dup.force_encoding("euc-jp")
  474. ss = create_string_scanner(str)
  475. assert_equal(str.dup, ss.scan(str.dup))
  476. end
  477. def test_invalid_encoding_string
  478. str = "\xA1\xA2".dup.force_encoding("euc-jp")
  479. ss = create_string_scanner(str)
  480. assert_raise(Encoding::CompatibilityError) do
  481. ss.scan(str.encode("UTF-8"))
  482. end
  483. end
  484. def test_generic_regexp
  485. ss = create_string_scanner("\xA1\xA2".dup.force_encoding("euc-jp"))
  486. t = ss.scan(/./)
  487. assert_equal("\xa1\xa2".dup.force_encoding("euc-jp"), t)
  488. end
  489. def test_set_pos
  490. s = create_string_scanner("test string")
  491. s.pos = 7
  492. assert_equal("ring", s.rest)
  493. end
  494. def test_match_p
  495. s = create_string_scanner("test string")
  496. assert_equal(4, s.match?(/\w+/))
  497. assert_equal(4, s.match?(/\w+/))
  498. assert_equal(nil, s.match?(/\s+/))
  499. end
  500. def test_check
  501. s = create_string_scanner("Foo Bar Baz")
  502. assert_equal("Foo", s.check(/Foo/))
  503. assert_equal(0, s.pos)
  504. assert_equal("Foo", s.matched)
  505. assert_equal(nil, s.check(/Bar/))
  506. assert_equal(nil, s.matched)
  507. end
  508. def test_scan_full
  509. s = create_string_scanner("Foo Bar Baz")
  510. assert_equal(4, s.scan_full(/Foo /, false, false))
  511. assert_equal(0, s.pos)
  512. assert_equal(nil, s.scan_full(/Baz/, false, false))
  513. assert_equal("Foo ", s.scan_full(/Foo /, false, true))
  514. assert_equal(0, s.pos)
  515. assert_equal(nil, s.scan_full(/Baz/, false, false))
  516. assert_equal(4, s.scan_full(/Foo /, true, false))
  517. assert_equal(4, s.pos)
  518. assert_equal(nil, s.scan_full(/Baz /, false, false))
  519. assert_equal("Bar ", s.scan_full(/Bar /, true, true))
  520. assert_equal(8, s.pos)
  521. assert_equal(nil, s.scan_full(/az/, false, false))
  522. end
  523. def test_exist_p
  524. s = create_string_scanner("test string")
  525. assert_equal(3, s.exist?(/s/))
  526. assert_equal(0, s.pos)
  527. s.scan(/test/)
  528. assert_equal(2, s.exist?(/s/))
  529. assert_equal(4, s.pos)
  530. assert_equal(nil, s.exist?(/e/))
  531. end
  532. def test_exist_p_string
  533. s = create_string_scanner("test string")
  534. assert_raise(TypeError) do
  535. s.exist?(" ")
  536. end
  537. end
  538. def test_skip_until
  539. s = create_string_scanner("Foo Bar Baz")
  540. assert_equal(3, s.skip_until(/Foo/))
  541. assert_equal(3, s.pos)
  542. assert_equal(4, s.skip_until(/Bar/))
  543. assert_equal(7, s.pos)
  544. assert_equal(nil, s.skip_until(/Qux/))
  545. end
  546. def test_check_until
  547. s = create_string_scanner("Foo Bar Baz")
  548. assert_equal("Foo", s.check_until(/Foo/))
  549. assert_equal(0, s.pos)
  550. assert_equal("Foo Bar", s.check_until(/Bar/))
  551. assert_equal(0, s.pos)
  552. assert_equal(nil, s.check_until(/Qux/))
  553. end
  554. def test_search_full
  555. s = create_string_scanner("Foo Bar Baz")
  556. assert_equal(8, s.search_full(/Bar /, false, false))
  557. assert_equal(0, s.pos)
  558. assert_equal("Foo Bar ", s.search_full(/Bar /, false, true))
  559. assert_equal(0, s.pos)
  560. assert_equal(8, s.search_full(/Bar /, true, false))
  561. assert_equal(8, s.pos)
  562. assert_equal("Baz", s.search_full(/az/, true, true))
  563. assert_equal(11, s.pos)
  564. end
  565. def test_peek
  566. s = create_string_scanner("test string")
  567. assert_equal("test st", s.peek(7))
  568. assert_equal("test st", s.peek(7))
  569. s.scan(/test/)
  570. assert_equal(" stri", s.peek(5))
  571. assert_equal(" string", s.peek(10))
  572. s.scan(/ string/)
  573. assert_equal("", s.peek(10))
  574. end
  575. def test_unscan
  576. s = create_string_scanner('test string')
  577. assert_equal("test", s.scan(/\w+/))
  578. s.unscan
  579. assert_equal("te", s.scan(/../))
  580. assert_equal(nil, s.scan(/\d/))
  581. assert_raise(ScanError) { s.unscan }
  582. end
  583. def test_rest
  584. s = create_string_scanner('test string')
  585. assert_equal("test string", s.rest)
  586. s.scan(/test/)
  587. assert_equal(" string", s.rest)
  588. s.scan(/ string/)
  589. assert_equal("", s.rest)
  590. s.scan(/ string/)
  591. end
  592. def test_rest_size
  593. s = create_string_scanner('test string')
  594. assert_equal(11, s.rest_size)
  595. s.scan(/test/)
  596. assert_equal(7, s.rest_size)
  597. s.scan(/ string/)
  598. assert_equal(0, s.rest_size)
  599. s.scan(/ string/)
  600. end
  601. def test_inspect2
  602. s = create_string_scanner('test string test')
  603. s.scan(/test strin/)
  604. assert_equal('#<StringScanner 10/16 "...strin" @ "g tes...">', s.inspect)
  605. end
  606. def test_aref_without_regex
  607. s = create_string_scanner('abc')
  608. s.get_byte
  609. assert_nil(s[:c])
  610. assert_nil(s["c"])
  611. s.getch
  612. assert_nil(s[:c])
  613. assert_nil(s["c"])
  614. end
  615. def test_size
  616. s = create_string_scanner("Fri Dec 12 1975 14:39")
  617. s.scan(/(\w+) (\w+) (\d+) /)
  618. assert_equal(4, s.size)
  619. end
  620. def test_captures
  621. s = create_string_scanner("Timestamp: Fri Dec 12 1975 14:39")
  622. s.scan("Timestamp: ")
  623. s.scan(/(\w+) (\w+) (\d+) /)
  624. assert_equal(["Fri", "Dec", "12"], s.captures)
  625. s.scan(/(\w+) (\w+) (\d+) /)
  626. assert_nil(s.captures)
  627. end
  628. def test_values_at
  629. s = create_string_scanner("Timestamp: Fri Dec 12 1975 14:39")
  630. s.scan("Timestamp: ")
  631. s.scan(/(\w+) (\w+) (\d+) /)
  632. assert_equal(["Fri Dec 12 ", "12", nil, "Dec"], s.values_at(0, -1, 5, 2))
  633. s.scan(/(\w+) (\w+) (\d+) /)
  634. assert_nil(s.values_at(0, -1, 5, 2))
  635. end
  636. def test_fixed_anchor_true
  637. assert_equal(true, StringScanner.new("a", fixed_anchor: true).fixed_anchor?)
  638. end
  639. def test_fixed_anchor_false
  640. assert_equal(false, StringScanner.new("a").fixed_anchor?)
  641. assert_equal(false, StringScanner.new("a", true).fixed_anchor?)
  642. assert_equal(false, StringScanner.new("a", false).fixed_anchor?)
  643. assert_equal(false, StringScanner.new("a", {}).fixed_anchor?)
  644. assert_equal(false, StringScanner.new("a", fixed_anchor: nil).fixed_anchor?)
  645. assert_equal(false, StringScanner.new("a", fixed_anchor: false).fixed_anchor?)
  646. end
  647. end
  648. class TestStringScannerFixedAnchor < TestStringScanner
  649. def create_string_scanner(string, *args)
  650. StringScanner.new(string, fixed_anchor: true)
  651. end
  652. def test_skip_with_begenning_of_string_anchor_match
  653. s = create_string_scanner("a")
  654. assert_equal 1, s.skip(/\Aa/)
  655. end
  656. def test_skip_with_begenning_of_string_anchor_not_match
  657. s = create_string_scanner("a\nb")
  658. assert_equal 2, s.skip(/a\n/)
  659. assert_nil s.skip(/\Ab/)
  660. end
  661. def test_skip_with_begenning_of_line_anchor_match
  662. s = create_string_scanner("a\nb")
  663. assert_equal 2, s.skip(/a\n/)
  664. assert_equal 1, s.skip(/^b/)
  665. end
  666. def test_skip_with_begenning_of_line_anchor_not_match
  667. s = create_string_scanner("ab")
  668. assert_equal 1, s.skip(/a/)
  669. assert_nil s.skip(/^b/)
  670. end
  671. end