Sha256: e3295af3eca79d249326e50999ec0a909a37deeee360466fe2c08c8d396ec3b0

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require_relative './spec_helper'

describe "Luhn" do
  describe "on Numeric" do
    let(:valid) { 4100410382 }
    let(:valid_check) { 2 }
    let(:incomplete) { 410041038 }
    let(:invalid) { 4100410383 }

    it "should validate valid luhn codes" do
      valid.luhn?.should be_true
    end

    it "should invalidate invalid luhn codes" do
      invalid.luhn?.should be_false
    end

    it "should calculate the correct luhn check digit" do
      incomplete.luhn.should == valid_check
    end

    it "should append the correct check digit" do
      incomplete.luhn!.should == valid
    end

    it "should quack like a Numeric" do
      incomplete.luhn!.should be_a Numeric
    end
  end

  describe "on String" do
    let(:valid) { '4 10041038 2' }
    let(:valid_check) { '2' }
    let(:incomplete) { '410041038' }
    let(:invalid) { '4100410383' }

    it "should validate valid luhn codes" do
      valid.luhn?.should be_true
    end

    it "should invalidate invalid luhn codes" do
      invalid.luhn?.should be_false
    end

    it "should calculate the correct luhn check digit" do
      incomplete.luhn.should == valid_check
    end

    it "should append the correct check digit" do
      incomplete.luhn!.should == valid.gsub(/\D+/, '')
    end

    it "should return Strings" do
      incomplete.luhn!.should be_a String
    end
  end

  it "should return valid checksum (one digit)" do
    123.luhn.should be 0
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
moeffju-luhn-0.1.3 spec/luhn_spec.rb