spec/char_set_spec.rb in chars-0.2.0 vs spec/char_set_spec.rb in chars-0.2.1

- old
+ new

@@ -1,73 +1,62 @@ +require 'spec_helper' require 'chars/chars' -require 'spec_helper' - describe Chars::CharSet do before(:all) do @integer_range = (0x41..0x43) @string_range = ('A'..'Z') @integers = @integer_range.to_a @strings = @string_range.to_a - @char_set = Chars::CharSet.new(*@strings) + @char_set = described_class.new(*@strings) end - it "may be created with String arguments" do - @chars = Chars::CharSet.new(*@strings) + describe "#initialize" do + it "may be created with String arguments" do + @chars = described_class.new(*@strings) - @strings.each do |s| - @chars.include_char?(s).should == true + @strings.all? { |s| @chars.include_char?(s) }.should == true end - end - it "may be created with an Array of Strings" do - @chars = Chars::CharSet.new(@strings) + it "may be created with an Array of Strings" do + @chars = described_class.new(@strings) - @strings.each do |s| - @chars.include_char?(s).should == true + @strings.all? { |s| @chars.include_char?(s) }.should == true end - end - it "may be created with a Range of Strings" do - @chars = Chars::CharSet.new(@string_range) + it "may be created with a Range of Strings" do + @chars = described_class.new(@string_range) - @strings.each do |s| - @chars.include_char?(s).should == true + @strings.all? { |s| @chars.include_char?(s) }.should == true end - end - it "may be created with Integer arguments" do - @chars = Chars::CharSet.new(*@integers) + it "may be created with Integer arguments" do + @chars = described_class.new(*@integers) - @integers.each do |i| - @chars.include?(i).should == true + @integers.all? { |i| @chars.include?(i) }.should == true end - end - it "may be created with an Array of Integers" do - @chars = Chars::CharSet.new(@integers) + it "may be created with an Array of Integers" do + @chars = described_class.new(@integers) - @integers.each do |i| - @chars.include?(i).should == true + @integers.all? { |i| @chars.include?(i) }.should == true end - end - it "may be created with a Range of Integers" do - @chars = Chars::CharSet.new(@integer_range) + it "may be created with a Range of Integers" do + @chars = described_class.new(@integer_range) - @integers.each do |i| - @chars.include?(i).should == true + @integers.all? { |i| @chars.include?(i) }.should == true end end it "should include Strings" do @char_set.include_char?('A').should == true end it "should include Integers" do - @char_set.include?(0x41).should == true + @char_set.should include(0x41) end it "should be able to select bytes" do @sub_chars = @char_set.select_bytes { |c| c <= 0x42 } @@ -79,129 +68,114 @@ @sub_chars.should == ['A', 'B'] end it "should return a random byte" do - @char_set.include?(@char_set.random_byte).should == true + @char_set.should include(@char_set.random_byte) end it "should return a random char" do @char_set.include_char?(@char_set.random_char).should == true end it "should iterate over n random bytes" do - @char_set.each_random_byte(10) do |b| - @char_set.include?(b).should == true - end + @char_set.each_random_byte(10).all? { |b| + @char_set.include?(b) + }.should == true end it "should iterate over n random chars" do - @char_set.each_random_char(10) do |c| - @char_set.include_char?(c).should == true - end + @char_set.each_random_char(10).all? { |c| + @char_set.include_char?(c) + }.should == true end it "should return a random Array of bytes" do bytes = @char_set.random_bytes(10) - bytes.each do |b| - @char_set.include?(b).should == true - end + bytes.all? { |b| @char_set.include?(b) }.should == true end it "should return a random Array of chars" do chars = @char_set.random_chars(10) - chars.each do |c| - @char_set.include_char?(c).should == true - end + chars.all? { |c| @char_set.include_char?(c) }.should == true end it "should return a random Array of bytes with a varying length" do bytes = @char_set.random_bytes(5..10) - bytes.length.between?(5, 10).should == true - bytes.each do |b| - @char_set.include?(b).should == true - end + bytes.length.should be_between(5, 10) + bytes.all? { |b| @char_set.include?(b) }.should == true end it "should return a random Array of chars with a varying length" do chars = @char_set.random_chars(5..10) - chars.length.between?(5, 10).should == true - chars.each do |c| - @char_set.include_char?(c).should == true - end + chars.length.should be_between(5, 10) + chars.all? { |c| @char_set.include_char?(c) }.should == true end it "should return a random String of chars" do - @char_set.random_string(10).each_byte do |b| - @char_set.include?(b).should == true - end + string = @char_set.random_string(10) + + string.chars.all? { |b| @char_set.include_char?(b) }.should == true end it "should return a random String of chars with a varying length" do string = @char_set.random_string(5..10) - string.length.between?(5, 10) - string.each_byte do |b| - @char_set.include?(b).should == true - end + string.length.should be_between(5, 10) + string.chars.all? { |b| @char_set.include_char?(b) }.should == true end it "should return a random Array of unique bytes" do bytes = @char_set.random_distinct_bytes(10) - bytes.uniq.length.should == bytes.length - bytes.each do |b| - @char_set.include?(b).should == true - end + + bytes.uniq.should == bytes + bytes.all? { |b| @char_set.include?(b) }.should == true end it "should return a random Array of unique chars" do chars = @char_set.random_distinct_chars(10) - chars.uniq.length.should == chars.length - chars.each do |c| - @char_set.include_char?(c).should == true - end + + chars.uniq.should == chars + chars.all? { |c| @char_set.include_char?(c) }.should == true end it "should return a random Array of unique bytes with a varying length" do bytes = @char_set.random_distinct_bytes(5..10) - bytes.uniq.length.should == bytes.length - bytes.length.between?(5, 10).should == true - bytes.each do |b| - @char_set.include?(b).should == true - end + + bytes.uniq.should == bytes + bytes.length.should be_between(5, 10) + bytes.all? { |b| @char_set.include?(b) }.should == true end it "should return a random Array of unique chars with a varying length" do chars = @char_set.random_distinct_chars(5..10) - chars.uniq.length.should == chars.length - chars.length.between?(5, 10).should == true - chars.each do |c| - @char_set.include_char?(c).should == true - end + + chars.uniq.should == chars + chars.length.should be_between(5, 10) + chars.all? { |c| @char_set.include_char?(c) }.should == true end - it "should be able to be compared with another set of chars" do - (@char_set == Chars::CharSet[('A'..'Z')]).should == true + @char_set.should == described_class['A'..'Z'] end it "should be able to be unioned with another set of chars" do - super_set = (@char_set | Chars::CharSet['D']) + super_set = (@char_set | described_class['D']) - super_set.class.should == Chars::CharSet - super_set.should == Chars::CharSet[('A'..'Z'), 'D'] + super_set.class.should == described_class + super_set.should == described_class['A'..'Z', 'D'] end it "should be able to be removed from another set of chars" do - sub_set = (@char_set - Chars::CharSet['B']) + sub_set = (@char_set - described_class['B']) - sub_set.class.should == Chars::CharSet - sub_set.subset?(@char_set).should == true + sub_set.class.should == described_class + sub_set.should be_subset(@char_set) end it "should find one sub-string from a String belonging to the char set" do @char_set.strings_in("AAAA").should == ["AAAA"] end @@ -212,9 +186,9 @@ "CCCCCC" ] end it "should determine if a String is made up of the characters from the char set" do - (@char_set === "AABCBAA").should == true - (@char_set === "AA!!EE").should_not == true + @char_set.should === "AABCBAA" + @char_set.should_not === "AA!!EE" end end