Skip to content

Commit

Permalink
Add tests and docs for measurement unit
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann committed Oct 13, 2022
1 parent 6eceb69 commit c478807
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@
7B6438A726A70DDB000D0F65 /* UIViewControllerSentryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B6438A626A70DDB000D0F65 /* UIViewControllerSentryTests.swift */; };
7B6438AA26A70F24000D0F65 /* UIViewController+Sentry.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B6438A826A70F24000D0F65 /* UIViewController+Sentry.h */; };
7B6438AB26A70F24000D0F65 /* UIViewController+Sentry.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B6438A926A70F24000D0F65 /* UIViewController+Sentry.m */; };
7B68345128F7EB3D00FB7064 /* SentryMeasurementUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B68345028F7EB3D00FB7064 /* SentryMeasurementUnitTests.swift */; };
7B68D93625FF5F1A0082D139 /* SentryAppState+Equality.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B68D93525FF5F1A0082D139 /* SentryAppState+Equality.m */; };
7B6ADFCF26A02CAE0076C206 /* SentryCrashReportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B6ADFCE26A02CAE0076C206 /* SentryCrashReportTests.swift */; };
7B6C5ED6264E62CA0010D138 /* SentryTransactionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B6C5ED5264E62CA0010D138 /* SentryTransactionTests.swift */; };
Expand Down Expand Up @@ -1080,6 +1081,7 @@
7B6438A626A70DDB000D0F65 /* UIViewControllerSentryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = UIViewControllerSentryTests.swift; path = Tests/SentryTests/Categories/UIViewControllerSentryTests.swift; sourceTree = SOURCE_ROOT; };
7B6438A826A70F24000D0F65 /* UIViewController+Sentry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "UIViewController+Sentry.h"; path = "include/UIViewController+Sentry.h"; sourceTree = "<group>"; };
7B6438A926A70F24000D0F65 /* UIViewController+Sentry.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+Sentry.m"; sourceTree = "<group>"; };
7B68345028F7EB3D00FB7064 /* SentryMeasurementUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryMeasurementUnitTests.swift; sourceTree = "<group>"; };
7B68D93425FF5F1A0082D139 /* SentryAppState+Equality.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SentryAppState+Equality.h"; sourceTree = "<group>"; };
7B68D93525FF5F1A0082D139 /* SentryAppState+Equality.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "SentryAppState+Equality.m"; sourceTree = "<group>"; };
7B6ADFCE26A02CAE0076C206 /* SentryCrashReportTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCrashReportTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2249,6 +2251,7 @@
7B68D93525FF5F1A0082D139 /* SentryAppState+Equality.m */,
7BD4BD4A27EB2DC20071F4FF /* SentryDiscardedEventTests.swift */,
7BD4BD4C27EB31820071F4FF /* SentryClientReportTests.swift */,
7B68345028F7EB3D00FB7064 /* SentryMeasurementUnitTests.swift */,
);
path = Protocol;
sourceTree = "<group>";
Expand Down Expand Up @@ -3474,6 +3477,7 @@
7B5AB65D27E48E5200F1D1BA /* TestThreadInspector.swift in Sources */,
7BF9EF742722A85B00B5BBEF /* SentryClassRegistrator.m in Sources */,
63B819141EC352A7002FDF4C /* SentryInterfacesTests.m in Sources */,
7B68345128F7EB3D00FB7064 /* SentryMeasurementUnitTests.swift in Sources */,
7B14089A248791660035403D /* SentryCrashStackEntryMapperTests.swift in Sources */,
7B869EBC249B91D8004F4FDB /* SentryDebugMetaEquality.swift in Sources */,
7B01CE3D271993AC00B5AF31 /* SentryTransportFactoryTests.swift in Sources */,
Expand Down
30 changes: 30 additions & 0 deletions Sources/Sentry/Public/SentryMeasurementUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@

NS_ASSUME_NONNULL_BEGIN

/**
* The unit of measurement of a metric value.
*
* Units augment metric values by giving them a magnitude and semantics. There are certain types
* of units that are subdivided in their precision, such as the ``SentryMeasurementUnitDuration``
* for time measurements. The following unit types are available: ``SentryMeasurementUnitDuration``,
* ``SentryMeasurementUnitInformation``, and``SentryMeasurementUnitFraction``.
*
* When using the units to custom measurements, Sentry will apply formatting to display
* measurement values in the UI.
*/
NS_SWIFT_NAME(MeasurementUnit)
@interface SentryMeasurementUnit : NSObject <NSCopying>
SENTRY_NO_INIT

/**
* Returns an initialized SentryMeasurementUnit with a custom measurement unit.
*
* @param unit Your own custom unit without built-in conversion in Sentry.
*/
- (instancetype)initWithUnit:(NSString *)unit;

/**
* The NSString representation of the measurement unit.
*/
@property (readonly, copy) NSString *unit;

