@everyonesoftware/common 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/.c8rc.json +12 -0
  2. package/.github/workflows/publish.yml +38 -0
  3. package/.mocharc.json +9 -0
  4. package/README.md +9 -0
  5. package/package.json +36 -0
  6. package/sources/assertMessageParameters.ts +22 -0
  7. package/sources/asyncIterator.ts +437 -0
  8. package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +48 -0
  9. package/sources/asyncResult.ts +95 -0
  10. package/sources/basicDisposable.ts +57 -0
  11. package/sources/byteList.ts +202 -0
  12. package/sources/byteListStream.ts +121 -0
  13. package/sources/byteReadStream.ts +24 -0
  14. package/sources/byteWriteStream.ts +16 -0
  15. package/sources/bytes.ts +25 -0
  16. package/sources/characterList.ts +195 -0
  17. package/sources/characterListStream.ts +151 -0
  18. package/sources/characterReadStream.ts +81 -0
  19. package/sources/characterReadStreamIterator.ts +128 -0
  20. package/sources/characterWriteStream.ts +45 -0
  21. package/sources/commandLineParameter.ts +45 -0
  22. package/sources/commandLineParameters.ts +21 -0
  23. package/sources/comparable.ts +144 -0
  24. package/sources/comparer.ts +133 -0
  25. package/sources/comparison.ts +20 -0
  26. package/sources/concatenateIterable.ts +119 -0
  27. package/sources/concatenateIterator.ts +165 -0
  28. package/sources/condition.ts +329 -0
  29. package/sources/currentProcess.ts +158 -0
  30. package/sources/dateTime.ts +130 -0
  31. package/sources/depthFirstSearch.ts +230 -0
  32. package/sources/disposable.ts +31 -0
  33. package/sources/emptyError.ts +10 -0
  34. package/sources/english.ts +45 -0
  35. package/sources/equalFunctions.ts +123 -0
  36. package/sources/fetchHttpClient.ts +89 -0
  37. package/sources/fetchHttpResponse.ts +106 -0
  38. package/sources/flatMapIterable.ts +104 -0
  39. package/sources/flatMapIterator.ts +152 -0
  40. package/sources/generator.ts +251 -0
  41. package/sources/httpClient.ts +36 -0
  42. package/sources/httpHeader.ts +37 -0
  43. package/sources/httpHeaders.ts +216 -0
  44. package/sources/httpIncomingRequest.ts +30 -0
  45. package/sources/httpIncomingResponse.ts +19 -0
  46. package/sources/httpMethod.ts +164 -0
  47. package/sources/httpOutgoingRequest.ts +119 -0
  48. package/sources/httpOutgoingResponse.ts +113 -0
  49. package/sources/httpServer.ts +34 -0
  50. package/sources/inMemoryCharacterWriteStream.ts +78 -0
  51. package/sources/index.ts +101 -0
  52. package/sources/iterable.ts +345 -0
  53. package/sources/iterator.ts +481 -0
  54. package/sources/iteratorToJavascriptIteratorAdapter.ts +48 -0
  55. package/sources/javascript.ts +59 -0
  56. package/sources/javascriptArrayList.ts +175 -0
  57. package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +124 -0
  58. package/sources/javascriptIteratorToIteratorAdapter.ts +133 -0
  59. package/sources/javascriptMapMap.ts +143 -0
  60. package/sources/javascriptSetSet.ts +134 -0
  61. package/sources/list.ts +330 -0
  62. package/sources/listQueue.ts +62 -0
  63. package/sources/listStack.ts +62 -0
  64. package/sources/luxonDateTime.ts +109 -0
  65. package/sources/map.ts +302 -0
  66. package/sources/mapAsyncIterator.ts +141 -0
  67. package/sources/mapIterable.ts +105 -0
  68. package/sources/mapIterator.ts +145 -0
  69. package/sources/mutableCondition.ts +451 -0
  70. package/sources/mutableHttpHeaders.ts +204 -0
  71. package/sources/mutableMap.ts +292 -0
  72. package/sources/network.ts +18 -0
  73. package/sources/node.ts +37 -0
  74. package/sources/nodeJSCharacterWriteStream.ts +42 -0
  75. package/sources/nodeJSHttpIncomingRequest.ts +132 -0
  76. package/sources/nodeJSHttpServer.ts +134 -0
  77. package/sources/notFoundError.ts +12 -0
  78. package/sources/postCondition.ts +284 -0
  79. package/sources/postConditionError.ts +12 -0
  80. package/sources/preCondition.ts +284 -0
  81. package/sources/preConditionError.ts +12 -0
  82. package/sources/promiseAsyncResult.ts +174 -0
  83. package/sources/property.ts +63 -0
  84. package/sources/queue.ts +49 -0
  85. package/sources/realNetwork.ts +28 -0
  86. package/sources/recreationDotGovClient.ts +259 -0
  87. package/sources/searchControl.ts +42 -0
  88. package/sources/set.ts +244 -0
  89. package/sources/skipAsyncIterator.ts +145 -0
  90. package/sources/skipIterator.ts +155 -0
  91. package/sources/stack.ts +48 -0
  92. package/sources/stringComparer.ts +33 -0
  93. package/sources/stringIterator.ts +149 -0
  94. package/sources/strings.ts +322 -0
  95. package/sources/syncResult.ts +300 -0
  96. package/sources/takeAsyncIterator.ts +141 -0
  97. package/sources/takeIterator.ts +151 -0
  98. package/sources/toStringFunctions.ts +185 -0
  99. package/sources/types.ts +371 -0
  100. package/sources/whereAsyncIterator.ts +143 -0
  101. package/sources/whereIterable.ts +108 -0
  102. package/sources/whereIterator.ts +157 -0
  103. package/sources/wonderlandTrailClient.ts +1503 -0
  104. package/tests/assertTest.ts +113 -0
  105. package/tests/assertTestTests.ts +75 -0
  106. package/tests/basicTestSkip.ts +51 -0
  107. package/tests/byteListStreamTests.ts +390 -0
  108. package/tests/byteListTests.ts +27 -0
  109. package/tests/bytesTests.ts +43 -0
  110. package/tests/characterListStreamTests.ts +391 -0
  111. package/tests/characterListTests.ts +250 -0
  112. package/tests/characterWriteStreamTests.ts +12 -0
  113. package/tests/comparerTests.ts +92 -0
  114. package/tests/conditionTests.ts +877 -0
  115. package/tests/consoleTestRunner.ts +404 -0
  116. package/tests/consoleTestRunnerTests.ts +651 -0
  117. package/tests/dateTimeTests.ts +30 -0
  118. package/tests/depthFirstSearchTests.ts +106 -0
  119. package/tests/disposableTests.ts +121 -0
  120. package/tests/englishTests.ts +103 -0
  121. package/tests/equalFunctionsTests.ts +223 -0
  122. package/tests/failedTest.ts +43 -0
  123. package/tests/fetchHttpClientTests.ts +33 -0
  124. package/tests/generatorTests.ts +86 -0
  125. package/tests/httpClientTests.ts +18 -0
  126. package/tests/inMemoryCharacterWriteStreamTests.ts +117 -0
  127. package/tests/iterableTests.ts +141 -0
  128. package/tests/iteratorTests.ts +1086 -0
  129. package/tests/javascriptMapMapTests.ts +21 -0
  130. package/tests/listTests.ts +338 -0
  131. package/tests/mapIteratorTests.ts +55 -0
  132. package/tests/mapTests.ts +104 -0
  133. package/tests/mutableConditionTests.ts +273 -0
  134. package/tests/mutableMapTests.ts +154 -0
  135. package/tests/nodeJSHttpServerTests.ts +75 -0
  136. package/tests/notFoundErrorTests.ts +24 -0
  137. package/tests/postConditionErrorTests.ts +24 -0
  138. package/tests/preConditionErrorTests.ts +24 -0
  139. package/tests/promiseAsyncResultTests.ts +688 -0
  140. package/tests/propertyTests.ts +63 -0
  141. package/tests/queueTests.ts +29 -0
  142. package/tests/recreationDotGovClientTests.ts +191 -0
  143. package/tests/setTests.ts +140 -0
  144. package/tests/skippedTest.ts +39 -0
  145. package/tests/stackTests.ts +66 -0
  146. package/tests/stringComparerTests.ts +60 -0
  147. package/tests/stringIteratorTests.ts +156 -0
  148. package/tests/stringsTests.ts +516 -0
  149. package/tests/syncResultTests.ts +1251 -0
  150. package/tests/test.ts +228 -0
  151. package/tests/testAction.ts +75 -0
  152. package/tests/testActionTests.ts +93 -0
  153. package/tests/testFailureTests.ts +12 -0
  154. package/tests/testRunner.ts +267 -0
  155. package/tests/testRunnerTests.ts +895 -0
  156. package/tests/testSkip.ts +34 -0
  157. package/tests/tests.ts +103 -0
  158. package/tests/toStringFunctionsTests.ts +55 -0
  159. package/tests/typesTests.ts +257 -0
  160. package/tests/whereIteratorTests.ts +77 -0
  161. package/tests/wonderlandTrailClientTests.ts +452 -0
  162. package/tsconfig.json +17 -0
  163. package/tsup.config.ts +13 -0
