spec/bencodr/parser_spec.rb in bencodr-1.2.0 vs spec/bencodr/parser_spec.rb in bencodr-2.0.0

- old
+ new

@@ -1,196 +1,61 @@ # encoding: UTF-8 -require "spec" +require "spec_helper" describe BEncodr::Parser do describe "#parse_object" do - # Most of this functionality is covered with other tests. So minimal stuff here. - it "should parse a bencoded string" do - scanner = StringScanner.new("6:string") - BEncodr::Parser.parse_object(scanner).should == "string" - end - - it "should parse a bencoded integer" do - scanner = StringScanner.new("i4e") - BEncodr::Parser.parse_object(scanner).should == 4 - end - - it "should parse a bencoded list" do - scanner = StringScanner.new("l6:stringeeeee") - BEncodr::Parser.parse_object(scanner).should == ["string"] - end - - it "should parse a bencoded dictionary containing a key value pair" do - scanner = StringScanner.new("d6:stringi1ee") - BEncodr::Parser.parse_object(scanner).should == {"string" => 1} - end - - it "should return nil when the type is not recognized" do - scanner = StringScanner.new("freak out!") - BEncodr::Parser.parse_object(scanner).should == nil - end + it{ should parse("6:string").as(:object).to("string") } + it{ should parse("i4e").as(:object).to(4) } + it{ should parse("l6:stringeeeee").as(:object).to(["string"]) } + it{ should parse("d6:stringi1ee").as(:object).to({"string" => 1}) } + it{ should parse("freak out!").as(:object).to(nil) } end - describe "#parse_stirng" do - it "should parse a bencoded string" do - scanner = StringScanner.new("6:string") - BEncodr::Parser.parse_string(scanner).should == "string" - end - - it "should parse a zero length bencoded string" do - scanner = StringScanner.new("0:") - BEncodr::Parser.parse_string(scanner).should == "" - end - - it "should raise an error if the length is invalid" do - scanner = StringScanner.new("fail:") - lambda {BEncodr::Parser.parse_string(scanner)}.should raise_error BEncodr::BEncodeError - end - - it "should raise an error if length is too long" do - scanner = StringScanner.new("3:a") - lambda {BEncodr::Parser.parse_string(scanner)}.should raise_error BEncodr::BEncodeError - end - - it "should raise an error if the colon is missing" do - scanner = StringScanner.new("3aaa") - lambda {BEncodr::Parser.parse_string(scanner)}.should raise_error BEncodr::BEncodeError - end + describe "#parse_string" do + it{ should parse("6:string").as(:string).to("string") } + it{ should parse("0:").as(:string).to("") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:string).with("fail:") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:string).with("3:a") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:string).with("3aaa") } end describe "#parse_integer" do - it "should parse a bencoded integer" do - scanner = StringScanner.new("i4e") - BEncodr::Parser.parse_integer(scanner).should == 4 - end - - it "should raise an error if there is no starting i" do - scanner = StringScanner.new("4e") - lambda{BEncodr::Parser.parse_integer(scanner)}.should raise_error BEncodr::BEncodeError - end - - it "should raise an error if there is no integer" do - scanner = StringScanner.new("ie") - lambda{BEncodr::Parser.parse_integer(scanner)}.should raise_error BEncodr::BEncodeError - end - - it "should raise an error if there is no closing e" do - scanner = StringScanner.new("i4") - lambda{BEncodr::Parser.parse_integer(scanner)}.should raise_error BEncodr::BEncodeError - end + it{ should parse("i4e").as(:integer).to(4) } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:integer).with("4e") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:integer).with("ie") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:integer).with("i4") } end describe "#parse_list" do - it "should parse an empty bencoded list" do - scanner = StringScanner.new("le") - BEncodr::Parser.parse_list(scanner).should == [] - end - - it "should parse a bencoded list containing a string" do - scanner = StringScanner.new("l6:stringeeeee") - BEncodr::Parser.parse_list(scanner).should == ["string"] - end - - it "should parse a bencoded list containing more than one string" do - scanner = StringScanner.new("l6:string6:stringe") - BEncodr::Parser.parse_list(scanner).should == ["string", "string"] - end - - it "should parse a bencoded list containing an integer" do - scanner = StringScanner.new("li1ee") - BEncodr::Parser.parse_list(scanner).should == [1] - end - - it "should parse a bencoded list containing more than one integer" do - scanner = StringScanner.new("li1ei2ee") - BEncodr::Parser.parse_list(scanner).should == [1, 2] - end - - it "should parse a bencoded list containing a list" do - scanner = StringScanner.new("llee") - BEncodr::Parser.parse_list(scanner).should == [[]] - end - - it "should parse a bencoded list containing more than one list" do - scanner = StringScanner.new("llelee") - BEncodr::Parser.parse_list(scanner).should == [[], []] - end - - it "should parse a bencoded list containing a dictionary" do - scanner = StringScanner.new("ldee") - BEncodr::Parser.parse_list(scanner).should == [{}] - end - - it "should parse a bencoded list containing more than one dictionary" do - scanner = StringScanner.new("ldedee") - BEncodr::Parser.parse_list(scanner).should == [{}, {}] - end - - it "should raise an error if there is no starting l" do - scanner = StringScanner.new("e") - lambda{BEncodr::Parser.parse_list(scanner)}.should raise_error BEncodr::BEncodeError - end - - it "should raise an error if there is no closing e" do - scanner = StringScanner.new("l") - lambda{BEncodr::Parser.parse_list(scanner)}.should raise_error BEncodr::BEncodeError - end + it{ should parse("le").as(:list).to([]) } + it{ should parse("l6:stringeeeee").as(:list).to(["string"]) } + it{ should parse("l6:string6:stringe").as(:list).to(["string", "string"]) } + it{ should parse("li1ee").as(:list).to([1]) } + it{ should parse("li1ei-2ee").as(:list).to([1, -2]) } + it{ should parse("llee").as(:list).to([[]]) } + it{ should parse("llelee").as(:list).to([[], []]) } + it{ should parse("ldee").as(:list).to([{}]) } + it{ should parse("ldedee").as(:list).to([{}, {}]) } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:list).with("e") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:list).with("l") } end describe "#parse_dictionary" do - it "should parse an empty bencoded dictionary" do - scanner = StringScanner.new("de") - BEncodr::Parser.parse_dictionary(scanner).should == {} - end - - it "should parse a bencoded dictionary containing a key value pair" do - scanner = StringScanner.new("d6:stringi1ee") - BEncodr::Parser.parse_dictionary(scanner).should == {"string" => 1} - end - - it "should parse a bencoded dictionary containing more than one key value pair" do - scanner = StringScanner.new("d7:anotherle6:stringi1ee") - BEncodr::Parser.parse_dictionary(scanner).should == {"string" => 1, "another" => []} - end - - it "should raise an error if there is no starting d" do - scanner = StringScanner.new("e") - lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError - end - - it "should raise an error if the key is not a string" do - scanner = StringScanner.new("di1ei1ee") - lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError - end - - it "should raise an error if there is no closing e" do - scanner = StringScanner.new("d") - lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError - end - - it "should raise an error if there is a key with no value" do - scanner = StringScanner.new("d1:ae") - lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError - end + it{ should parse("de").as(:dictionary).to({}) } + it{ should parse("d6:stringi1ee").as(:dictionary).to({"string" => 1}) } + it{ should parse("d7:anotherle6:stringi1ee").as(:dictionary).to({"string" => 1, "another" => []}) } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:dictionary).with("e") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:dictionary).with("di1ei1ee") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:dictionary).with("d") } + it{ should generate_parse_error(BEncodr::BEncodeError).for(:dictionary).with("d1:ae") } end end describe String do describe "#bdecode" do - it "should decode a bencoded string into a ruby string" do - "6:string".bdecode.should == "string" - end - - it "should decode a bencoded integer into a ruby integer" do - "i-1e".bdecode.should == -1 - end - - it "should decode a bencoded list into a ruby array" do - "le".bdecode.should == [] - end - - it "should decode a bencoded dictionary into a ruby hash" do - "de".bdecode.should == {} - end + it{ "6:string".should bdecode_to("string") } + it{ "i-1e".should bdecode_to(-1) } + it{ "le".should bdecode_to([]) } + it{ "de".should bdecode_to({}) } end end \ No newline at end of file