Module: KorInputJsonTest

Defined in:
lib/kor/input/json_test.rb

Constant Summary collapse

TEST_DATA =
[
  -> {
    io = StringIO.new("{\"foo\": 100, \"bar\": 200}\n{\"bar\": 500, \"baz\": 600}\n")
    Kor::Input::Json.new(io)
  },
  -> {
    io = StringIO.new('[{"foo": 100, "bar": 200}, {"bar": 500, "baz": 600}]')
    json = Kor::Input::Json.new(io)
    opt = OptionParser.new
    json.parse(opt)
    opt.parse(["--single"])
    json
  },
  -> {
    io = StringIO.new("{\"foo\": 100, \"bar\": 200}\n{\"bar\": 500, \"baz\": 600}\n")
    json = Kor::Input::Json.new(io)
    opt = OptionParser.new
    json.parse(opt)
    opt.parse(["--guess-time=0"])
    json
  },
]

Instance Method Summary collapse

Instance Method Details

#test_gets(t) ⇒ Object



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
# File 'lib/kor/input/json_test.rb', line 56

def test_gets(t)
  TEST_DATA.each do |pr|
    json = pr.call
    json.head

    body = json.gets
    expect = [100, 200, nil]
    if body != expect
      t.error("expect #{expect} got #{body}")
    end

    body = json.gets
    expect = [nil, 500, 600]
    if body != expect
      t.error("expect #{expect} got #{body}")
    end

    5.times do
      body = json.gets
      if body != nil
        t.error("expect nil got #{body}")
      end
    end
  end
end

#test_head(t) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/kor/input/json_test.rb', line 45

def test_head(t)
  TEST_DATA.each do |pr|
    json = pr.call
    head = json.head
    expect = %w(foo bar baz)
    if head != expect
      t.error("expect #{expect} got #{head}")
    end
  end
end

#test_initialize(t) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/kor/input/json_test.rb', line 4

def test_initialize(t)
  _, err = go { Kor::Input::Json.new }
  unless ArgumentError === err
    t.error("expect raise an ArgumentError got #{err.class}:#{err}")
  end

  _, err = go { Kor::Input::Json.new(nil) }
  if err != nil
    t.error("expect not raise an error got #{err.class}:#{err}")
  end
end