/** Untyped value without a unit. */
@property (class, readonly, copy) SentryMeasurementUnit *none;

@end

/**
* Time duration units.
*/
NS_SWIFT_NAME(MeasurementUnitDuration)
@interface SentryMeasurementUnitDuration : SentryMeasurementUnit
SENTRY_NO_INIT
Expand Down Expand Up @@ -45,6 +67,11 @@ SENTRY_NO_INIT

@end

/**
* Size of information units derived from bytes.
*
* See also [Units of information](https://en.wikipedia.org/wiki/Units_of_information)
*/
NS_SWIFT_NAME(MeasurementUnitInformation)
@interface SentryMeasurementUnitInformation : SentryMeasurementUnit
SENTRY_NO_INIT
Expand Down Expand Up @@ -93,6 +120,9 @@ SENTRY_NO_INIT

@end

/**
* Units of fraction.
*/
NS_SWIFT_NAME(MeasurementUnitFraction)
@interface SentryMeasurementUnitFraction : SentryMeasurementUnit
SENTRY_NO_INIT
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryMeasurementUnit.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ + (SentryMeasurementUnit *)none

- (id)copyWithZone:(nullable NSZone *)zone
{
return [[[self class] allocWithZone:zone] initWithSymbol:self.unit];
return [[[self class] allocWithZone:zone] initWithUnit:self.unit];
}

@end
Expand All @@ -28,7 +28,7 @@ @implementation SentryMeasurementUnitDuration

+ (SentryMeasurementUnitDuration *)nanosecond
{
return [[SentryMeasurementUnitDuration alloc] initWithUnit:@"nanoseconds"];
return [[SentryMeasurementUnitDuration alloc] initWithUnit:@"nanosecond"];
}

+ (SentryMeasurementUnitDuration *)microsecond
Expand Down
62 changes: 62 additions & 0 deletions Tests/SentryTests/Protocol/SentryMeasurementUnitTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import XCTest

final class SentryMeasurementUnitTests: XCTestCase {

func testCustomUnit() {
let unit = "custom"
let sut = MeasurementUnit(unit: unit)

XCTAssertEqual(unit, sut.unit)
}

func testUnitNone() {
XCTAssertEqual("", MeasurementUnit.none.unit)
}

func testCopy() {
let unit = "custom"
let sut = MeasurementUnit(unit: unit).copy() as! MeasurementUnit

XCTAssertEqual(unit, sut.unit)
}

func testCopyOfSubclass() {
let unit = "custom"
let sut = MeasurementUnitDuration(unit: unit).copy() as! MeasurementUnitDuration

XCTAssertEqual(unit, sut.unit)
}

func testMeasurementUnitDuration() {
XCTAssertEqual("nanosecond", MeasurementUnitDuration.nanosecond.unit)
XCTAssertEqual("microsecond", MeasurementUnitDuration.microsecond.unit)
XCTAssertEqual("millisecond", MeasurementUnitDuration.millisecond.unit)
XCTAssertEqual("second", MeasurementUnitDuration.second.unit)
XCTAssertEqual("minute", MeasurementUnitDuration.minute.unit)
XCTAssertEqual("hour", MeasurementUnitDuration.hour.unit)
XCTAssertEqual("day", MeasurementUnitDuration.day.unit)
XCTAssertEqual("week", MeasurementUnitDuration.week.unit)
}

func testMeasurementUnitInformation() {
XCTAssertEqual("bit", MeasurementUnitInformation.bit.unit)
XCTAssertEqual("byte", MeasurementUnitInformation.byte.unit)
XCTAssertEqual("kilobyte", MeasurementUnitInformation.kilobyte.unit)
XCTAssertEqual("kibibyte", MeasurementUnitInformation.kibibyte.unit)
XCTAssertEqual("megabyte", MeasurementUnitInformation.megabyte.unit)
XCTAssertEqual("mebibyte", MeasurementUnitInformation.mebibyte.unit)
XCTAssertEqual("gigabyte", MeasurementUnitInformation.gigabyte.unit)
XCTAssertEqual("gibibyte", MeasurementUnitInformation.gibibyte.unit)
XCTAssertEqual("terabyte", MeasurementUnitInformation.terabyte.unit)
XCTAssertEqual("tebibyte", MeasurementUnitInformation.tebibyte.unit)
XCTAssertEqual("petabyte", MeasurementUnitInformation.petabyte.unit)
XCTAssertEqual("pebibyte", MeasurementUnitInformation.pebibyte.unit)
XCTAssertEqual("exabyte", MeasurementUnitInformation.exabyte.unit)
XCTAssertEqual("exbibyte", MeasurementUnitInformation.exbibyte.unit)
}

func testMeasurementUnitFraction() {
XCTAssertEqual("ratio", MeasurementUnitFraction.ratio.unit)
XCTAssertEqual("percent", MeasurementUnitFraction.percent.unit)
}
}

0 comments on commit c478807

Please sign in to comment.