File: corpus_spec.rb

package info (click to toggle)
ruby-bson 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,828 kB
  • sloc: ruby: 11,712; ansic: 1,427; java: 514; makefile: 8
file content (134 lines) | stat: -rw-r--r-- 4,374 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# rubocop:todo all
require 'spec_helper'
require 'runners/corpus'

describe 'BSON Corpus spec tests' do

  BSON_CORPUS_TESTS.each do |path|
    basename = File.basename(path)
    # All of the tests in the failures subdir are failing apparently
    #basename = path.sub(/.*corpus-tests\//, '')

    spec = BSON::Corpus::Spec.new(path)

    context("(#{basename}): #{spec.description}") do

      spec.valid_tests&.each do |test|

        context("valid: #{test.description}") do

          let(:decoded_canonical_bson) do
            BSON::Document.from_bson(BSON::ByteBuffer.new(test.canonical_bson), mode: :bson)
          end

          it 'round-trips canonical bson' do
            decoded_canonical_bson.to_bson.to_s.should == test.canonical_bson
          end

=begin
          it 'converts bson to canonical extended json' do
            pending
            raise NotImplementedError
          end
=end

          it 'converts bson to canonical extended json' do
            decoded_canonical_bson.as_extended_json.should == test.canonical_extjson_doc
          end

          if test.relaxed_extjson
            it 'converts bson to relaxed extended json' do
              decoded_canonical_bson.as_extended_json(mode: :relaxed).should == test.relaxed_extjson_doc
            end

            let(:parsed_relaxed_extjson) do
              BSON::ExtJSON.parse_obj(test.relaxed_extjson_doc, mode: :bson)
            end

            let(:round_tripped_relaxed_extjson) do
              parsed_relaxed_extjson.as_extended_json(mode: :relaxed)
            end

            # Relaxed extended json may parse into something other than the
            # canonical bson. For example, relaxed extjson representation for
            # a small int64 is a number that would serialize to an int32.
            # But round-tripping extended json back to extjson should produce
            # the same representation we started with.
            it 'round-trips relaxed extended json' do
              round_tripped_relaxed_extjson.should == test.relaxed_extjson_doc
            end
          end

          if test.degenerate_bson

            let(:decoded_degenerate_bson) do
              BSON::Document.from_bson(BSON::ByteBuffer.new(test.degenerate_bson), mode: :relaxed)
            end

            it 'round-trips degenerate bson to canonical bson' do
              decoded_degenerate_bson.to_bson.to_s.should == test.canonical_bson
            end
          end

          let(:parsed_canonical_extjson) do
            BSON::ExtJSON.parse_obj(test.canonical_extjson_doc, mode: :bson)
          end

          unless test.lossy?
            it 'converts canonical extended json to bson' do
              parsed_canonical_extjson.to_bson.to_s.should == test.canonical_bson
            end
          end

        end
      end

      spec.decode_error_tests&.each do |test|

        context("decode error: #{test.description}") do

          let(:decoded_bson) do
            BSON::Document.from_bson(BSON::ByteBuffer.new(test.bson), mode: :bson)
          end

          # Until bson-ruby gets an exception hierarchy, we can only rescue
          # the basic Exception here.
          # https://jira.mongodb.org/browse/RUBY-1806
          it 'raises an exception' do
            expect do
              decoded_bson
            end.to raise_error(Exception)
          end
        end
      end

      spec.parse_error_tests&.each do |test|

        context("parse error: #{test.description}") do

          # As per:
          #   https://github.com/mongodb/specifications/blob/e7ee829329400786e01279b4f37d4e440d1e9cfa/source/bson-corpus/bson-corpus.rst#decimal128-type-0x13
          #
          # Values under test must be parsed in a type-specific manner.
          let(:parsed_value) do
            case spec.bson_type
            when BSON::Decimal128::BSON_TYPE
              BSON::Decimal128.new(test.string)
            else
              BSON::ExtJSON.parse(test.string, mode: :bson)
            end
          end

          # Until bson-ruby gets an exception hierarchy, we can only rescue
          # the basic Exception here.
          # https://jira.mongodb.org/browse/RUBY-1806
          it 'raises an exception' do
            expect do
              parsed_value
            end.to raise_error(Exception)
          end
        end
      end
    end
  end
end