|
| 1 | +@testable import BSON |
| 2 | +import Foundation |
| 3 | +import Nimble |
| 4 | +import XCTest |
| 5 | + |
| 6 | +final class BSONValueTests: BSONTestCase { |
| 7 | + func testInvalidDecimal128() throws { |
| 8 | + expect(try BSONDecimal128("hi")).to(throwError()) |
| 9 | + expect(try BSONDecimal128("123.4.5")).to(throwError()) |
| 10 | + expect(try BSONDecimal128("10")).toNot(throwError()) |
| 11 | + |
| 12 | + expect(try BSONDecimal128("nan")).toNot(throwError()) |
| 13 | + expect(try BSONDecimal128("nana")).to(throwError()) |
| 14 | + } |
| 15 | + |
| 16 | + func testUUIDBytes() throws { |
| 17 | + let twoBytes = Data(base64Encoded: "//8=")! |
| 18 | + let sixteenBytes = Data(base64Encoded: "c//SZESzTGmQ6OfR38A11A==")! |
| 19 | + |
| 20 | + // UUIDs must have 16 bytes |
| 21 | + expect(try BSONBinary(data: twoBytes, subtype: .uuidDeprecated)) |
| 22 | + .to(throwError(errorType: BSONError.InvalidArgumentError.self)) |
| 23 | + expect(try BSONBinary(data: twoBytes, subtype: .uuid)) |
| 24 | + .to(throwError(errorType: BSONError.InvalidArgumentError.self)) |
| 25 | + expect(try BSONBinary(data: sixteenBytes, subtype: .uuidDeprecated)).toNot(throwError()) |
| 26 | + expect(try BSONBinary(data: sixteenBytes, subtype: .uuid)).toNot(throwError()) |
| 27 | + } |
| 28 | + |
| 29 | + fileprivate func checkTrueAndFalse(val: BSON, alternate: BSON) { |
| 30 | + expect(val).to(equal(val)) |
| 31 | + expect(val).toNot(equal(alternate)) |
| 32 | + } |
| 33 | + |
| 34 | + func testBSONEquatable() throws { |
| 35 | + // Int |
| 36 | + self.checkTrueAndFalse(val: 1, alternate: 2) |
| 37 | + // Int32 |
| 38 | + self.checkTrueAndFalse(val: .int32(32), alternate: .int32(33)) |
| 39 | + // Int64 |
| 40 | + self.checkTrueAndFalse(val: .int64(64), alternate: .int64(65)) |
| 41 | + // Double |
| 42 | + self.checkTrueAndFalse(val: 1.618, alternate: 2.718) |
| 43 | + // Decimal128 |
| 44 | + self.checkTrueAndFalse( |
| 45 | + val: .decimal128(try BSONDecimal128("1.618")), |
| 46 | + alternate: .decimal128(try BSONDecimal128("2.718")) |
| 47 | + ) |
| 48 | + // Bool |
| 49 | + self.checkTrueAndFalse(val: true, alternate: false) |
| 50 | + // String |
| 51 | + self.checkTrueAndFalse(val: "some", alternate: "not some") |
| 52 | + // RegularExpression |
| 53 | + self.checkTrueAndFalse( |
| 54 | + val: .regex(BSONRegularExpression(pattern: ".*", options: "")), |
| 55 | + alternate: .regex(BSONRegularExpression(pattern: ".+", options: "")) |
| 56 | + ) |
| 57 | + // Timestamp |
| 58 | + self.checkTrueAndFalse( |
| 59 | + val: .timestamp(BSONTimestamp(timestamp: 1, inc: 2)), |
| 60 | + alternate: .timestamp(BSONTimestamp(timestamp: 5, inc: 10)) |
| 61 | + ) |
| 62 | + // Date |
| 63 | + self.checkTrueAndFalse( |
| 64 | + val: .datetime(Date(timeIntervalSinceReferenceDate: 5000)), |
| 65 | + alternate: .datetime(Date(timeIntervalSinceReferenceDate: 5001)) |
| 66 | + ) |
| 67 | + // MinKey & MaxKey |
| 68 | + expect(BSON.minKey).to(equal(.minKey)) |
| 69 | + expect(BSON.maxKey).to(equal(.maxKey)) |
| 70 | + // ObjectId |
| 71 | + self.checkTrueAndFalse(val: .objectID(), alternate: .objectID()) |
| 72 | + // CodeWithScope |
| 73 | + self.checkTrueAndFalse( |
| 74 | + val: .codeWithScope(BSONCodeWithScope(code: "console.log('foo');", scope: [:])), |
| 75 | + alternate: .codeWithScope(BSONCodeWithScope(code: "console.log(x);", scope: ["x": 2])) |
| 76 | + ) |
| 77 | + // Binary |
| 78 | + self.checkTrueAndFalse( |
| 79 | + val: .binary(try BSONBinary(data: Data(base64Encoded: "c//SZESzTGmQ6OfR38A11A==")!, subtype: .uuid)), |
| 80 | + alternate: .binary(try BSONBinary(data: Data(base64Encoded: "c//88KLnfdfefOfR33ddFA==")!, subtype: .uuid)) |
| 81 | + ) |
| 82 | + // Document |
| 83 | + self.checkTrueAndFalse( |
| 84 | + val: [ |
| 85 | + "foo": 1.414, |
| 86 | + "bar": "swift", |
| 87 | + "nested": ["a": 1, "b": "2"] |
| 88 | + ], |
| 89 | + alternate: [ |
| 90 | + "foo": 1.414, |
| 91 | + "bar": "swift", |
| 92 | + "nested": ["a": 1, "b": "different"] |
| 93 | + ] |
| 94 | + ) |
| 95 | + |
| 96 | + // Different types |
| 97 | + expect(BSON.int32(4)).toNot(equal("swift")) |
| 98 | + |
| 99 | + // Arrays of different sizes should not be equal |
| 100 | + let b0: BSON = [1, 2] |
| 101 | + let b1: BSON = [1, 2, 3] |
| 102 | + expect(b0).toNot(equal(b1)) |
| 103 | + } |
| 104 | + |
| 105 | + struct BSONNumberTestCase { |
| 106 | + let int: Int? |
| 107 | + let double: Double? |
| 108 | + let int32: Int32? |
| 109 | + let int64: Int64? |
| 110 | + let decimal: BSONDecimal128? |
| 111 | + |
| 112 | + static func compare<T: Equatable>(computed: T?, expected: T?) { |
| 113 | + guard computed != nil else { |
| 114 | + expect(expected).to(beNil()) |
| 115 | + return |
| 116 | + } |
| 117 | + expect(computed).to(equal(expected)) |
| 118 | + } |
| 119 | + |
| 120 | + func run() { |
| 121 | + let candidates: [BSON?] = [ |
| 122 | + self.int.map { BSON(integerLiteral: $0) }, |
| 123 | + self.double.map { .double($0) }, |
| 124 | + self.int32.map { .int32($0) }, |
| 125 | + self.int64.map { .int64($0) }, |
| 126 | + self.decimal.map { .decimal128($0) } |
| 127 | + ] |
| 128 | + |
| 129 | + candidates.compactMap { $0 }.forEach { l in |
| 130 | + // Skip the Decimal128 conversions until they're implemented |
| 131 | + // TODO: don't skip these (SWIFT-367) |
| 132 | + guard l.decimal128Value == nil else { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + BSONNumberTestCase.compare(computed: l.toInt(), expected: self.int) |
| 137 | + BSONNumberTestCase.compare(computed: l.toInt32(), expected: self.int32) |
| 138 | + BSONNumberTestCase.compare(computed: l.toInt64(), expected: self.int64) |
| 139 | + BSONNumberTestCase.compare(computed: l.toDouble(), expected: self.double) |
| 140 | + |
| 141 | + // Skip double for this conversion since it generates a Decimal128(5.0) =/= Decimal128(5) |
| 142 | + if l.doubleValue == nil { |
| 143 | + BSONNumberTestCase.compare(computed: l.toDecimal128(), expected: self.decimal) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + func testBSONNumber() throws { |
| 150 | + let decimal128 = try BSONDecimal128("5.5") |
| 151 | + let double: BSON = 5.5 |
| 152 | + |
| 153 | + expect(double.toDouble()).to(equal(5.5)) |
| 154 | + expect(double.toDecimal128()).to(equal(decimal128)) |
| 155 | + |
| 156 | + let cases = [ |
| 157 | + BSONNumberTestCase(int: 5, double: 5.0, int32: Int32(5), int64: Int64(5), decimal: try BSONDecimal128("5")), |
| 158 | + BSONNumberTestCase( |
| 159 | + int: -5, |
| 160 | + double: -5.0, |
| 161 | + int32: Int32(-5), |
| 162 | + int64: Int64(-5), |
| 163 | + decimal: try BSONDecimal128("-5") |
| 164 | + ), |
| 165 | + BSONNumberTestCase(int: 0, double: 0.0, int32: Int32(0), int64: Int64(0), decimal: try BSONDecimal128("0")), |
| 166 | + BSONNumberTestCase(int: nil, double: 1.234, int32: nil, int64: nil, decimal: try BSONDecimal128("1.234")), |
| 167 | + BSONNumberTestCase( |
| 168 | + int: nil, |
| 169 | + double: -31.234, |
| 170 | + int32: nil, |
| 171 | + int64: nil, |
| 172 | + decimal: try BSONDecimal128("-31.234") |
| 173 | + ) |
| 174 | + ] |
| 175 | + |
| 176 | + cases.forEach { $0.run() } |
| 177 | + } |
| 178 | +} |
0 commit comments