File: test_scanfblocks.rb

package info (click to toggle)
ruby2.3 2.3.3-1%2Bdeb9u8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 65,344 kB
  • sloc: ruby: 639,947; ansic: 317,772; xml: 25,445; yacc: 9,068; javascript: 6,648; lisp: 2,568; tcl: 949; makefile: 623; sh: 533; perl: 62; sed: 53; python: 47; awk: 36; asm: 35
file content (82 lines) | stat: -rw-r--r-- 1,760 bytes parent folder | download | duplicates (4)
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
# frozen_string_literal: false
# $Id$
#
# scanf for Ruby
#
# Some not very comprehensive tests of block behavior.


require 'test/unit'
require 'scanf'
require 'tmpdir'

class TestScanfBlock < Test::Unit::TestCase

  def setup
    @str = <<-EOS
    Beethoven  1770
    Bach       1685
    Handel     1685
    Scarlatti  1685
    Brahms     1833
    EOS
  end

alias set_up setup
  def test_str1
    res = @str.scanf("%s%d") { |name, year| "#{name} was born in #{year}." }
    assert_equal(res,
    [ "Beethoven was born in 1770.",
      "Bach was born in 1685.",
      "Handel was born in 1685.",
      "Scarlatti was born in 1685.",
      "Brahms was born in 1833." ])
  end

  def test_str2
    names = @str.scanf("%s%d") { |name, year| name.upcase }
    assert_equal(names, ["BEETHOVEN", "BACH", "HANDEL", "SCARLATTI", "BRAHMS"])
  end

  def test_str3
    assert_equal("".scanf("%d%f%s") {}, [])
  end

  def test_str4
    assert_equal("abc".scanf("%d%f%s") {}, [])
  end

  def test_str5
    assert_equal("abc".scanf("") {}, [])
  end

  def test_io1
    fn = "#{Dir.tmpdir}/iotest.dat.#{$$}"
    File.open(fn, "w") { |fh| fh.puts(@str) }
    fh = File.open(fn, "rb")
    res = fh.scanf("%s%d") { |name, year| "#{name} was born in #{year}." }

    assert_equal(
    [ "Beethoven was born in 1770.",
      "Bach was born in 1685.",
      "Handel was born in 1685.",
      "Scarlatti was born in 1685.",
      "Brahms was born in 1833." ],res)
    fh.close
  ensure
    File.delete(fn)
  end

  def test_io2
    fn = "#{Dir.tmpdir}/iotest.dat.#{$$}"
    File.open(fn, "w").close
    fh = File.open(fn,"rb")
    assert_equal(fh.scanf("") {}, [])
    fh.seek(0)
    assert_equal(fh.scanf("%d%f%s") {}, [])
    fh.close
  ensure
    File.delete(fn)
  end

end