<commit_before>require File.dirname(__FILE__) + '../../spec_helper'
set :environment, :test

describe "My Traffic light server" do
  include Rack::Test::Methods

  class TrafficLightPiServer < Sinatra::Base
    configure do
      @@line_map = {
        :front => {
          :green => 12,
          :orange => 13,
          :red => 14
        },
        :left => {
          :green => 4,
          :red => 5
        }
      }
      init_lights
    end
  end

  def app
    @app || TrafficLightPiServer
  end

  # Do a root test
  it "should respond to /" do
    get '/'
    last_response.should be_ok
  end

  it "should get 0 for /front/red" do
    get '/front/red/0'
    get '/front/red'
    last_response.should be_ok
    last_response.body.should == "14:0"
  end

  it "should get 0 for /left/red" do
    get '/left/red/0'
    get '/left/red'
    last_response.body.should == "5:0"
  end

  it "should get 0 for /front/green" do
    get '/front/green/0'
    get '/front/green'
    last_response.body.should == "12:0"
  end

  it "should change color for /front/green" do
    get '/front/green/0'

    get '/front/green'
    last_response.body.should == "12:0"

    get '/front/green/1'
    last_response.body.should == "12:1"

    get '/front/green'
    last_response.body.should == "12:1"

    get '/front/green/0'
    last_response.body.should == "12:0"

    get '/front/green'
    last_response.body.should == "12:0"
  end

  it "should change color for /front/red and keep green status" do
    get '/front/green/0'
    get '/front/red/0'

    get '/front/red'
    last_response.body.should == "14:0"

    get '/front/red/1'
    last_response.body.should == "14:1"

    get '/front/red'
    last_response.body.should == "14:1"

    get '/front/green'
    last_response.body.should == "12:0"
  end

  it "should change color for /left/red and keep /front/red status" do
    get '/front/red/0'
    get '/left/red/0'

    get '/front/red'
    last_response.body.should == "14:0"

    get '/front/red/1'
    last_response.body.should == "14:1"

    get '/front/red'
    last_response.body.should == "14:1"

    get '/left/red'
    last_response.body.should == "5:0"
  end

  it "should play mp3" do
    get '/play/clic'
    last_response.should be_ok
    last_response.body.should =~ /Played with pid: \d+/
  end

  it "should return 404 if try to play a missing file" do
    get '/play/missing-file'
    last_response.status.should == 404
  end

  it "should return a custom 404 error page" do
    get '/left/red/bad/url'
    last_response.status.should == 404
    last_response.body.should =~ /traffic-lights-lost/
  end

  it "should return a custom 500 error page" do
    get '/left/red/4'
    last_response.status.should == 500
    last_response.body.should =~ /traffic-lights-ko/
  end
end
<commit_msg>Spec refactoring<commit_after>require File.dirname(__FILE__) + '../../spec_helper'
set :environment, :test

describe "My Traffic light server" do
  include Rack::Test::Methods

  class TrafficLightPiServer < Sinatra::Base
    configure do
      @@line_map = {
        :front => {
          :green => 12,
          :orange => 13,
          :red => 14
        },
        :left => {
          :green => 4,
          :red => 5
        }
      }
      init_lights
    end
  end

  def app
    @app || TrafficLightPiServer
  end

  # Do a root test
  it "should respond to /" do
    get '/'
    last_response.should be_ok
  end

  it "should change status for different line & light" do
    get '/front/green/0'
    get '/front/red/0'
    get '/left/red/0'

    get '/front/green'
    last_response.body.should == "12:0"

    get '/front/green/1'
    last_response.body.should == "12:1"

    get '/front/green'
    last_response.body.should == "12:1"

    get '/front/green/0'
    last_response.body.should == "12:0"

    get '/front/green'
    last_response.body.should == "12:0"

    get '/front/red'
    last_response.body.should == "14:0"

    get '/front/red/1'
    last_response.body.should == "14:1"

    get '/front/red'
    last_response.body.should == "14:1"

    get '/front/green'
    last_response.body.should == "12:0"

    get '/left/red'
    last_response.body.should == "5:0"
  end

  it "should play mp3" do
    get '/play/clic'
    last_response.should be_ok
    last_response.body.should =~ /Played with pid: \d+/
  end

  it "should return 404 if try to play a missing file" do
    get '/play/missing-file'
    last_response.status.should == 404
  end

  it "should return a custom 404 & 500 error pages" do
    get '/left/red/bad/url'
    last_response.status.should == 404
    last_response.body.should =~ /traffic-lights-lost/

    get '/left/red/4'
    last_response.status.should == 500
    last_response.body.should =~ /traffic-lights-ko/
  end
end

{'fraction_non_alphanumeric': 0.13658230495712195, 'fraction_numerical': 0.03179251202677264, 'mean_word_length': 2.248641304347826, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4541753', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1643, 'n_words': 422}