te3270 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,179 @@
1
+ require 'spec_helper'
2
+
3
+ describe TE3270::Emulators::X3270 do
4
+
5
+ let(:x3270) { TE3270::Emulators::X3270.new }
6
+
7
+ before(:each) do
8
+ @x3270_io = double('x3270_io')
9
+ Open3.stub(:popen2e).and_return [@x3270_io,@x3270_io,nil]
10
+ x3270.instance_variable_set(:@executable_command, "the_command")
11
+ x3270.instance_variable_set(:@host, "the_host")
12
+ @x3270_io.stub(:print)
13
+ @x3270_io.stub(:flush)
14
+ @x3270_io.stub(:gets).and_return('goo','ok')
15
+ end
16
+
17
+ describe "global behaviors" do
18
+ it 'should open pipe to x3270' do
19
+ Open3.should_receive(:popen2e).and_return(x3270_system)
20
+ x3270.connect
21
+ end
22
+
23
+ it 'should take the timeout value from a block' do
24
+ x3270.should_receive(:executable_command=).with('path to the x3270 executable')
25
+ x3270.connect do |platform|
26
+ platform.executable_command = 'path to the x3270 executable'
27
+ end
28
+ end
29
+
30
+ it 'should take the host to connect to from a block' do
31
+ x3270.should_receive(:host=).with('name of host to connect to')
32
+ x3270.connect do |platform|
33
+ platform.host = 'name of host to connect to'
34
+ end
35
+ end
36
+
37
+ it 'should take the max timeout value from a block' do
38
+ x3270.should_receive(:max_wait_time=).with(42)
39
+ x3270.connect do |platform|
40
+ platform.max_wait_time = 42
41
+ end
42
+ end
43
+
44
+ it 'should display an error when the path to the executable is not set' do
45
+ x3270.instance_variable_set(:@executable_command, nil)
46
+ expect { x3270.connect }.to raise_error('The executable command must be set in a block when calling connect with the X3270 emulator.')
47
+ end
48
+
49
+ it 'should display an error when the host is not set' do
50
+ x3270.instance_variable_set(:@host, nil)
51
+ expect { x3270.connect }.to raise_error('The host must be set in a block when calling connect with the X3270 emulator.')
52
+ end
53
+
54
+ it 'should default to max_wait_time being 10 when not provided' do
55
+ #x3270.should_receive(:max_wait_time=).with(10)
56
+ x3270.connect
57
+ x3270.instance_variable_get(:@max_wait_time).should eq(10)
58
+ end
59
+
60
+ it 'should display an error when cannot popen supplied x3270 program' do
61
+ Open3.stub(:popen2e).and_raise('darn it, popen failed')
62
+ expect { x3270.connect }.to raise_error( "Could not start x3270 'the_command': darn it, popen failed")
63
+ end
64
+
65
+ it 'should close input and output on pipe when disconnect called' do
66
+ io = double('IO')
67
+ Open3.stub(:popen2e).and_return([io,io,io])
68
+ io.should_receive(:close).twice
69
+ x3270.connect
70
+ x3270.disconnect
71
+ end
72
+ end
73
+
74
+ describe "interacting with text fields" do
75
+ it 'should get the value from the screen' do
76
+ @x3270_io.should_receive(:print).with("ascii(0,1,7)\n")
77
+ @x3270_io.stub(:gets).and_return('data: blah','goo','ok')
78
+ #@x3270_io.stub(:gets).and_return('ok')
79
+ x3270.connect
80
+ x3270.get_string(1, 2, 7).should == 'blah'
81
+ end
82
+
83
+ it 'should put a value on the screen' do
84
+ @x3270_io.should_receive(:print).with("MoveCursor(14,55)\n")
85
+ @x3270_io.should_receive(:print).with('string "blah"'+"\n")
86
+ x3270.connect
87
+ x3270.put_string('blah', 15, 56)
88
+ end
89
+
90
+ it 'should put proper escape value on the screen' do
91
+ @x3270_io.should_receive(:print).with("MoveCursor(14,55)\n")
92
+ @x3270_io.should_receive(:print).with('string "ab\"cd"'+"\n")
93
+ x3270.connect
94
+ x3270.put_string('ab"cd', 15, 56)
95
+ end
96
+ end
97
+
98
+ describe "interacting with the screen" do
99
+ it 'should know how to send function keys' do
100
+ @x3270_io.should_receive(:print).with("Home\n")
101
+ @x3270_io.should_receive(:print).with("wait(output)\n")
102
+ x3270.connect
103
+ x3270.send_keys(TE3270.Home)
104
+ end
105
+
106
+ it 'should know how to send program function keys ' do
107
+ @x3270_io.should_receive(:print).with("Pf(13)\n")
108
+ @x3270_io.should_receive(:print).with("wait(output)\n")
109
+ x3270.connect
110
+ x3270.send_keys(TE3270.Pf13)
111
+ end
112
+
113
+ it 'should know how to send program attention keys ' do
114
+ @x3270_io.should_receive(:print).with("Pa(2)\n")
115
+ @x3270_io.should_receive(:print).with("wait(output)\n")
116
+ x3270.connect
117
+ x3270.send_keys(TE3270.Pa2)
118
+ end
119
+
120
+ it 'should wait for a string to appear' do
121
+ @x3270_io.should_receive(:print).with("ascii(2,9,6)\n")
122
+ @x3270_io.should_receive(:gets).and_return('data: string','goo','ok')
123
+ x3270.should_not_receive(:sleep)
124
+ x3270.connect
125
+ x3270.wait_for_string('string', 3, 10)
126
+ end
127
+
128
+ it 'should timeout when wait for a string does not appear' do
129
+ @x3270_io.should_receive(:print).with("ascii(2,9,6)\n").exactly(20).times
130
+ @x3270_io.should_receive(:gets).and_return('data: stuff','goo','ok')
131
+ x3270.should_receive(:sleep).exactly(20).times
132
+ x3270.connect
133
+ x3270.wait_for_string('string', 3, 10)
134
+ end
135
+
136
+ it 'should find string after one sleep when waiting for a string to appear' do
137
+ @x3270_io.should_receive(:print).with("ascii(2,9,6)\n").twice
138
+ @x3270_io.should_receive(:gets).and_return('data: blah','goo','ok','data: string','goo','ok')
139
+ x3270.stub(:sleep).with(0.5).once
140
+ x3270.connect
141
+ x3270.wait_for_string('string', 3, 10)
142
+ end
143
+
144
+ it 'should wait for host' do
145
+ @x3270_io.should_receive(:print).with("Wait(10,Output)\n")
146
+ x3270.connect
147
+ x3270.wait_for_host(10)
148
+ end
149
+
150
+ it 'should wait until the cursor is at a position' do
151
+ @x3270_io.should_receive(:print).with("MoveCursor(5,8)\n")
152
+ x3270.connect
153
+ x3270.wait_until_cursor_at(6,9)
154
+ end
155
+
156
+ it 'should take screenshots' do
157
+ File.should_receive(:exists?).and_return(false)
158
+ File.should_not_receive(:delete)
159
+ @x3270_io.should_receive(:print).with("printtext(file,image.txt)\n")
160
+ x3270.connect
161
+ x3270.screenshot("image.txt")
162
+ end
163
+
164
+ it 'should delete existing file when taking screenshots' do
165
+ File.should_receive(:exists?).and_return(true)
166
+ File.should_receive(:delete)
167
+ @x3270_io.should_receive(:print).with("printtext(file,image.txt)\n")
168
+ x3270.connect
169
+ x3270.screenshot("image.txt")
170
+ end
171
+
172
+ it 'should get all text from screen' do
173
+ @x3270_io.should_receive(:print).with("ascii(0,0,1920)\n")
174
+ @x3270_io.should_receive(:gets).and_return('data: string','goo','ok')
175
+ x3270.connect
176
+ x3270.text.should == 'string'
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ class ScreenFactoryScreen
4
+ include TE3270
5
+ end
6
+
7
+ class WorldSuper
8
+ attr_reader :super_called
9
+ def on(screen_class, &block)
10
+ @super_called = true
11
+ end
12
+ end
13
+
14
+ class World < WorldSuper
15
+ include TE3270::ScreenFactory
16
+ end
17
+
18
+ describe TE3270::ScreenFactory do
19
+
20
+ let(:world) { World.new }
21
+
22
+ it 'should create a new screen object' do
23
+ emulator = double('platform')
24
+ world.instance_variable_set('@emulator', emulator)
25
+ world.on(ScreenFactoryScreen).should be_instance_of ScreenFactoryScreen
26
+ end
27
+
28
+ it 'should create a new screen object and execute a block' do
29
+ emulator = double('platform')
30
+ world.instance_variable_set('@emulator', emulator)
31
+ world.on(ScreenFactoryScreen) do |page|
32
+ page.should be_instance_of ScreenFactoryScreen
33
+ end
34
+ end
35
+
36
+ it 'should raise an error when an @emulator instance variable does not exist' do
37
+ expect { world.on(ScreenFactoryScreen) }.to raise_error("@emulator instance variable must be available to use the ScreenFactory methods")
38
+ end
39
+
40
+ it 'should pass control to super if passed a class that does not include TE3270' do
41
+ class NoTE
42
+ end
43
+ emulator = double('platform')
44
+ world.instance_variable_set('@emulator', emulator)
45
+ world.on(NoTE)
46
+ world.super_called.should be true
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ class ScreenPopulatorScreen
4
+ include TE3270
5
+
6
+ text_field(:editable, 1, 2, 10, true)
7
+ text_field(:read_only, 2, 3, 12, false)
8
+ end
9
+
10
+ describe TE3270::ScreenPopulator do
11
+
12
+ let(:platform) { double('platform') }
13
+ let(:screen_object) { ScreenPopulatorScreen.new platform }
14
+
15
+ it 'should set a value in a text field' do
16
+ platform.should_receive(:put_string).with('the_value', 1, 2)
17
+ screen_object.populate_screen_with editable: 'the_value'
18
+ end
19
+
20
+ it 'should not set a value when a text field is read only' do
21
+ platform.should_not_receive(:put_string)
22
+ screen_object.populate_screen_with read_only: 'the_value'
23
+ end
24
+
25
+ it 'should attempt to set all values from the provided Hash' do
26
+ platform.should_receive(:put_string).with('the_value', 1, 2)
27
+ platform.should_not_receive(:put_string).with('read_only', 2, 3)
28
+ screen_object.populate_screen_with(editable: 'the_value', read_only: 'read_only')
29
+ end
30
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ class TEScreenObject
4
+ include TE3270
5
+
6
+ attr_reader :initialize_screen
7
+
8
+ def initialize_screen
9
+ @initialize_screen = true
10
+ end
11
+ end
12
+
13
+ describe TE3270 do
14
+ let(:platform) { double('platform') }
15
+ let(:screen_object) { TEScreenObject.new platform }
16
+
17
+ before(:each) do
18
+ screen_object.stub(:platform).and_return platform
19
+ end
20
+
21
+ describe "interacting with the platform" do
22
+ it 'should use the platform to connect to an emulator' do
23
+ platform.should_receive(:connect)
24
+ screen_object.connect
25
+ end
26
+
27
+ it 'should use the platform to disconnect from an emulator' do
28
+ platform.should_receive(:disconnect)
29
+ screen_object.disconnect
30
+ end
31
+
32
+ it 'should use the platform to send keys to the screen' do
33
+ platform.should_receive(:send_keys).with('<Clear>')
34
+ screen_object.send_keys(TE3270.Clear)
35
+ end
36
+
37
+ it 'should use the platform to wait for a string to appear on the screen' do
38
+ platform.should_receive(:wait_for_string).with('The String', 2, 4)
39
+ screen_object.wait_for_string('The String', 2, 4)
40
+ end
41
+
42
+ it 'should use the platform to wait for the host to be quiet' do
43
+ platform.should_receive(:wait_for_host).with(4)
44
+ screen_object.wait_for_host(4)
45
+ end
46
+
47
+ it 'should default to five seconds when waiting for the host' do
48
+ platform.should_receive(:wait_for_host).with(5)
49
+ screen_object.wait_for_host
50
+ end
51
+
52
+ it 'should use the platform to wait until the cursor at a specific position' do
53
+ platform.should_receive(:wait_until_cursor_at).with(10, 10)
54
+ screen_object.wait_until_cursor_at(10, 10)
55
+ end
56
+
57
+ it 'should use the platform to take a screenshot of the screen' do
58
+ platform.should_receive(:screenshot).with('image.png')
59
+ screen_object.screenshot('image.png')
60
+ end
61
+
62
+ it 'should use the platform to get the text for the entire screen' do
63
+ platform.should_receive(:text).and_return('123abc')
64
+ screen_object.text.should == '123abc'
65
+ end
66
+ end
67
+
68
+ describe "module functionality" do
69
+ it 'should know the function keys' do
70
+ TE3270.Clear.should == '<Clear>'
71
+ TE3270.Pf24.should == '<Pf24>'
72
+ end
73
+
74
+ it 'should call initialize_screen if it exists' do
75
+ screen_object.initialize_screen.should be true
76
+ end
77
+
78
+ it 'should create an emulator and connect to terminal' do
79
+ TE3270::Emulators::Extra.should_receive(:new).and_return(platform)
80
+ platform.should_receive(:connect)
81
+ TE3270.emulator_for :extra
82
+ end
83
+
84
+ if Gem.win_platform?
85
+ it 'should accept a block when creating an emulator' do
86
+ WIN32OLE.stub(:new).and_return extra_system
87
+ extra_sessions.should_receive(:Open).with('blah.edp').and_return(extra_session)
88
+ TE3270.emulator_for :extra do |emulator|
89
+ emulator.session_file = 'blah.edp'
90
+ end
91
+ end
92
+ end
93
+
94
+ it 'should allow one to disconnect using the module' do
95
+ platform.should_receive(:disconnect)
96
+ TE3270.disconnect(platform)
97
+ end
98
+ end
99
+
100
+ end
@@ -0,0 +1,73 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'te3270'
4
+ if Gem.win_platform?
5
+ require 'win32ole'
6
+ require 'win32/screenshot'
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_with :rspec do |mocks|
11
+ mocks.syntax = :should
12
+ end
13
+ end
14
+
15
+ def extra_system
16
+ @extra_system ||= double('system')
17
+ @extra_system.stub(:Sessions).and_return extra_sessions
18
+ @extra_system.stub(:Version).and_return("0")
19
+ @extra_system
20
+ end
21
+
22
+ def extra_sessions
23
+ @extra_sessions ||= double('sessions')
24
+ @extra_sessions.stub(:Count).and_return 0
25
+ @extra_sessions.stub(:Open).and_return extra_session
26
+ @extra_sessions
27
+ end
28
+
29
+ def extra_session
30
+ @extra_session ||= double('session')
31
+ @extra_session.stub(:Screen).and_return extra_screen
32
+ @extra_session.stub(:WindowState=)
33
+ @extra_session.stub(:Visible=)
34
+ @extra_session
35
+ end
36
+
37
+ def extra_screen
38
+ @extra_screen ||= double('screen')
39
+ @extra_screen.stub(:SelectAll).and_return extra_area
40
+ @extra_screen
41
+ end
42
+
43
+ def extra_area
44
+ @extra_area ||= double('area')
45
+ @extra_area
46
+ end
47
+
48
+ def quick_system
49
+ @quick_system ||= double('quick_system')
50
+ @quick_system.stub(:ActiveSession).and_return quick_session
51
+ @quick_system.stub(:Visible=)
52
+ @quick_system
53
+ end
54
+
55
+ def quick_session
56
+ @quick_session ||= double('quick_session')
57
+ @quick_session.stub(:Screen).and_return quick_screen
58
+ @quick_session.stub(:Open)
59
+ @quick_session.stub(:Connect)
60
+ @quick_session.stub(:Server_Name=)
61
+ @quick_session.stub(:Connected).and_return true
62
+ @quick_session
63
+ end
64
+
65
+ def quick_screen
66
+ @quick_screen ||= double('screen')
67
+ @quick_screen
68
+ end
69
+
70
+ def x3270_system
71
+ @x3270_system ||= double('x3270_system')
72
+ @x3270_system
73
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'te3270/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "te3270"
8
+ spec.version = TE3270::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.authors = ["Jeffrey S. Morgan", "Nithin C. Reddy", "Glenn W. Waters"]
11
+ spec.email = ["jeff.morgan@leandog.com","nithinreddyc@gmail.com", "gwwaters@gmail.com"]
12
+ spec.description = %q{Automates a 3270 Terminal Emulator}
13
+ spec.summary = %q{Automates a 3270 Terminal Emulator}
14
+ spec.homepage = "http://github.com/cheezy/te3270"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+ spec.extensions = ["ext/mkrf_conf.rb"]
22
+
23
+ spec.add_dependency 'page_navigation', '>= 0.9'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ end
29
+