|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe CodeBreaker::Parser do |
| 4 | + |
| 5 | + let(:code_snippet) { 'sum = "2".to_i + 3' } |
| 6 | + let(:output) { [:sum, :'=', String, :to_i, :+, Fixnum] } |
| 7 | + |
| 8 | + subject { CodeBreaker::Parser.new(code_snippet) } |
| 9 | + |
| 10 | + it { is_expected.to respond_to :run } |
| 11 | + it { is_expected.to respond_to :input } |
| 12 | + it { is_expected.to respond_to :output } |
| 13 | + |
| 14 | + describe 'input' do |
| 15 | + it 'returns the code snippet the parser was instanciated with' do |
| 16 | + expect(subject.input).to eq code_snippet |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + describe 'output' do |
| 21 | + it 'is an alias method for #run' do |
| 22 | + expect(subject.method(:output)).to eq subject.method(:run) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe '#run' do |
| 27 | + it 'returns an Array' do |
| 28 | + expect(subject.run).to be_an Array |
| 29 | + end |
| 30 | + |
| 31 | + context 'for code snippets defining a variable' do |
| 32 | + it 'returns the called classes and methods of the right hand side' do |
| 33 | + code_snippet = 'number = 1 + 2.3 - Rational(2,3) * Complex(0.4, 0.2)' |
| 34 | + expected_result = [:number, :'=', Fixnum, :+, Float, :-, Rational, :*, Complex] |
| 35 | + |
| 36 | + actual_result = CodeBreaker::Parser.new(code_snippet).run |
| 37 | + expect(actual_result).to eq expected_result |
| 38 | + end |
| 39 | + |
| 40 | + it 'returns a single item Array for only one statement' do |
| 41 | + code_snippet = '"3.14159265359"' |
| 42 | + expected_result = [String] |
| 43 | + |
| 44 | + actual_result = CodeBreaker::Parser.new(code_snippet).run |
| 45 | + expect(actual_result).to eq expected_result |
| 46 | + end |
| 47 | + |
| 48 | + it 'returns a nested Array for nested statements' do |
| 49 | + code_snippet = 'number = 1.2 * (Rational(2) - 1)' |
| 50 | + expected_result = [:number, :'=', Float, :*, [Rational, :-, Fixnum]] |
| 51 | + |
| 52 | + actual_result = CodeBreaker::Parser.new(code_snippet).run |
| 53 | + expect(actual_result).to eq expected_result |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context 'for code snippets not assigning a variable' do |
| 58 | + it 'returns the called classes and methods' do |
| 59 | + code_snippet = '1.2 * (Rational(2) - 1)' |
| 60 | + expected_result = [Float, :*, [Rational, :-, Fixnum]] |
| 61 | + |
| 62 | + actual_result = CodeBreaker::Parser.new(code_snippet).run |
| 63 | + expect(actual_result).to eq expected_result |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments