Created
November 1, 2011 14:08
-
-
Save practicingruby/1330575 to your computer and use it in GitHub Desktop.
Examples for Practicing Ruby 2.11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Feature: Addition | |
| Scenario: Add two numbers | |
| Given I have entered 70 into the calculator | |
| And I have entered 50 into the calculator | |
| When I press add | |
| Then the result should be 120 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In this example I will demonstrate how to implement step() methods which parse the addition.feature file | |
| step /I have entered (\d+) into the calculator/ do |n| | |
| @calculator ||= Calculator.new | |
| @calculator.push(n.to_i) | |
| end | |
| step /I press add/ do | |
| @result = @calculator.add | |
| end | |
| step /the result should be (\d+)/ do |n| | |
| abort "Expected #{n}, got #{@result}" unless @result == n.to_i | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In this example I will demonstrate how to implement attribute, a clone of attr_accessor | |
| class Person | |
| attribute :name, :email | |
| end | |
| person = Person.new | |
| person.name = "Greg Brown" | |
| p person.name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In this example I will demonstrate how to implement a clone of Rails before_filter | |
| class ApplicationController < BasicController | |
| before_filter :authenticate | |
| def authenticate | |
| puts "Authenticating current user" | |
| end | |
| end | |
| class PersonController < ApplicationController | |
| before_filter :locate_person, :only => [:show, :edit, :update] | |
| def show | |
| puts "showing a person's data" | |
| end | |
| def edit | |
| puts "displaying a person edit form" | |
| end | |
| def update | |
| puts "committing updated person data to the database" | |
| end | |
| def create | |
| puts "creating a new person" | |
| end | |
| def locate_person | |
| puts "Locating a person" | |
| end | |
| end | |
| controller = PersonController.new | |
| puts "EXECUTING SHOW" | |
| controller.execute(:show) | |
| puts | |
| puts "EXECUTING CREATE" | |
| controller.execute(:create) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In this example I will demonstrate a naive implementation of XML Builder | |
| builder = FakeBuilder::XmlMarkup.new | |
| xml = builder.class do |roster| | |
| roster.student { |b| b.name("Jim"); b.phone("555-1234") } | |
| roster.student { |b| b.name("Jordan"); b.phone("123-1234") } | |
| roster.student { |b| b.name("Greg"); b.phone("567-1234") } | |
| end | |
| puts xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In this example I will demonstrate how to build something similar to citrusbyte/contest on top of MiniTest | |
| context "A Portal" do | |
| setup do | |
| @portal = Portal.new | |
| end | |
| test "must not be open by default" do | |
| refute @portal.open? | |
| end | |
| test "must not be open when just the orange endpoint is set" do | |
| @portal.orange = [3,3,3] | |
| refute @portal.open? | |
| end | |
| test "must not be open when just the blue endpoint is set" do | |
| @portal.blue = [5,5,5] | |
| refute @portal.open? | |
| end | |
| test "must be open when both endpoints are set" do | |
| @portal.orange = [3,3,3] | |
| @portal.blue = [5,5,5] | |
| assert @portal.open? | |
| end | |
| test "must require endpoints to be a 3 element array of numbers" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In this example I will show how to build an instance_eval'ed DSL | |
| puts "Via DSL" | |
| FakeMail.deliver do | |
| from "gregory.t.brown@gmail.com" | |
| to "test@test.com" | |
| subject "Hello world" | |
| body "Hi there! This isn't spam, I swear" | |
| end | |
| puts "Via ordinary API" | |
| mail = FakeMail::Message.new | |
| mail.from = "gregory.t.brown@gmail.com" | |
| mail.to = "test@test.com" | |
| mail.subject = "Hello world" | |
| mail.body = "Hi there! This isn't spam, I swear" | |
| mail.deliver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment