spec/luhnacy_spec.rb in luhnacy-0.1.0 vs spec/luhnacy_spec.rb in luhnacy-0.2.0
- old
+ new
@@ -21,6 +21,53 @@
it "should return a string of digits that does not satisfy Luhn" do
string_size = 5
candidate = Luhnacy.generate(string_size, :invalid => true)
Luhnacy.valid?(candidate).should be_false
end
+
+ it "should return a string that satisfies luhn and includes a given prefix" do
+ prefix = '12345'
+ string_size = 15
+ candidate = Luhnacy.generate(string_size, :prefix => prefix)
+ candidate.should match /^#{prefix}\d{#{string_size - prefix.size}}$/
+ end
+
+ it "should return an error if the prefix contains any non-numeric characters" do
+ prefix = 'AB123'
+ string_size = 15
+ lambda { Luhnacy.generate(string_size, :prefix => prefix) }.should raise_error ArgumentError
+ end
+
+ context "Named generators and validators" do
+ it "should validate a correct number" do
+ number = '5354334446769063'
+ Luhnacy.mastercard?(number).should be_true
+ end
+
+ it "should not validate a number if it does not satisfy Luhn" do
+ number = '5354334446769064'
+ Luhnacy.mastercard?(number).should be_false
+ end
+
+ it "should not validate a number if it does not have the correct prefix" do
+ number = Luhnacy.generate(16, :prefix => '49')
+ Luhnacy.mastercard?(number).should be_false
+ end
+
+ it "should not validate a number if it does not have the correct length" do
+ number = Luhnacy.generate(12, :prefix => '52')
+ Luhnacy.mastercard?(number).should be_false
+ end
+
+ it "should generate a valid number" do
+ Luhnacy.mastercard?(Luhnacy.mastercard).should be_true
+ end
+
+ it "should generate an invalid number" do
+ Luhnacy.mastercard?(Luhnacy.mastercard(:invalid => true)).should be_false
+ end
+
+ it "should raise an error if there is no matcher or validator with a given name" do
+ lambda { Luhnacy.superawesomecard?('123') }.should raise_error NoMethodError
+ end
+ end
end