@@ -0,0 +1,113 @@
1
+ import * as assert from "assert";
2
+
3
+ import { PreCondition } from "../sources/preCondition";
4
+ import { isFunction, Type } from "../sources/types";
5
+ import { Test } from "./test";
6
+ import { SyncResult } from "../sources/syncResult";
7
+ import { PromiseAsyncResult } from "../sources/promiseAsyncResult";
8
+
9
+ /**
10
+ * A {@link Test} type that uses the standard "assert" module to make assertions.
11
+ */
12
+ export class AssertTest implements Test
13
+ {
14
+ private readonly name: string;
15
+
16
+ protected constructor(name: string)
17
+ {
18
+ this.name = name;
19
+ }
20
+
21
+ /**
22
+ * Create a new {@link AssertTest} object.
23
+ */
24
+ public static create(name: string): AssertTest
25
+ {
26
+ return new AssertTest(name);
27
+ }
28
+
29
+ public fail(message: string): never
30
+ {
31
+ PreCondition.assertNotEmpty(message, "message");
32
+
33
+ assert.fail(message);
34
+ }
35
+
36
+ public assertUndefined(value: unknown): asserts value is undefined
37
+ {
38
+ Test.assertUndefined(this, value);
39
+ }
40
+
41
+ public assertNotUndefined<T>(value: T): asserts value is NonNullable<T>
42
+ {
43
+ Test.assertNotUndefined(this, value);
44
+ }
45
+
46
+ public assertNull(value: unknown): asserts value is null
47
+ {
48
+ Test.assertNull(this, value);
49
+ }
50
+
51
+ public assertNotNull<T>(value: T): asserts value is NonNullable<T>
52
+ {
53
+ Test.assertNotNull(this, value);
54
+ }
55
+
56
+ public assertNotUndefinedAndNotNull<T>(value: T): asserts value is NonNullable<T>
57
+ {
58
+ Test.assertNotUndefinedAndNotNull(this, value);
59
+ }
60
+
61
+ public assertSame<T>(left: T, right: T): void
62
+ {
63
+ assert.strictEqual(left, right);
64
+ }
65
+
66
+ public assertNotSame<T>(left: T, right: T): void
67
+ {
68
+ assert.notStrictEqual(left, right);
69
+ }
70
+
71
+ public assertEqual<T>(left: T, right: T, message?: string): void
72
+ {
73
+ assert.deepStrictEqual(left, right, message);
74
+ }
75
+
76
+ public assertNotEqual<T>(left: T, right: T): void
77
+ {
78
+ assert.notDeepStrictEqual(left, right);
79
+ }
80
+
81
+ public assertFalse(value: boolean): void
82
+ {
83
+ Test.assertFalse(this, value);
84
+ }
85
+
86
+ public assertTrue(value: boolean): void
87
+ {
88
+ Test.assertTrue(this, value);
89
+ }
90
+
91
+ public assertThrows(action: SyncResult<unknown> | (() => void), expectedError?: Error): void
92
+ {
93
+ if (!isFunction(action))
94
+ {
95
+ const syncResult: SyncResult<unknown> = action;
96
+ action = () => { syncResult.await(); };
97
+ }
98
+ assert.throws(action, expectedError);
99
+ }
100
+
101
+ public assertThrowsAsync(action: Promise<unknown> | (() => Promise<unknown>), expectedError: Error): PromiseAsyncResult<void>
102
+ {
103
+ const promiseOrAsyncAction = isFunction(action)
104
+ ? async () => await action()
105
+ : action;
106
+ return PromiseAsyncResult.create(assert.rejects(promiseOrAsyncAction, expectedError));
107
+ }
108
+
109
+ public assertInstanceOf<T>(value: unknown, type: Type<T>, typeCheck?: (value: unknown) => value is T): asserts value is T
110
+ {
111
+ Test.assertInstanceOf(this, value, type, typeCheck);
112
+ }
113
+ }
@@ -0,0 +1,75 @@
1
+ import { AssertionError } from "assert";
2
+ import { AssertTest } from "./assertTest";
3
+ import { Test } from "./test";
4
+ import { TestRunner } from "./testRunner";
5
+
6
+ export function test(runner: TestRunner): void
7
+ {
8
+ runner.testFile("assertTest.ts", () =>
9
+ {
10
+ runner.testType("AssertTest", () =>
11
+ {
12
+ runner.testFunction("assertThrows()", () =>
13
+ {
14
+ runner.test("with throwing action", (test: Test) =>
15
+ {
16
+ const at: AssertTest = AssertTest.create("fake-test-name");
17
+ at.assertThrows(() => { throw new Error("abc"); }, new Error("abc"));
18
+ });
19
+
20
+ runner.test("with non-throwing action", (test: Test) =>
21
+ {
22
+ const at: AssertTest = AssertTest.create("fake-test-name");
23
+ test.assertThrows(
24
+ () => at.assertThrows(() => {}, new Error("oops")),
25
+ new AssertionError({
26
+ message: "Missing expected exception (Error).",
27
+ operator: "throws",
28
+ expected: new Error("oops"),
29
+ }),
30
+ );
31
+ });
32
+ });
33
+
34
+ runner.testFunction("assertThrowsAsync()", () =>
35
+ {
36
+ runner.test("with throwing sync action", async (test: Test) =>
37
+ {
38
+ const at: AssertTest = AssertTest.create("fake-test-name");
39
+ await at.assertThrowsAsync(() => { throw new Error("abc"); }, new Error("abc"));
40
+ });
41
+
42
+ runner.test("with throwing async action", async (test: Test) =>
43
+ {
44
+ const at: AssertTest = AssertTest.create("fake-test-name");
45
+ await at.assertThrowsAsync(async () => { throw new Error("abc"); }, new Error("abc"));
46
+ });
47
+
48
+ runner.test("with rejected Promise", async (test: Test) =>
49
+ {
50
+ const at: AssertTest = AssertTest.create("fake-test-name");
51
+ await at.assertThrowsAsync(Promise.reject(new Error("abc")), new Error("abc"));
52
+ });
53
+
54
+ runner.test("with throwing action that returns a rejected Promise", async (test: Test) =>
55
+ {
56
+ const at: AssertTest = AssertTest.create("fake-test-name");
57
+ await at.assertThrowsAsync(() => Promise.reject(new Error("abc")), new Error("abc"));
58
+ });
59
+
60
+ runner.test("with non-throwing async action", async (test: Test) =>
61
+ {
62
+ const at: AssertTest = AssertTest.create("fake-test-name");
63
+ await test.assertThrowsAsync(
64
+ async () => await at.assertThrowsAsync(async () => {}, new Error("oops")),
65
+ new AssertionError({
66
+ message: "Missing expected rejection (Error).",
67
+ operator: "rejects",
68
+ expected: new Error("oops"),
69
+ }),
70
+ );
71
+ });
72
+ });
73
+ });
74
+ });
75
+ }
@@ -0,0 +1,51 @@
1
+ import { PreCondition } from "../sources/preCondition";
2
+ import { TestSkip } from "./testSkip";
3
+
4
+ /**
5
+ * A type that is used to mark that a test group or a test should be skipped.
6
+ */
7
+ export class BasicTestSkip implements TestSkip
8
+ {
9
+ private readonly shouldSkip: boolean;
10
+ private readonly message: string;
11
+
12
+ private constructor(shouldSkip: boolean, message: string)
13
+ {
14
+ PreCondition.assertNotUndefinedAndNotNull(message, "message");
15
+
16
+ this.shouldSkip = shouldSkip;
17
+ this.message = message;
18
+ }
19
+
20
+ /**
21
+ * Create a new {@link TestSkip} with the provided properties.
22
+ * @param shouldSkip Whether the tests associated with the new {@link TestSkip}
23
+ * should be skipped.
24
+ * @param message The message that explains why the tests associated with this {@link TestSkip}
25
+ * should be skipped.
26
+ */
27
+ public static create(shouldSkip?: boolean, message?: string): BasicTestSkip
28
+ {
29
+ if (shouldSkip === undefined || shouldSkip === null)
30
+ {
31
+ shouldSkip = true;
32
+ }
33
+
34
+ if (message === undefined || message === null)
35
+ {
36
+ message = "";
37
+ }
38
+
39
+ return new BasicTestSkip(shouldSkip, message);
40
+ }
41
+
42
+ public getShouldSkip(): boolean
43
+ {
44
+ return this.shouldSkip;
45
+ }
46
+
47
+ public getMessage(): string
48
+ {
49
+ return this.message;
50
+ }
51
+ }
@@ -0,0 +1,390 @@
1
+ import { ByteListStream } from "../sources/byteListStream";
2
+ import { EmptyError } from "../sources/emptyError";
3
+ import { PreConditionError } from "../sources/preConditionError";
4
+ import { Test } from "./test";
5
+ import { TestRunner } from "./testRunner";
6
+
7
+ export function test(runner: TestRunner): void
8
+ {
9
+ runner.testFile("byteListStream.ts", () =>
10
+ {
11
+ runner.testType("ByteListStream", () =>
12
+ {
13
+ runner.testFunction("create()", () =>
14
+ {
15
+ runner.test("with no arguments", (test: Test) =>
16
+ {
17
+ const stream: ByteListStream = ByteListStream.create();
18
+ test.assertNotUndefinedAndNotNull(stream);
19
+ test.assertEqual(0, stream.getAvailableByteCount());
20
+ });
21
+
22
+ runner.test("with empty array", (test: Test) =>
23
+ {
24
+ const stream: ByteListStream = ByteListStream.create([]);
25
+ test.assertNotUndefinedAndNotNull(stream);
26
+ test.assertEqual(0, stream.getAvailableByteCount());
27
+ });
28
+
29
+ runner.test("with non-empty array", (test: Test) =>
30
+ {
31
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
32
+ test.assertNotUndefinedAndNotNull(stream);
33
+ test.assertEqual(3, stream.getAvailableByteCount());
34
+ });
35
+ });
36
+
37
+ runner.testFunction("writeBytes()", () =>
38
+ {
39
+ runner.test("with undefined bytes", (test: Test) =>
40
+ {
41
+ const stream: ByteListStream = ByteListStream.create();
42
+ test.assertThrows(() => stream.writeBytes(undefined!), new PreConditionError(
43
+ "Expression: bytes",
44
+ "Expected: not undefined and not null",
45
+ "Actual: undefined",
46
+ ));
47
+ test.assertEqual(0, stream.getAvailableByteCount());
48
+ });
49
+
50
+ runner.test("with null bytes", (test: Test) =>
51
+ {
52
+ const stream: ByteListStream = ByteListStream.create();
53
+ test.assertThrows(() => stream.writeBytes(null!), new PreConditionError(
54
+ "Expression: bytes",
55
+ "Expected: not undefined and not null",
56
+ "Actual: null",
57
+ ));
58
+ test.assertEqual(0, stream.getAvailableByteCount());
59
+ });
60
+
61
+ runner.test("with empty bytes", (test: Test) =>
62
+ {
63
+ const stream: ByteListStream = ByteListStream.create();
64
+ test.assertEqual(0, stream.writeBytes([]).await());
65
+ test.assertEqual(0, stream.getAvailableByteCount());
66
+ });
67
+
68
+ runner.test("with non-empty bytes", (test: Test) =>
69
+ {
70
+ const stream: ByteListStream = ByteListStream.create();
71
+
72
+ test.assertEqual(4, stream.writeBytes([1, 2, 3, 4]).await());
73
+ test.assertEqual(4, stream.getAvailableByteCount());
74
+
75
+ const bytes: Uint8Array = stream.readBytes(10).await();
76
+ test.assertNotUndefinedAndNotNull(bytes);
77
+ test.assertEqual(4, bytes.length);
78
+ test.assertEqual(new Uint8Array([1, 2, 3, 4]), bytes);
79
+ test.assertEqual(0, stream.getAvailableByteCount());
80
+ });
81
+
82
+ runner.test("with negative startIndex", (test: Test) =>
83
+ {
84
+ const stream: ByteListStream = ByteListStream.create();
85
+ test.assertThrows(() => stream.writeBytes([1, 2], -1), new PreConditionError(
86
+ "Expression: startIndex",
87
+ "Expected: between 0 and 2",
88
+ "Actual: -1",
89
+ ));
90
+ test.assertEqual(0, stream.getAvailableByteCount());
91
+ });
92
+
93
+ runner.test("with too large startIndex", (test: Test) =>
94
+ {
95
+ const stream: ByteListStream = ByteListStream.create();
96
+ test.assertThrows(() => stream.writeBytes([1, 2], 3), new PreConditionError(
97
+ "Expression: startIndex",
98
+ "Expected: between 0 and 2",
99
+ "Actual: 3",
100
+ ));
101
+ test.assertEqual(0, stream.getAvailableByteCount());
102
+ });
103
+
104
+ runner.test("with valid non-zero startIndex", (test: Test) =>
105
+ {
106
+ const stream: ByteListStream = ByteListStream.create();
107
+ const writeBytesResult: number = stream.writeBytes([1, 2], 1).await();
108
+ test.assertEqual(1, writeBytesResult);
109
+ test.assertEqual(1, stream.getAvailableByteCount());
110
+
111
+ test.assertEqual(new Uint8Array([2]), stream.readBytes(5).await());
112
+ test.assertEqual(0, stream.getAvailableByteCount());
113
+ });
114
+
115
+ runner.test("with startIndex equal to bytes length", (test: Test) =>
116
+ {
117
+ const stream: ByteListStream = ByteListStream.create();
118
+ const writeBytesResult: number = stream.writeBytes([1, 2], 2).await();
119
+ test.assertEqual(0, writeBytesResult);
120
+ test.assertEqual(0, stream.getAvailableByteCount());
121
+ });
122
+
123
+ runner.test("with negative length", (test: Test) =>
124
+ {
125
+ const stream: ByteListStream = ByteListStream.create();
126
+ test.assertThrows(() => stream.writeBytes([1, 2], 0, -1), new PreConditionError(
127
+ "Expression: length",
128
+ "Expected: between 0 and 2",
129
+ "Actual: -1",
130
+ ));
131
+ test.assertEqual(0, stream.getAvailableByteCount());
132
+ });
133
+
134
+ runner.test("with too large length", (test: Test) =>
135
+ {
136
+ const stream: ByteListStream = ByteListStream.create();
137
+ test.assertThrows(() => stream.writeBytes([1, 2], 0, 3), new PreConditionError(
138
+ "Expression: length",
139
+ "Expected: between 0 and 2",
140
+ "Actual: 3",
141
+ ));
142
+ test.assertEqual(0, stream.getAvailableByteCount());
143
+ });
144
+
145
+ runner.test("with valid startIndex and length values", (test: Test) =>
146
+ {
147
+ const stream: ByteListStream = ByteListStream.create();
148
+ const writeBytesResult: number = stream.writeBytes([1, 2, 3, 4, 5], 2, 2).await();
149
+ test.assertEqual(2, writeBytesResult);
150
+ test.assertEqual(2, stream.getAvailableByteCount());
151
+
152
+ test.assertEqual(new Uint8Array([3, 4]), stream.readBytes(50).await());
153
+ test.assertEqual(0, stream.getAvailableByteCount());
154
+ });
155
+ });
156
+
157
+ runner.testFunction("readBytes()", () =>
158
+ {
159
+ runner.testGroup("with empty stream", () =>
160
+ {
161
+ runner.test("with negative count", (test: Test) =>
162
+ {
163
+ const stream: ByteListStream = ByteListStream.create();
164
+ test.assertThrows(() => stream.readBytes(-1).await(), new PreConditionError(
165
+ "Expression: count",
166
+ "Expected: greater than or equal to 0",
167
+ "Actual: -1",
168
+ ));
169
+ test.assertEqual(0, stream.getAvailableByteCount());
170
+ });
171
+
172
+ runner.test("with zero count", (test: Test) =>
173
+ {
174
+ const stream: ByteListStream = ByteListStream.create();
175
+ test.assertThrows(() => stream.readBytes(0).await(), new EmptyError());
176
+ test.assertEqual(0, stream.getAvailableByteCount());
177
+ });
178
+
179
+ runner.test("with positive count", (test: Test) =>
180
+ {
181
+ const stream: ByteListStream = ByteListStream.create();
182
+ test.assertThrows(() => stream.readBytes(1).await(), new EmptyError());
183
+ test.assertEqual(0, stream.getAvailableByteCount());
184
+ });
185
+ });
186
+
187
+ runner.testGroup("with non-empty stream", () =>
188
+ {
189
+ runner.test("with negative count", (test: Test) =>
190
+ {
191
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
192
+ test.assertThrows(() => stream.readBytes(-1).await(), new PreConditionError(
193
+ "Expression: count",
194
+ "Expected: greater than or equal to 0",
195
+ "Actual: -1",
196
+ ));
197
+ test.assertEqual(3, stream.getAvailableByteCount());
198
+ });
199
+
200
+ runner.test("with zero count", (test: Test) =>
201
+ {
202
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
203
+ const readBytesResult: Uint8Array = stream.readBytes(0).await();
204
+ test.assertEqual(new Uint8Array(), readBytesResult);
205
+ test.assertEqual(3, stream.getAvailableByteCount());
206
+ });
207
+
208
+ runner.test("with positive count less than bytes available", (test: Test) =>
209
+ {
210
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
211
+ const readBytesResult: Uint8Array = stream.readBytes(2).await();
212
+ test.assertEqual(new Uint8Array([1, 2]), readBytesResult);
213
+ test.assertEqual(1, stream.getAvailableByteCount());
214
+ });
215
+
216
+ runner.test("with positive count equal to bytes available", (test: Test) =>
217
+ {
218
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
219
+ const readBytesResult: Uint8Array = stream.readBytes(3).await();
220
+ test.assertEqual(new Uint8Array([1, 2, 3]), readBytesResult);
221
+ test.assertEqual(0, stream.getAvailableByteCount());
222
+ });
223
+
224
+ runner.test("with positive count greater than bytes available", (test: Test) =>
225
+ {
226
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
227
+ const readBytesResult: Uint8Array = stream.readBytes(4).await();
228
+ test.assertEqual(new Uint8Array([1, 2, 3]), readBytesResult);
229
+ test.assertEqual(0, stream.getAvailableByteCount());
230
+ });
231
+
232
+ runner.test("with undefined output", (test: Test) =>
233
+ {
234
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
235
+ test.assertThrows(() => stream.readBytes(undefined!), new PreConditionError(
236
+ "Expression: output",
237
+ "Expected: not undefined and not null",
238
+ "Actual: undefined",
239
+ ));
240
+ test.assertEqual(3, stream.getAvailableByteCount());
241
+ });
242
+
243
+ runner.test("with null output", (test: Test) =>
244
+ {
245
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
246
+ test.assertThrows(() => stream.readBytes(null!), new PreConditionError(
247
+ "Expression: output",
248
+ "Expected: not undefined and not null",
249
+ "Actual: null",
250
+ ));
251
+ test.assertEqual(3, stream.getAvailableByteCount());
252
+ });
253
+
254
+ runner.test("with empty output", (test: Test) =>
255
+ {
256
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
257
+ const output: Uint8Array = new Uint8Array();
258
+ const readBytesResult: number = stream.readBytes(output).await();
259
+ test.assertEqual(0, readBytesResult);
260
+ test.assertEqual(output, new Uint8Array());
261
+ test.assertEqual(3, stream.getAvailableByteCount());
262
+ });
263
+
264
+ runner.test("with output smaller than the available bytes", (test: Test) =>
265
+ {
266
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
267
+ const output: Uint8Array = new Uint8Array(1);
268
+ const readBytesResult: number = stream.readBytes(output).await();
269
+ test.assertEqual(1, readBytesResult);
270
+ test.assertEqual(output, new Uint8Array([1]));
271
+ test.assertEqual(2, stream.getAvailableByteCount());
272
+ });
273
+
274
+ runner.test("with output equal to the available bytes", (test: Test) =>
275
+ {
276
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
277
+ const output: Uint8Array = new Uint8Array(3);
278
+ const readBytesResult: number = stream.readBytes(output).await();
279
+ test.assertEqual(3, readBytesResult);
280
+ test.assertEqual(output, new Uint8Array([1, 2, 3]));
281
+ test.assertEqual(0, stream.getAvailableByteCount());
282
+ });
283
+
284
+ runner.test("with output larger than available bytes", (test: Test) =>
285
+ {
286
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
287
+ const output: Uint8Array = new Uint8Array(5);
288
+ const readBytesResult: number = stream.readBytes(output).await();
289
+ test.assertEqual(3, readBytesResult);
290
+ test.assertEqual(output, new Uint8Array([1, 2, 3, 0, 0]));
291
+ test.assertEqual(0, stream.getAvailableByteCount());
292
+ });
293
+
294
+ runner.test("with negative startIndex", (test: Test) =>
295
+ {
296
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
297
+ const output: Uint8Array = new Uint8Array(5);
298
+ test.assertThrows(() => stream.readBytes(output, -1), new PreConditionError(
299
+ "Expression: startIndex",
300
+ "Expected: between 0 and 5",
301
+ "Actual: -1",
302
+ ));
303
+ test.assertEqual(3, stream.getAvailableByteCount());
304
+ });
305
+
306
+ runner.test("with too large startIndex", (test: Test) =>
307
+ {
308
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
309
+ const output: Uint8Array = new Uint8Array(5);
310
+ test.assertThrows(() => stream.readBytes(output, 6), new PreConditionError(
311
+ "Expression: startIndex",
312
+ "Expected: between 0 and 5",
313
+ "Actual: 6",
314
+ ));
315
+ test.assertEqual(3, stream.getAvailableByteCount());
316
+ });
317
+
318
+ runner.test("with startIndex with enough space to read the entire stream", (test: Test) =>
319
+ {
320
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
321
+ const output: Uint8Array = new Uint8Array(5);
322
+ test.assertEqual(3, stream.readBytes(output, 2).await());
323
+ test.assertEqual(new Uint8Array([0, 0, 1, 2, 3]), output);
324
+ test.assertEqual(0, stream.getAvailableByteCount());
325
+ });
326
+
327
+ runner.test("with startIndex with not enough space to read the entire stream", (test: Test) =>
328
+ {
329
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
330
+ const output: Uint8Array = new Uint8Array(5);
331
+ test.assertEqual(2, stream.readBytes(output, 3).await());
332
+ test.assertEqual(new Uint8Array([0, 0, 0, 1, 2]), output);
333
+ test.assertEqual(1, stream.getAvailableByteCount());
334
+ });
335
+
336
+ runner.test("with startIndex equal to output length", (test: Test) =>
337
+ {
338
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
339
+ const output: Uint8Array = new Uint8Array(5);
340
+ test.assertEqual(0, stream.readBytes(output, 5).await());
341
+ test.assertEqual(new Uint8Array(5), output);
342
+ test.assertEqual(3, stream.getAvailableByteCount());
343
+ });
344
+
345
+ runner.test("with negative count", (test: Test) =>
346
+ {
347
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
348
+ const output: Uint8Array = new Uint8Array(5);
349
+ test.assertThrows(() => stream.readBytes(output, 1, -1), new PreConditionError(
350
+ "Expression: count",
351
+ "Expected: between 0 and 4",
352
+ "Actual: -1",
353
+ ));
354
+ test.assertEqual(3, stream.getAvailableByteCount());
355
+ });
356
+
357
+ runner.test("with count larger than output.length - startIndex", (test: Test) =>
358
+ {
359
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
360
+ const output: Uint8Array = new Uint8Array(5);
361
+ test.assertThrows(() => stream.readBytes(output, 1, 5), new PreConditionError(
362
+ "Expression: count",
363
+ "Expected: between 0 and 4",
364
+ "Actual: 5",
365
+ ));
366
+ test.assertEqual(3, stream.getAvailableByteCount());
367
+ });
368
+
369
+ runner.test("with startIndex with enough space to read the entire stream", (test: Test) =>
370
+ {
371
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
372
+ const output: Uint8Array = new Uint8Array(5);
373
+ test.assertEqual(3, stream.readBytes(output, 2, 3).await());
374
+ test.assertEqual(new Uint8Array([0, 0, 1, 2, 3]), output);
375
+ test.assertEqual(0, stream.getAvailableByteCount());
376
+ });
377
+
378
+ runner.test("with startIndex with not enough space to read the entire stream", (test: Test) =>
379
+ {
380
+ const stream: ByteListStream = ByteListStream.create([1, 2, 3]);
381
+ const output: Uint8Array = new Uint8Array(5);
382
+ test.assertEqual(1, stream.readBytes(output, 3, 1).await());
383
+ test.assertEqual(new Uint8Array([0, 0, 0, 1, 0]), output);
384
+ test.assertEqual(2, stream.getAvailableByteCount());
385
+ });
386
+ });
387
+ });
388
+ });
389
+ });
390
+ }
@@ -0,0 +1,27 @@
1
+ import { ByteList } from "../sources/byteList";
2
+ import { EqualFunctions } from "../sources/equalFunctions";
3
+ import { NotFoundError } from "../sources/notFoundError";
4
+ import { PreConditionError } from "../sources/preConditionError";
5
+ import { isNumber } from "../sources/types";
6
+ import { Test } from "./test";
7
+ import { TestRunner } from "./testRunner";
8
+
9
+ export function test(runner: TestRunner): void
10
+ {
11
+ runner.testFile("byteList.ts", () =>
12
+ {
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+ });
27
+ }