-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9138b3
commit 15d5493
Showing
6 changed files
with
123 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { User, Moderator, Administrator } from "./Users"; | ||
|
||
class UserView { | ||
|
||
protected user: User = new User(); | ||
|
||
constructor(user?: User) { | ||
if(user != undefined) this.user = user; | ||
} | ||
|
||
public getUser(): User { | ||
return this.user; | ||
} | ||
|
||
public setUser(user: User): void { | ||
this.user = user; | ||
} | ||
|
||
} | ||
|
||
class ModeratorView extends UserView { | ||
|
||
constructor(mod?: Moderator) { | ||
super(mod); | ||
} | ||
|
||
// Covariant redefinition of return type is allowed | ||
public getUser(): Moderator { | ||
return this.user as Moderator; | ||
} | ||
|
||
// Covariant redefinition of argument type (why is this allowed?) | ||
public setUser(user: Moderator): void { | ||
this.user = user; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Example for covariant redefinition of return type | ||
*/ | ||
let modView1: ModeratorView = new ModeratorView(new Moderator()); | ||
let modAsUserView1: UserView = modView1 as UserView; | ||
let mod1: Moderator = modAsUserView1.getUser() as Moderator; | ||
mod1.moderate(); // should work, no problem | ||
|
||
/** | ||
* Example for covariant redefinition of argument type | ||
*/ | ||
let modView2: ModeratorView = new ModeratorView(new Moderator()); | ||
let modAsUserView2 = modView2 as UserView; | ||
modAsUserView2.setUser(new User()); // sets up for failure | ||
let mod2: Moderator = modView2.getUser(); // creates failure point | ||
mod2.moderate(); // should fail because mod2 is of dyanmic type User | ||
|
||
class AdministratorView extends ModeratorView { | ||
|
||
constructor(admin?: Administrator) { | ||
super(admin); | ||
} | ||
|
||
// @ts-expect-error Contravariant redefinition of return type not allowed | ||
public getUser(): User { | ||
return this.user; | ||
} | ||
|
||
// Contravariant redefinition of argument type is allowed | ||
public setUser(user: User): void { | ||
this.user = user; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Example for contravariant redefinition of return type | ||
*/ | ||
let adminView: AdministratorView = new AdministratorView(); | ||
let adminViewAsModView1: ModeratorView = adminView as ModeratorView; | ||
let mod3: Moderator = adminViewAsModView1.getUser(); | ||
mod3.moderate() // will fail because mod3 is of dynamic type User | ||
|
||
/** | ||
* Example 1 for contravariant redefinition of argument type | ||
*/ | ||
adminView.setUser(new User()); | ||
let user1: User = adminView.getUser(); | ||
user1.use(); // no problem | ||
let admin1: Administrator = user1 as Administrator; | ||
admin1.administer(); // will fail but also was not promised | ||
|
||
/** | ||
* Example 2 for contravariant redefinition of argument type | ||
*/ | ||
adminView.setUser(new Administrator()); | ||
let user2: User = adminView.getUser(); | ||
user2.use(); // still no problem | ||
let admin2: Administrator = user2 as Administrator; | ||
admin2.administer(); // will work but only because of extra knowledge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, it, expect } from "vitest"; | ||
|
||
import { IllegalArgumentException } from "../../../src/adap-b04/common/IllegalArgumentException"; | ||
import { MethodFailureException } from "../../../src/adap-b04/common/MethodFailureException"; | ||
import { InvalidStateException } from "../../../src/adap-b04/common/InvalidStateException"; | ||
|
||
describe("Asserting not null or undefined", () => { | ||
it("test asserIsNotNullOrUndefined", async () => { | ||
const exMsg: string = "null or undefined"; | ||
|
||
IllegalArgumentException.assertIsNotNullOrUndefined("hurray!"); | ||
expect(() => IllegalArgumentException.assertIsNotNullOrUndefined(null)).toThrow(new IllegalArgumentException(exMsg)); | ||
|
||
MethodFailureException.assertIsNotNullOrUndefined("hurray!"); | ||
expect(() => MethodFailureException.assertIsNotNullOrUndefined(null)).toThrow(new MethodFailureException(exMsg)); | ||
|
||
InvalidStateException.assertIsNotNullOrUndefined("hurray!"); | ||
expect(() => InvalidStateException.assertIsNotNullOrUndefined(null)).toThrow(new InvalidStateException(exMsg)); | ||
}); | ||
}); |