Skip to content

Commit

Permalink
Bugfix: assertions were being thrown if class names were changed code…
Browse files Browse the repository at this point in the history
… minifiers.
  • Loading branch information
cowwoc committed Nov 30, 2023
1 parent 36115ca commit c9d722c
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ A [fluent API](https://en.wikipedia.org/wiki/Fluent_interface) for enforcing
To get started, add this dependency:

```shell
npm install --save @cowwoc/[email protected].1
npm install --save @cowwoc/[email protected].2
```

or [pnpm](https://pnpm.io/):

```shell
pnpm add @cowwoc/[email protected].1
pnpm add @cowwoc/[email protected].2
```

The contents of the API classes depend on which [modules](wiki/Supported_Libraries.md) are enabled.
Expand Down
6 changes: 5 additions & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
Minor updates involving cosmetic changes have been omitted from this list. See
https://github.com/cowwoc/requirements.java/commits/master for a full list.

## Version 3.3.2 - 2023/11/30

* Bugfix: Assertions were being thrown if class names were changed code minifiers.

## Version 3.3.1 - 2023/11/29

* Bugfix: Output ESM format to browsers instead of IIFE
* Bugfix: Output ESM format to browsers instead of IIFE.

## Version 3.3.0 - 2023/11/29

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cowwoc/requirements",
"version": "3.3.1",
"version": "3.3.2",
"keywords": [
"preconditions",
"postconditions",
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration.mts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Configuration
typeToStringConverter = new Map<string, (input: unknown) => string>())
{
this.globalConfiguration = globalConfiguration;
Objects.assertThatObjectOf(context, "context", "Map");
Objects.assertThatInstanceOf(context, "context", Map);
this.context = context;
if (typeof (assertionsEnabled) === "undefined")
assertionsEnabled = globalConfiguration.assertionsAreEnabled();
Expand Down
8 changes: 4 additions & 4 deletions src/ValidationFailure.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {Configuration} from "./internal/internal.mjs";
import {
ContextLine,
Objects
Objects,
Configuration
} from "./internal/internal.mjs";

/**
Expand All @@ -27,7 +27,7 @@ class ValidationFailure
*/
constructor(configuration: Configuration, exceptionType: new (message: string) => Error, message: string)
{
Objects.assertThatObjectOf(configuration, "configuration", "Configuration");
Objects.assertThatInstanceOf(configuration, "configuration", Configuration);
Objects.requireThatInstanceOf(exceptionType, "exceptionType", Function);
Objects.requireThatStringIsNotEmpty(message, "message");

Expand Down Expand Up @@ -111,7 +111,7 @@ class ValidationFailure
const existingKeys = new Set();
for (const entry of this.context)
{
Objects.requireThatObjectOf(entry, "entry", ContextLine.name);
Objects.requireThatInstanceOf(entry, "entry", ContextLine);
mergedContext.push(entry);
if (entry.key !== "")
existingKeys.add(entry.key);
Expand Down
51 changes: 0 additions & 51 deletions src/internal/Objects.mts
Original file line number Diff line number Diff line change
Expand Up @@ -207,42 +207,6 @@ class Objects
return true;
}

/**
* Requires that a value is an object whose class has the specified name if assertions are enabled. We
* assume that <code>Objects.assert()</code> will be stripped out at build-time if assertions are disabled.
*
* @param value - the value of a parameter
* @param name - the name of the parameter
* @param clazz - he expected class name of <code>value</code>
* @returns <code>true</code>
* @throws TypeError if <code>value</code> is not an object of type <code>clazz</code>. If
* <code>name</code> is not a string.
*/
static requireThatObjectOf(value: unknown, name: string, clazz: string)
{
const nameInfo = Objects.getTypeInfo(name);
if (nameInfo.type !== "string")
{
throw new TypeError("name must be a string.\n" +
"Actual: " + Objects.toString(name) + "\n" +
"Type : " + nameInfo.toString());
}
const valueInfo = Objects.getTypeInfo(value);
if (valueInfo.type !== "object")
{
throw new TypeError("value must be an object.\n" +
"Actual: " + Objects.toString(value) + "\n" +
"Type : " + valueInfo.toString());
}
if (valueInfo.name !== clazz)
{
throw new TypeError(name + " must be an object of type " + clazz + ".\n" +
"Actual: " + Objects.toString(value) + "\n" +
"Type : " + valueInfo.toString());
}
return true;
}

/**
* Requires that an object is an instance of <code>type</code>.
*
Expand Down Expand Up @@ -290,21 +254,6 @@ class Objects
Objects.assert(this.requireThatTypeOf(value, name, type));
}

/**
* Requires that a value is an object whose class has the specified name if assertions are enabled. We
* assume that <code>Objects.assert()</code> will be stripped out at build-time if assertions are disabled.
*
* @param value - the value of a parameter
* @param name - the name of the parameter
* @param type - the expected class name of <code>value</code>
* @throws TypeError if <code>value</code> is not an object of type <code>type</code>. If
* <code>name</code> is not a string.
*/
static assertThatObjectOf(value: unknown, name: string, type: string): void
{
Objects.assert(this.requireThatObjectOf(value, name, type));
}

/**
* Requires that an object is an instance of the expected type.
*
Expand Down
2 changes: 1 addition & 1 deletion src/internal/Strings.mts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Strings
static lastIndexOf(source: string, target: RegExp): SearchResult | null
{
Objects.assertThatTypeOf(source, "source", "string");
Objects.assertThatObjectOf(target, "target", "RegExp");
Objects.assertThatInstanceOf(target, "target", RegExp);

// RegExp is stateful: https://stackoverflow.com/a/11477448/14731
let flags = target.flags;
Expand Down
4 changes: 2 additions & 2 deletions src/internal/diff/ContextGenerator.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isEqual from "lodash/isEqual.js";
import type {Configuration} from "../internal.mjs";
import {
Configuration,
ContextLine,
DiffGenerator,
Objects,
Expand Down Expand Up @@ -29,7 +29,7 @@ class ContextGenerator
*/
constructor(configuration: Configuration)
{
Objects.assertThatObjectOf(configuration, "configuration", "Configuration");
Objects.assertThatInstanceOf(configuration, "configuration", Configuration);

this.config = configuration;
this.diffGenerator = new DiffGenerator(configuration.getGlobalConfiguration().getTerminalEncoding());
Expand Down

0 comments on commit c9d722c

Please sign in to comment.