# -*- coding: utf-8 -*- require 'spec_helper' require_relative 'ruby_to_block/block/shared/block_examples' # rubocop:disable EmptyLines, LineLength describe RubyToBlock do describe '#to_blocks', to_blocks: true do context '成功する場合' do _data = <<-EOS require "smalruby" car1 = Character.new(costume: "car1.png", x: 0, y: 0, angle: 0) car1.on(:start) do loop do move(10) turn_if_reach_wall end end EOS describe compact_source_code(_data) do __data = _data let(:data) { __data } it '結果が正しいこと' do should eq_block_xml(<<-XML) car1 10 XML end end _data = <<-EOS require "smalruby" car1 = Character.new(costume: "car2.png", x: 0, y: 0, angle: 0) # 逃げる車 car1.on(:start) do loop do move(6) if reach_wall? turn end end end car1.on(:key_down, K_LEFT) do rotate(-15) end car1.on(:key_down, K_RIGHT) do rotate(15) end EOS describe compact_source_code(_data) do __data = _data let(:data) { __data } it '結果が正しいこと' do should eq_block_xml(<<-XML) 逃げる車 car1 6 K_LEFT down 15 K_RIGHT down 15 XML end end _data = <<-EOS require "smalruby" car1 = Character.new(costume: "car2.png", x: 0, y: 0, angle: 0) car2 = Character.new(costume: "car3.png", x: 0, y: 415, angle: 0) # 追いかける車 car2.on(:start) do loop do point_towards(car1) move(3) if hit?(car1) say(message: "追いつきました!") else say(message: "") end end end EOS describe compact_source_code(_data) do __data = _data let(:data) { __data } it '結果が正しいこと' do should eq_block_xml(<<-XML) 追いかける車 car2 car1 3 car1 追いつきました! XML end end _data = <<-EOS require "smalruby" car1 = Character.new(costume: "car2.png", x: 0, y: 0, angle: 0) car2 = Character.new(costume: "car3.png", x: 0, y: 415, angle: 0) # 逃げる車 car1.on(:start) do loop do move(6) if reach_wall? turn end end end car1.on(:key_down, K_LEFT) do rotate(-15) end car1.on(:key_down, K_RIGHT) do rotate(15) end # 追いかける車 car2.on(:start) do loop do point_towards(car1) move(3) if hit?(car1) say(message: "追いつきました!") else say(message: "") end end end EOS describe compact_source_code(_data) do __data = _data let(:data) { __data } it '結果が正しいこと' do should eq_block_xml(<<-XML) 逃げる車 car1 6 K_LEFT down 15 K_RIGHT down 15 追いかける車 car2 car1 3 car1 追いつきました! XML end end end _data = <<-EOS require "smalruby" init_hardware frog1 = Character.new(costume: "frog1.png", x: 261, y: 191, angle: 0) frog1.on(:click) do say(message: "ライトをぴかっとさせるでよ♪") rgb_led_anode("D3").color = [51, 51, 255] sleep(1) rgb_led_anode("D3").color = [255, 255, 153] sleep(1) rgb_led_anode("D3").color = [255, 0, 0] sleep(1) rgb_led_anode("D3").turn_off say(message: "") end EOS describe compact_source_code(_data) do __data = _data let(:data) { __data } it '結果が正しいこと' do should eq_block_xml(<<-XML) frog1 ライトをぴかっとさせるでよ♪ anode D3 #3333ff 1 anode D3 #ffff99 1 anode D3 #ff0000 1 anode D3 XML end end context '失敗する場合' do let(:data) { '__FAIL__' } it { expect { subject }.to raise_error } end end end